javascript利用餘數寫網頁loop輪播器slider當前index

/*html*/
<div class="arrow arrow_left" onclick="changeindex(-1);"></div>
<div class="arrow arrow_right" onclick="changeindex(1)"></div>


/*javascript*/
var this_index = 0; //設定一個初始0的整數

function changeindex(change){
    this_index = (this_index+change+7)%7;

    console.log(this_index)

}

邏輯是(例如):
30%30=>0
31%30=>1
32%30=>2
33%30=>3

7的部分要看你的slider的lenght(長度)而定,

如此一來按左右兩箭頭就能判斷slider當前的index囉!進而寫出輪播器效果

留言

這個網誌中的熱門文章

PHP讀資料庫的資料,保留換行及空格

CSS animation 簡寫&好用的工具

使陣列裡的數值隨機重新排序