js实现飘飘飞舞的虫子

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    html, body {
        width: 100%;
        height: 100%;
        background: url(images/w.jpg) no-repeat center;
        background-size: cover;
        overflow: hidden;
        position: relative;
    }

    img {
        position: absolute;
        width: 38px;
        height: 38px;
        /*left:100px;
        top:100px;*/
    }
</style>
<body>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script>
    function createFirefly() {
        var firefly = new Object();
        firefly.element = $("<img src = 'images/6.png'/>");
        firefly.show = function () {
            this.element.css({
                "top": this.showPoint().pointY,
                "left": this.showPoint().pointX
            });
            $("body").append(this.element);
        };
        firefly.showPoint = function () {
            var pointY = Math.abs(Math.random() * window.innerHeight - 18) + "px",
                pointX = Math.abs(Math.random() * window.innerWidth - 18) + "px";
            return {
                "pointY": pointY,
                "pointX": pointX
            }
        };
        firefly.flyTime = function () {
            var flyTime = Math.floor((Math.random() * 3 + 2) * 1000);
            return flyTime;
        };
        firefly.run = function () {
            var self = this;
            console.log(self);
            
            var animateMove=  self.element.animate({
                left: self.showPoint().pointX,
                top: self.showPoint().pointY,
            }, self.flyTime(),function () {
                self.run();
            });
        };
        return firefly
    }

    for (var i = 0; i < 20; i++) {
        var fire1 = createFirefly();
        fire1.show();
        fire1.run();
    }

    function show(n) {
        var arr = [];
        console.log('1');
        return (function fn() {
            arr.unshift(n);
            n--;
            console.log(arr);
            if (n != 0) {
                fn();
            }
            return arr;

        })()
    }
    console.log(show(5));
// /*********************************************************************************************************/
    //
    // //面向对象,模拟java
    // function Firefly() {
    //     this.element = $("<img src = 'img/1.jpg'/>");
    //     this.setPointX = function (value) {
    //         this.pointX = value;
    //     };
    //     this.getPointX = function () {
    //         return this.pointX;
    //     };
    //     this.setPointY = function (value) {
    //         this.pointY = value;
    //     };
    //     this.getPointY = function () {
    //         return this.pointY;
    //     };
    //     this.setRunTime = function (value) {
    //         this.runtime = value;
    //     };
    //     this.getRunTime = function () {
    //         return this.runtime;
    //     }
    // }
    //
    // Firefly.prototype.show = function () {
    //     this.setPointX(Math.abs(Math.random() * window.innerWidth - 18) + "px");
    //     this.setPointY(Math.abs(Math.random() * window.innerHeight - 18) + "px");
    //     this.element.css({
    //         left: this.getPointX(),
    //         top: this.getPointY()
    //     });
    //     $("body").append(this.element);
    // };
    // Firefly.prototype.run = function () {
    //     //alert(this.flyTime())
    //     var self = this;
    //     this.element.animate({
    //         left: this.getPointX(this.setPointX(Math.abs(Math.random() * window.innerWidth - 18) + "px")),
    //         top: this.getPointY(this.setPointY(Math.abs(Math.random() * window.innerHeight - 18) + "px"))
    //     }, this.getRunTime(this.setRunTime(Math.floor((Math.random() * 3 + 2) * 1000))), function () {
    //         self.element.animate({
    //             left: self.getPointX(self.setPointX(Math.abs(Math.random() * window.innerWidth - 18) + "px")),
    //             top: self.getPointY(self.setPointY(Math.abs(Math.random() * window.innerHeight - 18) + "px"))
    //         }, self.getRunTime(self.setRunTime(Math.floor((Math.random() * 3 + 2) * 1000))), arguments.callee);
    //     });
    // };
    // for (var i = 0; i < 20; i++) {
    //     var fire1 = new Firefly();
    //     fire1.show();
    //     fire1.run();
    // }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值