//面向对象,模拟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();
}