js面向对象(萤火虫效果)

 

//面向对象,模拟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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值