原生js實(shí)現(xiàn)無(wú)縫輪播圖效果
更新時(shí)間:2021年01月28日 11:02:46 作者:搬磚大法
這篇文章主要為大家詳細(xì)介紹了原生js實(shí)現(xiàn)無(wú)縫輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
原生js實(shí)現(xiàn)輪播圖效果(無(wú)縫滾動(dòng)) ,供大家參考,具體內(nèi)容如下
效果圖:
代碼:
<!DOCTYPE html> <html lang="en"> <!-- day07 7-10-14 --> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./images1/20.animate.js"></script> <style> * { margin: 0; padding: 0; } li { list-style: none; } .focus { /*overflow: hidden;*/ position: absolute; top: 100px; left: 200px; width: 721px; height: 455px; background-color: brown; } .prev, .next { display: none; position: absolute; top: 50%; margin-top: -15px; width: 20px; height: 30px; background-color: rgba(0, 0, 0, .3); text-decoration: none; color: #fff; line-height: 30px; text-align: center; font-size: 16px; z-index: 2; } .focus ul { /* 引入動(dòng)畫js文件要求必須有定位 */ position: absolute; width: 600%; } .focus ul li { float: left; } .prev { left: 0; border-top-right-radius: 15px; border-bottom-right-radius: 15px; } .next { right: 0; border-top-left-radius: 15px; border-bottom-left-radius: 15px; } .promo-nav { position: absolute; bottom: 10px; left: 60px; width: 200px; height: 18px; border-radius: 9px; } .promo-nav li { float: left; width: 10px; height: 10px; background-color: #fff; margin: 2px; border-radius: 50%; } .promo-nav .current { background-color: orange; } .focus ul li a img { width: 721px; height: 455px; } </style> </head> <body> <div class="focus"> <ul> <li> <a href="#" ><img src="images1/focu01.jpg" alt=""></a> </li> <li> <a href="#" ><img src="images1/focu02.jpg" alt=""></a> </li> <li> <a href="#" ><img src="images1/focu03.jpg" alt=""></a> </li> <li> <a href="#" ><img src="images1/focu04.jpg" alt=""></a> </li> </ul> <!-- 左側(cè)按鈕 --> <a href="javascript:;" class="prev"><</a> <!-- 右側(cè)按鈕 --> <a href="javascript:;" class="next">></a> <ol class="promo-nav"> </ol> </div> <script> window.addEventListener('load', function() { var focus = document.querySelector('.focus'); var prev = document.querySelector('.prev'); var next = document.querySelector('.next'); var focusWidth = focus.offsetWidth; //鼠標(biāo)經(jīng)過 focus.addEventListener('mouseenter', function() { prev.style.display = 'block'; next.style.display = 'block'; clearInterval(timer); timer = null; //清除定時(shí)器變量 }) //鼠標(biāo)離開 focus.addEventListener('mouseleave', function() { prev.style.display = 'none'; next.style.display = 'none'; timer = setInterval(function() { next.click(); }, 2000) }) //3.動(dòng)態(tài)生成小圓圈 有幾張圖片 就生成幾個(gè)小圓圈 var ul = focus.querySelector('ul'); var ol = focus.querySelector('.promo-nav'); // console.log(ul.children.length); 4 for (var i = 0; i < ul.children.length; i++) { //創(chuàng)建一個(gè)li var li = document.createElement('li'); //記錄當(dāng)前小圓圈的索引號(hào) 通過自定義屬性來(lái)做 li.setAttribute('index', i); //插入到ol后面 ol.appendChild(li); //4.鼠標(biāo)點(diǎn)擊小圓圈小圓圈變色(給小圓圈添加current類其余小圓圈移除這個(gè)類)(排他思想) //在生成小圓圈的同時(shí)直接綁定點(diǎn)擊事件 li.addEventListener('click', function() { for (var i = 0; i < ol.children.length; i++) { ol.children[i].className = ''; } this.className = 'current'; //5.點(diǎn)擊小圓點(diǎn) 移動(dòng)圖片 移動(dòng)的是ul //ul移動(dòng)的距離 小圓圈的索引號(hào)乘以圖片寬度 注意是負(fù)值 //當(dāng)我們點(diǎn)擊了某個(gè)小li就得到了當(dāng)前小li的索引號(hào) var index = this.getAttribute('index'); //當(dāng)我們點(diǎn)擊了某個(gè)li就把li的索引號(hào)給num num = index; //當(dāng)我們點(diǎn)擊了某個(gè)li就把li的索引號(hào)給index circle = index; console.log(index); animate(ul, -index * focusWidth, ); }) } //把ol里面的第一個(gè)li北京顏色設(shè)置成白色 ol.children[0].className = 'current'; //6. 克隆第一張li放到ul后面 var first = ul.children[0].cloneNode(true); ul.appendChild(first); //7.點(diǎn)擊右側(cè)按鈕圖片滾動(dòng)一張 var num = 0; var circle = 0; var flag = true; //右側(cè)按鈕 next.addEventListener('click', function() { if (flag) { flag = false; //先關(guān)閉節(jié)流閥 //5.如果走到最后一張復(fù)制圖片此時(shí)ul快速?gòu)?fù)原 left改為0(無(wú)縫滾動(dòng)) if (num == ul.children.length - 1) { ul.style.left = 0; num = 0; } num++; animate(ul, -num * focusWidth, function() { flag = true; }); //8.點(diǎn)擊右側(cè)按鈕小圓圈跟隨一起變化 聲明一個(gè)變量控制小圓圈變化 circle++; //如果 circle等于4說明做到最后克隆的這張圖片了 我們就復(fù)原 if (circle == ol.children.length) { circle = 0; } // //清除其余小圓圈類名 // for (var i = 0; i < ol.children.length; i++) { // ol.children[i].className = ''; // } // //留下當(dāng)前小圓圈current類名 // ol.children[circle].className = 'current'; circleChange(); } }) //左側(cè)按鈕 prev.addEventListener('click', function() { if (flag) { flag = false; //5.如果走到最后一張復(fù)制圖片此時(shí)ul快速?gòu)?fù)原 left改為0(無(wú)縫滾動(dòng)) if (num == 0) { num = ul.children.length - 1; ul.style.left = -num * focusWidth + 'px'; } num--; animate(ul, -num * focusWidth, function() { flag = true; }); //8.點(diǎn)擊右側(cè)按鈕小圓圈跟隨一起變化 聲明一個(gè)變量控制小圓圈變化 circle--; //如果 circle小于0小圓圈要改為第四個(gè)小圓圈 if (circle < 0) { circle = ol.children.length - 1; } // 清除其余小圓圈類名 // for (var i = 0; i < ol.children.length; i++) { // ol.children[i].className = ''; // } // 留下當(dāng)前小圓圈current類名 // ol.children[circle].className = 'current'; circleChange(); } }) function circleChange() { //清除其余小圓圈類名 for (var i = 0; i < ol.children.length; i++) { ol.children[i].className = ''; } //留下當(dāng)前小圓圈current類名 ol.children[circle].className = 'current'; } //10.自動(dòng)播放輪播圖 var timer = setInterval(function() { next.click(); }, 2000) }) </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js實(shí)現(xiàn)圖片輪播效果學(xué)習(xí)筆記
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)圖片輪播效果的學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07判斷是否安裝flash player及當(dāng)前版本的JS代碼
本文為大家講述下如何使用jsJS判斷是否安裝flash player及版本,下面的處理代碼或許對(duì)大家有所幫助,感興趣的朋友可以參考下,希望對(duì)大家有所幫助2013-08-08前端使用crypto.js進(jìn)行加密的函數(shù)代碼
最近在使用Cookies加密保存數(shù)據(jù)的時(shí)候,接觸到crypto,使用還算簡(jiǎn)單,在這里記錄一下2020-08-08JS動(dòng)態(tài)給對(duì)象添加事件的簡(jiǎn)單方法
下面小編就為大家?guī)?lái)一篇JS動(dòng)態(tài)給對(duì)象添加事件的簡(jiǎn)單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-07-07基于Web標(biāo)準(zhǔn)的UI組件 — 樹狀菜單(2)
基于Web標(biāo)準(zhǔn)的UI組件 — 樹狀菜單(2)...2006-09-09