JS烟花效果

这篇博客展示了一个使用HTML5和JavaScript实现的烟花效果。通过点击一个容器,会在鼠标点击的位置生成大烟花,并随即分散成多个小烟花,每个小烟花随机移动并消失,营造出绚丽的视觉效果。代码中包含大烟花和小烟花的生成、运动函数以及颜色和位置的随机生成方法。
摘要由CSDN通过智能技术生成

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <title>Document</title>

</head>

<style>

  #container {

    width: 80%;

    height: 600px;

    border: 1px red solid;

    position: relative;

    margin: 20px auto;

    cursor: pointer;

    background: black;

  }

  .fire {

    background: red;

    position: absolute;

    /* 设置bottom时,top获取为最大值,减去鼠标点击位置 */

    bottom: 0px;

    width: 6px;

    height: 6px;

  }

  .small-fire {

    width: 10px;

    height: 10px;

    position: absolute;

    border-radius: 50%;

  }

</style>

<body>

  <div id="container">

  </div>

  <script>

    // 1 获取节点,绑定点击事件

    let conObj = document.getElementById('container');

    conObj.onclick = function (eve) {

      // 2 获取点击位置

      let pos = {

        cx: eve.offsetX,

        cy: eve.offsetY

      }

      new Fire(conObj, pos);

    }

    /*******实现大烟花*******/

    function Fire(conObj, pos) {

      // 1 把属性设置为变量

      this.pos = pos;

      this.conObj = conObj;

      // 2  生成div,且追加class

      let bigDiv = document.createElement('div');

      bigDiv.classList.add('fire');

      // console.log(bigDiv);

      // 3 设置大烟花的样式

      bigDiv.style.left = pos.cx + 'px';

      bigDiv.style.background = this.getColor();

      // console.log(bigDiv);

      // 4 追加到con中

      this.conObj.appendChild(bigDiv);

      // let that = this;

      // 5 调用运动函数,从底部运动到鼠标点击的位置

      this.move(bigDiv, { top: this.pos.cy }, () => {

        // console.log(that);

        // console.log(this);

        // 6 清除大烟花,调用小烟花

        bigDiv.remove();

        this.smallFire()

      })

    }

    /******小烟花*************/

    Fire.prototype.smallFire = function () {

      // 1 随机生成烟花个数

      let num = this.rand(20, 50);

      // 2 循环生成div,且添加class

      for (let i = 0; i < num; i++) {

        let sFire = document.createElement('div');

        sFire.className = 'small-fire';

        // 3 设置小烟花的位置为鼠标点击的位置

        Object.assign(sFire.style, {

          top: this.pos.cy + 'px',

          left: this.pos.cx + 'px',

          background: this.getColor()

        })

        // 4  追加到页面中

        this.conObj.appendChild(sFire)

        // 5 随机top,left值,让烟花动起来

        let top = this.rand(0, this.conObj.offsetHeight - sFire.offsetHeight);

        let left = this.rand(0, this.conObj.offsetWidth - sFire.offsetWidth);

        // sFire.style.background

        // 6调用运动函数

        this.move(sFire, { top, left }, () => {

          // console.log(11111);

          sFire.remove();

        })

      }

    }

    /********随机数方法*******/

    Fire.prototype.rand = (min, max) => {

      return Math.round(Math.random() * (max - min) + min);

    }

    /********随机颜色*****/

    Fire.prototype.getColor = function () {

      // console.log(this);

      let c = '#';

      for (let i = 0; i < 6; i++) {

        c += this.rand(0, 15).toString(16)

      }

      return c;

    }

    /*****运动方法****/

    // let times = '';

    // 函数之间不会互相干扰....

    Fire.prototype.move = function (ele, target, cb) {

      // clearInterval(times);

      var times = setInterval(function () {

        // console.log(this);

        var onOff = true;

        // 遍历运动的方向和目标

        for (let attr in target) {

          // attr 表示运动的属性

          // console.log(attr);

          // 获取元素属性的实时值

          let tmpVal = parseInt(this.getPos(ele, attr))

          // 计算speed

          // console.log(tmpVal, attr);

          let speed = (target[attr] - tmpVal) / 10;

          // 取整

          speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);

          // 停止计时器,当一个属性运动到位置,设置开关状态

          if (tmpVal == target[attr]) onOff = true;

          // 让元素动起来

          ele.style[attr] = tmpVal + speed + 'px';

        }

        // 判断开关状态,清除定时器

        for (var moveAttr in target) {

          // 如果不相等,就说明有属性没有运动到位置,定时器不能停止

          if (target[moveAttr] !== parseInt(this.getPos(ele, moveAttr))) {

            onOff = false;

            break;

          }

        }

        if (onOff) {

          clearInterval(times);

          cb && cb();

        }

        // console.log(1111);

      }.bind(this), 30)

    }

    // 获取元素的实时位置的函数

    Fire.prototype.getPos = function (obj, attr) {

      if (obj.currentStyle) {   // 获取css的样式

        return obj.currentStyle[attr];

      } else {

        return getComputedStyle(obj)[attr]

      }

    }

  </script>

</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清风如水668

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值