您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)JS怎么實(shí)現(xiàn)橫向輪播圖的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
描述:
輪播圖,初級(jí),橫向buton或者底部數(shù)字控制輪播,可以實(shí)現(xiàn)自動(dòng)輪播(注釋了,使用的話將其注釋消掉),核心知識(shí)是數(shù)據(jù)驅(qū)動(dòng)圖像的位移達(dá)到效果。
效果:
代碼:
js文件:
/* * 工廠模式 * */ var Method=(function () { return { loadImage:function (arr,callback) { var img=new Image(); img.arr=arr; img.list=[]; img.num=0; img.callback=callback; // 如果DOM對(duì)象下的事件偵聽(tīng)沒(méi)有被刪除掉,將會(huì)常駐堆中 // 一旦觸發(fā)了這個(gè)事件需要的條件,就會(huì)繼續(xù)執(zhí)行事件函數(shù) img.addEventListener("load",this.loadHandler); img.self=this; img.src=arr[img.num]; }, loadHandler:function (e) { this.list.push(this.cloneNode(false)); this.num++; if(this.num>this.arr.length-1){ this.removeEventListener("load",this.self.loadHandler); this.callback(this.list); return; } this.src=this.arr[this.num]; }, $c:function (type,parent,style) { var elem=document.createElement(type); if(parent) parent.appendChild(elem); for(var key in style){ elem.style[key]=style[key]; } return elem; } } })();
html文件:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; padding: 0; } #carousel,#imgCon img{ width: 1200px; height: 400px; } #carousel { position: relative; margin: auto; overflow: hidden; } #imgCon{ width: 6000px; height: 400px; position: absolute; left: 0; font-size: 0; transition: all 1s; } #leftBn,#rightBn { position: absolute; top:170px; } #leftBn{ left: 20px; } #rightBn { right: 20px; } ul{ position: absolute; bottom: 20px; list-style: none; margin: auto; left: 45%; } li { width: 20px; height: 20px; border: 1px solid red; border-radius: 10px; float: left; text-align: center; color: white; cursor: default; line-height:20px; font-size: 12px; margin-left: 8px; } </style> </head> <body> <div id="carousel"> <div id="imgCon"> <img src="img/a.jpeg"> <img src="img/b.jpeg"> <img src="img/c.jpeg"> <img src="img/d.jpeg"> <img src="img/e.jpeg"> </div> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> <img src="img/left.png" id="leftBn"> <img src="img/right.png" id="rightBn"> </div> <script> /* * * 數(shù)據(jù)驅(qū)動(dòng)顯示 * * */ var imgCon,leftBn,rightBn,lis,ul,prevLi; var position=0;//圖像的標(biāo)記 第一張0 第二張1... init(); function init() { imgCon=document.getElementById("imgCon");//圖 leftBn=document.getElementById("leftBn");//左按鈕 rightBn=document.getElementById("rightBn");//右按鈕 lis=document.getElementsByTagName("li");//下方數(shù)字右按鈕 ul=document.getElementsByTagName("ul")[0]; leftBn.addEventListener("click",clickHandler); rightBn.addEventListener("click",clickHandler); for(var i=0;i<lis.length;i++){//為每隔小Li 也就是底部數(shù)字賦值 lis[i].num=i; lis[i].addEventListener("click",liClickHandler); } changeLi(); } // setInterval(autoImg,3000);可以實(shí)現(xiàn)自動(dòng) function autoImg() {//自動(dòng)輪播 position++; if(position>4) position=0; changeImg(); } function clickHandler(e) { e= e || window.event; if(this===leftBn){ position--; if(position<0) position=4; }else if(this===rightBn){ position++; if(position>4) position=0; } changeImg(); } function liClickHandler(e) { e= e || window.event; position=this.num; changeImg(); } function changeImg() {//圖片的轉(zhuǎn)換效果 唯一 imgCon.style.left=-position*1200+"px";//一次一張圖片的位移 changeLi(); } function changeLi() {//底部數(shù)字的轉(zhuǎn)換效果 if(prevLi){ prevLi.style.backgroundColor="rgba(255,0,0,0)"; } prevLi=lis[position]; prevLi.style.backgroundColor="rgba(255,0,0,0.5)"; } </script> </body> </html>
感謝各位的閱讀!關(guān)于“JS怎么實(shí)現(xiàn)橫向輪播圖”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。