昨天朋友問(wèn)我怎么用js實(shí)現(xiàn)分頁(yè)瀏覽橫向圖片功能,其實(shí)實(shí)現(xiàn)代碼很簡(jiǎn)單的,下面小編給大家?guī)?lái)了具體實(shí)現(xiàn)代碼,代碼如下所示:
common.js
function $(id) { return document.getElementById(id); } function $_tag(tag) { return document.getElementsByTagName(tag); }
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> a { cursor: pointer; } .div1 { width: 410px; /* 可以去掉 */ height: 100px; overflow-x: hidden; white-space: nowrap; } .div2 { display: none; } </style> </head> <body> <div class="div1" id="div1"> </div> <a onclick="pre()">上一頁(yè)</a> <a onclick="next()">下一頁(yè)</a> <div class="div2"> <img src="images/1.jpg" alt=""> <img src="images/2.jpg" alt=""> <img src="images/3.jpg" alt=""> <img src="images/4.jpg" alt=""> <img src="images/5.jpg" alt=""> <img src="images/6.jpg" alt=""> <img src="images/7.jpg" alt=""> </div> <script src="js/common.js"></script> <script> //搞一個(gè)拼img標(biāo)簽的函數(shù) 傳參數(shù) for循環(huán) //擴(kuò)展、穩(wěn)定、兼容、好用(簡(jiǎn)單)... //<img src="images/7.jpg" alt=""> var count = 4; // 每頁(yè)的圖片數(shù)目 var currentpage = 1; //當(dāng)前頁(yè)數(shù) var imgs = $_tag("img"); //console.log(imgs); 可以深入了解下 console.log("圖片總數(shù):" + imgs.length); var totalpage = Math.ceil(imgs.length/count); console.log("總頁(yè)數(shù):" + totalpage); function pre() { if(currentpage == 1){ console.log("已經(jīng)是第一頁(yè)了!"); return; }else if(currentpage > 1){ var tmp = (currentpage - 2) * count + 1; var str = ""; str += "<img src='" + "images/" + tmp + ".jpg'>"; str += "<img src='" + "images/" + (tmp+1) + ".jpg'>"; str += "<img src='" + "images/" + (tmp+2) + ".jpg'>"; str += "<img src='" + "images/" + (tmp+3) + ".jpg'>"; currentpage -= 1; console.log(str); $("div1").innerHTML = str; } } function next() { if(currentpage == totalpage){ console.log("已經(jīng)是最后一頁(yè)了!"); }else if(currentpage == (totalpage - 1)){ var sur = imgs.length % count; var tmp1 = currentpage * count + 1; var str = ""; console.log("剩余數(shù):" + sur); for(var i=0;i<sur;i++){ console.log("第幾張:" + (tmp1+i)); str += "<img src=\"" + "images/" + (tmp1+i) + ".jpg\">"; } currentpage += 1; console.log(str); $("div1").innerHTML = str; }else if(currentpage > 0){ var tmp = currentpage * count + 1; console.log("tmp:" + tmp); var str = ""; str += "<img src='" + "images/" + tmp + ".jpg'>"; str += "<img src='" + "images/" + (tmp+1) + ".jpg'>"; str += "<img src='" + "images/" + (tmp+2) + ".jpg'>"; str += "<img src='" + "images/" + (tmp+3) + ".jpg'>"; currentpage += 1; $("div1").innerHTML = str; } } function firstpage() { var str = ""; str += "<img src='" + "images/" + 1 + ".jpg'>"; str += "<img src='" + "images/" + 2 + ".jpg'>"; str += "<img src='" + "images/" + 3 + ".jpg'>"; str += "<img src='" + "images/" + 4 + ".jpg'>"; console.log(str); $("div1").innerHTML = str; } window.onload = function() { firstpage(); } </script> </body> </html>
總結(jié)
以上所述是小編給大家介紹的JS實(shí)現(xiàn)分頁(yè)瀏覽橫向圖片(類(lèi)輪播)實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號(hào)-2
違法及侵權(quán)請(qǐng)聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市萬(wàn)商天勤律師事務(wù)所王興未律師提供法律服務(wù)