您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)原生js怎么實(shí)現(xiàn)3D輪播圖的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
首先分析一下3D圖片輪播的功能需求:
和其它圖片輪播大體一致,無(wú)非就是點(diǎn)擊按鈕向左、右翻頁(yè),點(diǎn)擊下方提示位置小點(diǎn)切換到小點(diǎn)表示對(duì)的位置頁(yè)(我是真的不知道那個(gè)小點(diǎn)叫什么名字,大家將就著聽(tīng)吧)
上代碼:
基本代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>立體輪播圖</title> <style> .block{ position: relative; width: 900px; height: 370px; margin: 0 auto; overflow: hidden; } .imgae_div{ position: relative; width: 900px; height: 300px; } .imgae_div>div{ position: absolute; width: 400px; height: 200px; transition: all 1s linear; } .imgae_div img{ width: 400px; height: 200px; } .imgae_div>div:nth-child(1){ top: 150px; left: 250px; z-index: 6; } .imgae_div>div:nth-child(2){ top: 100px; left: 0px; z-index: 5; } .imgae_div>div:nth-child(3){ top: 50px; left: 0px; z-index: 4; } .imgae_div>div:nth-child(4){ top: 0px; left: 250px; z-index: 3; } .imgae_div>div:nth-child(5){ top: 50px; left: 500px; z-index: 4; } .imgae_div>div:nth-child(6){ top: 100px; left: 500px; z-index: 5; } .btn{ width: 900px; height: 40px; position: absolute; top: 155px; z-index: 999; overflow: hidden; } .btn>span:nth-child(1){ width: 30px; background-color: rgba(164, 150, 243, 0.336); display: block; float: left; text-align: center; line-height: 40px; margin-left: -30px; cursor: pointer; opacity: 0; transition: all 0.5s linear; } .btn>span:nth-child(2){ width: 30px; background-color: rgba(164, 150, 243, 0.336); display: block; float: right; text-align: center; line-height: 40px; margin-right: -30px; cursor: pointer; opacity: 0; transition: all 0.5s linear; } .marright{ margin-right: 0px !important; opacity: 1 !important; } .marleft{ margin-left: 0px !important; opacity: 1 !important; } .point{ width: 108px; height: 14px; position: absolute; bottom: 0; left: 396px; z-index: 10; } .point>div{ width: 14px; height: 14px; border-radius: 50%; background-color: white; float: left; margin: 0 2px; border: 2px solid black; box-sizing: border-box; } </style> </head> <body> <div class="block"> <div class="imgae_div"> <div><img src="./img/111.jpg" alt=""></div> <div><img src="./img/222.jpg" alt=""></div> <div><img src="./img/333.jpg" alt=""></div> <div><img src="./img/444.jpg" alt=""></div> <div><img src="./img/555.jpg" alt=""></div> <div><img src="./img/666.jpg" alt=""></div> </div> <div class="btn"> <span><</span> <span>></span> </div> <div class="point"> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </div> </div> <script src="./js/index.js"></script> </body> </html>
js代碼:
// 簡(jiǎn)單說(shuō)一下我是怎么想的:1.分步實(shí)現(xiàn),先實(shí)現(xiàn)圖片自己動(dòng),在加其它的功能 // 2.每實(shí)現(xiàn)一個(gè)功能要立馬去測(cè)bug,因?yàn)榉诺胶竺婢驮诫y找了。 // 3.輪播向左,向右是兩個(gè)互相聯(lián)系的方法,需要找到彼此的關(guān)系 var imgae_div = document.getElementsByClassName('imgae_div')[0]; var imgae_div_child = imgae_div.children; var btn=document.getElementsByClassName('btn')[0]; var block=document.getElementsByClassName('block')[0]; var new_point = document.getElementsByClassName("point")[0].children; new_point[0].style.backgroundColor = "#000000"; // 利用函數(shù)的封裝 ps:圖片輪播離不開(kāi)計(jì)時(shí)器,且個(gè)人覺(jué)得用setIntervar比較多 img_work(); function img_work() { time = setInterval(function () { img_workfirst('left', 0);//兩個(gè)參數(shù),判斷向左還是向右輪播,索引 }, 1500); } // console.log(point.child); function img_workfirst(left_right, cindex) { // 這里面首先說(shuō)一下css中寫(xiě)好的默認(rèn)層關(guān)系:從第1張到第6張為別為 6 5 4 3 4 5,和頁(yè)面的布局有關(guān) var firstpage = {//當(dāng)前頁(yè)的各種屬性 // getComputedStyle()獲取css屬性 left: window.getComputedStyle(imgae_div_child[cindex]).left, top: window.getComputedStyle(imgae_div_child[cindex]).top, zIndex: window.getComputedStyle(imgae_div_child[cindex]).zIndex, backcolor: window.getComputedStyle(new_point[cindex]).backgroundColor }; if (left_right == 'left') { // 向左輪播為默認(rèn)輪播 for (var i = 0; i < imgae_div_child.length; i++) { // for循環(huán)遍歷所有元素 if (i == imgae_div_child.length - 1) { // 當(dāng)前頁(yè)的下一張為 最后一張(位置都是動(dòng)態(tài)切換的) imgae_div_child[i].style.left = firstpage.left; imgae_div_child[i].style.top = firstpage.top; imgae_div_child[i].style.zIndex = firstpage.zIndex; new_point[i].style.backgroundColor = firstpage.backcolor; } else { // 其它頁(yè)對(duì)應(yīng)為它前面元素的屬性 imgae_div_child[i].style.left = window.getComputedStyle(imgae_div_child[i + 1]).left; imgae_div_child[i].style.top = window.getComputedStyle(imgae_div_child[i + 1]).top; imgae_div_child[i].style.zIndex = window.getComputedStyle(imgae_div_child[i + 1]).zIndex; new_point[i].style.backgroundColor = window.getComputedStyle(new_point[i + 1]).backgroundColor; } } } // 向右輪播,借助向左輪播分析 else { for (var i = imgae_div_child.length - 1; i >= 0; i--) { if (i == 0) { imgae_div_child[i].style.left = firstpage.left; imgae_div_child[i].style.top = firstpage.top; imgae_div_child[i].style.zIndex = firstpage.zIndex; new_point[i].style.backgroundColor = firstpage.backcolor; } else { imgae_div_child[i].style.left = window.getComputedStyle(imgae_div_child[i - 1]).left; imgae_div_child[i].style.top = window.getComputedStyle(imgae_div_child[i - 1]).top; imgae_div_child[i].style.zIndex = window.getComputedStyle(imgae_div_child[i - 1]).zIndex; new_point[i].style.backgroundColor = window.getComputedStyle(new_point[i - 1]).backgroundColor; } } } firstpage = null; // 將表示當(dāng)前頁(yè)的數(shù)據(jù)清空,防止bug(因?yàn)楫?dāng)前頁(yè)也是動(dòng)態(tài)變化的,需要?jiǎng)討B(tài)創(chuàng)建) } // console.log(new_point); // 消除一些bug window.onblur = function () {//窗口失焦時(shí),計(jì)時(shí)器停止 clearInterval(time); } window.onfocus = function () { img_work();//獲焦時(shí)開(kāi)啟計(jì)時(shí)器 } document.onselectstart = function () {//禁止用戶復(fù)制 return false; } block.οnmοuseenter=function(){//鼠標(biāo)進(jìn)入輪播圖時(shí),兩個(gè)按鈕滑動(dòng)出來(lái) clearInterval(time); btn.children[1].className='marright'; btn.children[0].className='marleft'; } block.οnmοuseleave=function(){//離開(kāi)時(shí)和平時(shí)隱藏 img_work(); btn.children[1].className=''; btn.children[0].className=''; for (var k = 0; k < imgae_div_child.length; k++) { imgae_div_child[k].style.transitionDuration = "0.5s"; } } // 對(duì)應(yīng)的左右按鈕的點(diǎn)擊事件 btn.children[0].οnclick=function(event){ if(event.detail==1){//用于控制鼠標(biāo)的連擊,但是效果對(duì)于故意測(cè)試來(lái)說(shuō)還是有所缺陷 下同 img_workfirst('left',0); } } btn.children[1].οnclick=function(event){ if(event.detail==1){ img_workfirst('right',imgae_div_child.length-1); } } // point的事件 for(var i=0;i<new_point.length;i++){ new_point[i].index=i; new_point[i].οnclick=function(){ for(var k=0;k<imgae_div_child.length;k++){ imgae_div_child[k].style.transitionDuration='0.1s';//動(dòng)畫(huà)完成 } var oldindex=0; for(var k=0;k<new_point.length;k++){ if(new_point[k].style.backgroundColor== 'rgb(0, 0, 0)'){//格式必須統(tǒng)一 oldindex=new_point[k].index; } } var num =0;//判斷計(jì)算轉(zhuǎn)動(dòng)次數(shù) if(this.index>oldindex){//所需頁(yè)在當(dāng)前頁(yè)的左(左右相對(duì)于下方點(diǎn)來(lái)說(shuō)) num=this.index-oldindex; var timego=setInterval(function(){ num--; if(num<0){ clearInterval(timego); } else{ img_workfirst('right',5)//因?yàn)榉较蜃兞耍韵乱豁?yè)就為當(dāng)前頁(yè)的上一頁(yè),也就是cindex為5 } },100);//動(dòng)畫(huà)時(shí)間縮短,優(yōu)化體驗(yàn) } else{//所需頁(yè)在當(dāng)前頁(yè)的左(左右相對(duì)于下方點(diǎn)來(lái)說(shuō)) num=Math.abs(this.index-oldindex); var timego=setInterval(function(){ num--; if(num<0){ clearInterval(timego); } else{ img_workfirst('left',0) } },100); } } }
關(guān)于出現(xiàn)的bug的一些總結(jié):
1、注意左右的區(qū)分與聯(lián)系
2、注意連續(xù)點(diǎn)擊的bug
3、注意切換窗口,切換頁(yè)面,最小化等這些切換的bug
4、注意代碼格式,在js中寫(xiě)css樣式時(shí),要注意格式
感謝各位的閱讀!關(guān)于“原生js怎么實(shí)現(xiàn)3D輪播圖”這篇文章就分享到這里了,希望以上內(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)容。