((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();
}
}
2019-12-16
我是看了好幾遍視頻才勉強搞明白什么意思,這么給你解釋吧:
看上面這個數軸:
首先speed為負的時候,為向左進行位移,這個應該不會有問題吧。然后你想,向左進行位移是想達到什么目的呢?肯定是想讓位移后的位置跑到位移前位置的左邊,也就是說在進行位移之前,需要滿足位移前的位置在數軸上要在位移后的位置的右邊,即parseInt(list.style.left)>newLeft,這么說應該就可以懂了吧。
如果懂了,那么想右位移的情況應該也就不難理解了
2019-07-01
就是向左運動時 算出來的speed是負值, 并且目前的left值比目標值小或者大 (其實就是沒有到大目的位置的時候),然后每10毫秒給當前left值加一個speed 直至當前left值和目標值相等,跳出if 加speed的部分 ;一次動畫執行結束。?