做一个小特效,让星星亮起来,非常简单!

这篇博客介绍如何使用HTML、CSS和JavaScript创建一个点击按钮后星星闪烁的特效。通过在HTML中设置input按钮和div容器,CSS定义样式,JavaScript处理点击事件并动态插入星星元素,实现了星星在页面上随机闪烁的效果。
摘要由CSDN通过智能技术生成

 HTML部分:

第一步我们需要写一个input标签,我给这个input标签命名为“点击亮起来”,再将input标签的类型改为button,我命名id为“btn”。然后第二步就是写一个div标签,id为“dvn”,这个div标签的作用是用来装从JavaScript传过来的星星的。代码如下图所示:

   CSS部分:

代码如图所示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例代码,可以实现鼠标移动时,产生星星特效效果: HTML代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>鼠标粒子星星特效</title> <style> canvas { position: absolute; top: 0; left: 0; z-index: -1; } </style> </head> <body> <h1>鼠标粒子星星特效</h1> <canvas id="canvas"></canvas> <script src="stars.js"></script> </body> </html> ``` JS代码(保存为stars.js文件): ```javascript var canvas = document.getElementById('canvas'), ctx = canvas.getContext('2d'), stars = [], maxStars = 100; canvas.width = window.innerWidth; canvas.height = window.innerHeight; function Star() { this.x = Math.random() * canvas.width; this.y = Math.random() * canvas.height; this.radius = Math.random() * 2; this.vx = Math.random() * 0.5 - 0.25; this.vy = Math.random() * 0.5 - 0.25; } Star.prototype.move = function() { this.x += this.vx; this.y += this.vy; if (this.x < -this.radius || this.x > canvas.width + this.radius) { this.vx = -this.vx; } if (this.y < -this.radius || this.y > canvas.height + this.radius) { this.vy = -this.vy; } } Star.prototype.draw = function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2); ctx.fillStyle = '#fff'; ctx.fill(); } function drawStar() { for (var i = 0; i < maxStars; i++) { stars[i].move(); stars[i].draw(); } } function init() { for (var i = 0; i < maxStars; i++) { stars[i] = new Star(); } setInterval(function() { ctx.clearRect(0, 0, canvas.width, canvas.height); drawStar(); }, 30); } init(); canvas.addEventListener('mousemove', function(e) { for (var i = 0; i < maxStars; i++) { var dx = e.clientX - stars[i].x, dy = e.clientY - stars[i].y, distance = Math.sqrt(dx * dx + dy * dy), force = -distance / 1000; if (distance < 100) { stars[i].vx += dx * force; stars[i].vy += dy * force; } } }); ``` 这段代码实现了鼠标移动时,产生星星特效效果。具体实现方式是:首先定义一个Star对象,包含星星的位置、大小、速度等属性,并定义move和draw方法分别用于更新星星的位置和绘制星星。然后在init函数中,初始化100个星星,并使用setInterval定时更新画面。最后,使用canvas的mousemove事件,来控制星星的运动方向和速度。 这只是一个简单的示例代码,如果需要更加复杂的效果,可以根据实际需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值