亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

((speed < 0 && parseInt(list.style.left) > newLeft) || (speed > 0 && parseInt(list.style.left) < newLeft))這是什么意思

window.onload = function(){


var container = document.getElementById('container');

var list = document.getElementById('list');

var buttons = document.getElementById('buttons').getElementsByTagName('span');

var prev = document.getElementById('prev');

var next = document.getElementById('next');

var index = 1;


function showButton(){

for (var i = 0; i < buttons.length; i++) {

if(buttons[i].className == 'on'){

buttons[i].className = '';

break;

}

}

buttons[index - 1].className = 'on';

}


function animate(offset){

var newLeft =? parseInt(list.style.left) + offset;

var time = 300;

var interval = 10;

var speed = offset/(time/interval);


function go(){

if((speed < 0 && parseInt(list.style.left) > newLeft) || (speed > 0 && parseInt(list.style.left) < newLeft)){

list.style.left = parseInt(list.style.left) + speed + 'px';

setTimeout(go, interval);

}else {

list.style.left = newLeft + 'px';


if (newLeft < -3000) {

list.style.left = -600 +'px';

}else if(newLeft > -600){

list.style.left = -3000 +'px';

}

}


}


go();

}




next.onclick = function(){

if(index == 5){

index = 1;

}else {

index += 1;

}

showButton();

animate(-600);

}


prev.onclick = function(){

if(index == 1){

index = 5;

}else {

index -= 1;

}

showButton();

animate(600);

}


for(var i = 0; i < buttons.length ; i++){

buttons[i].onclick = function(){

if(this.className == 'on'){

return;

}

var myIndex = parseInt(this.getAttribute('index'));

var offset = -600 * (myIndex - index);


animate(offset);

index = myIndex;

showButton();

}

}


正在回答

2 回答

我是看了好幾遍視頻才勉強搞明白什么意思,這么給你解釋吧:

http://img1.sycdn.imooc.com//5df73a820001490014790512.jpg

看上面這個數軸:

首先speed為負的時候,為向左進行位移,這個應該不會有問題吧。然后你想,向左進行位移是想達到什么目的呢?肯定是想讓位移后的位置跑到位移前位置的左邊,也就是說在進行位移之前,需要滿足位移前的位置在數軸上要在位移后的位置的右邊,即parseInt(list.style.left)>newLeft,這么說應該就可以懂了吧。

如果懂了,那么想右位移的情況應該也就不難理解了

1 回復 有任何疑惑可以回復我~

就是向左運動時 算出來的speed是負值, 并且目前的left值比目標值小或者大 (其實就是沒有到大目的位置的時候),然后每10毫秒給當前left值加一個speed 直至當前left值和目標值相等,跳出if 加speed的部分 ;一次動畫執行結束。?

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

((speed < 0 && parseInt(list.style.left) > newLeft) || (speed > 0 && parseInt(list.style.left) < newLeft))這是什么意思

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號