js 面向对象方式 动画效果:花瓣雨

效果图:
在这里插入图片描述
说明:效果图片中的花瓣是图片,
代码如下:


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            overflow: hidden;
        }
    </style>
</head>

<body>

    <script>
        // 从a到b的随机数
        function ranDom(a, b) {
            var r = parseInt(Math.random() * (b - a + 1)) + a;
            return r;
        }

        // 构造函数
        function Girl(top, right) {

            this.top = top;
            this.right = right;

            // 创建的方法
            this.init = function () {
                // 创建节点, 节点的样式, 节点拼接
                this.dom = document.createElement('div');
                this.dom.style.cssText = `width: 30px; height: 30px; position: absolute; background: url(img/hb${ranDom(1, 3)}.png) no-repeat; background-position: 0px -2px;`;
                this.dom.style.top = this.top + 'px';
                this.dom.style.right = this.right + 'px';
                document.body.appendChild(this.dom);
            }

            // 移动的方法
            this.move = function () {

                // 此处的this为新创建的Girl对象, 把this保存一份(self), 在进入定时器内部再使用
                // console.log(this);
                var self = this;

                // 声明变量: 位置, 图片编码(0-7)
                var top = 0;
                var index = 0;

                // 定时器, top实时改变, index++
                this.timer = setInterval(function () {
                    top += 5;
                    index++;

                    // 验收index
                    if (index > 7) index = 0;
                    // 验收top
                    if (top >= 1000) self.byebye();

                    // 位置改变
                    // console.log(this); // window
                    // console.log(this.dom); // undefined
                    // console.log(self); // Girl对象
                    // console.log(self.dom); // div
                    self.dom.style.top = top + 'px';
                    // 图片切换
                    // self.dom.style.backgroundPosition = -79 * index + 'px -216px';
                }, 50);
            }

            // 消失的方法
            this.byebye = function () {
                // 清除定时器
                clearInterval(this.timer);
                // 移除节点
                this.dom.remove();
            }

            this.init();
            this.move();
        }
        setInterval(() => {
            new Girl(0, ranDom(100, 900));
            new Girl(0, ranDom(100, 900));
            new Girl(0, ranDom(100, 900));
            new Girl(0, ranDom(100, 900));
        }, 1000)
        setInterval(() => {
            new Girl(0, ranDom(100, 900));
            new Girl(0, ranDom(100, 900));

        }, 1500)
        setInterval(() => {
            new Girl(0, ranDom(500, 1200));
            new Girl(0, ranDom(500, 1200));
        }, 2000)

    </script>
</body>

</html>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值