HTML+CSS部分 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> .item { display: none; width: 300px; height: 400px; overflow: hidden; position: relative; } .item>img { width: 100%; height: auto; position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; } .item.active { display: block; } </style> </head> <body> <div> <button class="prev" >上一頁</button> <button class="btn">1</button> <button class="btn">2</button> <button class="btn">3</button> <button class="btn">4</button> <button class="next" >下一頁</button> </div> <div> <div class="item active"><img src="img/1.png" height="1191" width="820" /></div> <div class="item"><img src="img/2.png" height="1191" width="820" /></div> <div class="item"><img src="img/3.png" height="1191" width="820" /></div> <div class="item"><img src="img/4.png" height="1191" width="820" /></div> </div> </body> </html> js部分 <script type="text/javascript"> //封裝函數(shù)、圖片顯示的部分、傳入獲取到的div,和被點(diǎn)擊的序號(hào) function toggle(eles, active) { for(var i = eles.length; i--;) { eles[i].className = "item"; //先讓所有div隱藏 } eles[active].className = "item active";//再讓被點(diǎn)擊的序號(hào)對(duì)應(yīng)的div 顯示 } //獲取按鍵和div var aBtn = document.getElementsByClassName("btn"); var aIem = document.getElementsByClassName("item"); var prev = document.getElementsByClassName("prev"); var next = document.getElementsByClassName("next"); var nowPage = 0; //定義當(dāng)前頁,默認(rèn)值為0; for(var i = aBtn.length; i--;) { aBtn[i].tab = i; aBtn[i].onclick=function(){ toggle(aIem,this.tab); nowPage=this.tab; //被點(diǎn)擊后,保存當(dāng)前頁的序號(hào) } } //下一頁 next[0].onclick = function () { nowPage++; nowPage %= aBtn.length; toggle(aIem,nowPage); } //上一頁 prev[0].onclick=function(){ nowPage--; if(nowPage ==-1){ nowPage = aBtn.length-1; } toggle(aIem,nowPage); } </script> |
|