myIndex之前的哪個index值是怎么來的?
for (var i = 0; i < buttons.length; i++) {
buttons[i].onclick = function () {
if (animated) {
return;
}
if(this.className == 'on') {
return;
}
var myIndex = parseInt(this.getAttribute('index'));
var offset = -600 * (myIndex - index);
animate(offset);
index = myIndex;
showButton();
}
}
如圖 如果我要點擊到第五張圖那么第五張圖的index是5,如果我是從第一張圖切換到第五張圖的話,第一張圖的index值是1
-600*(5-1)=-2400 那就是第四張圖的位置
除非第一張圖的index值是0這樣結(jié)果才是第五張圖的位置
我是哪里沒有理解到?
2023-05-27
最前面的那個index是用在buttons[index - 1].className = 'on'; 這個語句里面,這里的index是自定義的變量,為了將圓點帶上"on"這個class名字;后面的myIndex是為了獲取當(dāng)前的index的數(shù)值,而再次定義的一個變量
2018-11-11
自問自答,animate()里面的newLeft = 跳轉(zhuǎn)之前的left值+offset 這里的offst=-2400 所以加上第一張圖的left值-600 就等于第五張圖的left值 因為也點用了函數(shù)animate(offset);感覺好繞啊 這算不算JS里最難的?