0
我正在練習一些JavaScript,我想弄清楚如何將屏幕左側的div移動到右側。我在一個單獨的文件中做了這個測試,並首先測試它,但是,如果我創建一個類並將其中的函數和變量停止工作,並且Firebug不會返回任何錯誤。誰能幫我? 這裏是我想出代碼:寫入類後不工作
function crear_div(){
this.pos_x = 10;
this.pos_y = 10;
this.x_min = 10;
this.x_max = 500;
this.y_min = 10;
this.y_max = 500;
this.incremento = 10;
this.estado = 0;
this.id_elemento = "pelota";
this.f0 = f0;
this.f2 = f2;
}
function f0(){
this.pos_x = this.pos_x + this.incremento;
document.getElementById(this.id_elemento).style.left = this.pos_x + "px";
if (this.pos_x >= this.x_max){
this.estado = 1;
}
}
function f2(){
this.pos_x = this.pos_x - this.incremento;
document.getElementById(id_elemento).style.left = this.pos_x + "px";
if (this.pos_x <= this.x_min){
this.estado = 0;
}
}
function desplazar(){
switch(this.estado){
case 0:
this.f0();
break;
case 1:
this.f2();
break;
}
}
window.setInterval(function() {this.desplazar();}, 250);
'this'沒有在'setInterval的設置'功能。 – Barmar 2015-03-31 02:38:53
你永遠不會調用'crear_div'。 – Barmar 2015-03-31 02:39:37