亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

JavaScript實現輪播圖效果

瀏覽:67日期:2023-10-09 15:06:51

要求:

鼠標經過輪播圖模塊,左右按鈕顯示,離開隱藏左右按鈕 點擊右側按鈕一次,圖片往左播放一張,以此類推,左側按鈕同理 圖片播放的同時,下面小圓圈模塊跟隨一起變化 點擊小圓圈,可以播放相應圖片 鼠標不經過輪播圖,輪播圖也會自動播放圖片 鼠標經過,輪播圖模塊,自動播放停止

代碼實現:

autoPlay.html(復制并保存為html文件,打開即可見效果):

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Document</title> <link rel='stylesheet' rel='external nofollow' > <script src='https://blog-static.cnblogs.com/files/jacklzx/animate.js'></script> <script src='https://blog-static.cnblogs.com/files/jacklzx/autoPlay.js'></script></head><body> <div class='focus'> <!-- 左側按鈕 --> <a href='javascript:;' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' class='arrow-l'>&lt;</a> <!-- 右側按鈕 --> <a href='javascript:;' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' class='arrow-r'>&gt;</a> <!-- 滾動區域 --> <ul> <li><a href='javascript:;' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' ><img src='https://s1.ax1x.com/2020/10/12/0W1wlt.jpg' alt=''></a> </li> <li><a href='javascript:;' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' ><img src='https://s1.ax1x.com/2020/10/12/0W3nHS.jpg' alt=''></a> </li> <li><a href='javascript:;' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' ><img src='https://s1.ax1x.com/2020/10/12/0Wtrmq.jpg' alt=''></a> </li> <li><a href='javascript:;' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' ><img src='https://s1.ax1x.com/2020/10/12/0W1NYd.jpg' alt=''></a> </li> </ul> <!-- 小圓圈 --> <ol class='circle'> </ol> </div></body></html>

autoPlay.css:

li { list-style: none;}a { text-decoration: none;}* { margin: 0; padding: 0;}body { background-color: #00e1ff;}.focus { overflow: hidden; position: relative; width: 721px; height: 455px; margin: 100px auto; box-shadow: 10px 10px 20px rgba(0, 0, 0, 0.6); border-radius: 40px;}.focus ul { position: absolute; top: 0; left: 0; width: 600%;}.focus ul li { float: left;}.arrow-l { display: none; position: absolute; top: 50%; left: -12px; margin-top: -20px; width: 40px; height: 40px; background: rgba(0, 0, 0, .3); text-align: center; line-height: 40px; color: #fff; font-size: 18px; border-radius: 0 50% 50% 0; z-index: 999;}.arrow-r { display: none; position: absolute; top: 50%; right: -12px; margin-top: -20px; width: 40px; height: 40px; background: rgba(0, 0, 0, .3); text-align: center; line-height: 40px; color: #fff; font-size: 18px; border-radius: 50% 0 0 50%; z-index: 999;}.circle { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%);}.circle li { float: left; width: 12px; height: 12px; border: 2px solid rgba(255, 255, 255, .5); margin: 0 4px; border-radius: 50%; cursor: pointer;}.current { background-color: #fff; box-shadow: 0 0 10px #fff;}

autoPlay.js:

window.addEventListener(’load’, function() { // 獲取元素 var arrow_l = document.querySelector(’.arrow-l’); var arrow_r = document.querySelector(’.arrow-r’); var focus = document.querySelector(’.focus’); var focusWidth = focus.offsetWidth; // 自定義動畫函數animate的參數,表示動畫間隔 var step = 5; // 鼠標經過focus 就顯示左右按鈕,停止計時器 focus.addEventListener(’mouseenter’, function() { arrow_l.style.display = ’block’; arrow_r.style.display = ’block’; clearInterval(timer); timer = null; // 清空計時器 }); // 鼠標離開focus 就隱藏左右按鈕,調用定時器 focus.addEventListener(’mouseleave’, function() { arrow_l.style.display = ’none’; arrow_r.style.display = ’none’; timer = setInterval(function() { // 手動調用點擊事件 arrow_r.click(); }, 2000); }); var ul = focus.querySelector(’ul’); var ol = focus.querySelector(’.circle’); for (var i = 0; i < ul.children.length; i++) { // 創建 li var li = document.createElement(’li’); // 設置自定義屬性,記錄小圓圈索引號 li.setAttribute(’index’, i); // li插入ol ol.appendChild(li); // 小圈圈排他思想 生成圈圈同時 直接綁定事件 li.addEventListener(’click’, function() { for (var i = 0; i < ol.children.length; i++) {ol.children[i].className = ’’; } this.className = ’current’; // 點擊小圈圈,移動圖片,移動的是ul // 點擊li,拿到當前的索引號 var index = this.getAttribute(’index’); // 當點擊了li之后,就要把index給num,實現同步 num = index; // 當點擊了li之后,就要把index給circle,實現同步 circle = index; animate(ul, -index * focusWidth, step); }); } // ol里第一個li的類名設置為current ol.children[0].className = ’current’; var num = 0; // circle控制小圓圈的播放 var circle = 0; // 克隆第一章圖片li,放到ul最后面 // 要在生成小圓圈之后克隆 var first = ul.children[0].cloneNode(true); ul.appendChild(first); // 點擊右側按鈕,圖片滾動 arrow_r.addEventListener(’click’, function() { // 如果到了最后一張圖片,ul要快速復原:left改為0 if (num == ul.children.length - 1) { ul.style.left = 0; num = 0; } num++; animate(ul, -num * focusWidth, step); // circle控制小圓圈的播放 circle++; circle = circle == ol.children.length ? 0 : circle; circleChange(); }); // 點擊左側按鈕,圖片滾動 arrow_l.addEventListener(’click’, function() { if (num == 0) { num = ul.children.length - 1; ul.style.left = -num * focusWidth + ’px’; } num--; animate(ul, -num * focusWidth, step); // circle控制小圓圈的播放 circle--; circle = circle < 0 ? (ol.children.length - 1) : circle; circleChange(); }); // 小圈圈改變樣式 function circleChange() { // 排他其他 for (var i = 0; i < ol.children.length; i++) { ol.children[i].className = ’’; } // 留下自己 ol.children[circle].className = ’current’; } // 自動播放功能 var timer = setInterval(function() { // 手動調用點擊事件 arrow_r.click(); }, 2000);});

animate.js:

function animate(obj, target, time, callback) { // 先清除以前的定時器,只保留當前的一個定時器執行 clearInterval(obj.timer); obj.timer = setInterval(function() { // 步長值寫到定時器的里面,并設置為整數 var step = (target - obj.offsetLeft) / 10; step = step > 0 ? Math.ceil(step) : Math.floor(step); if (obj.offsetLeft == target) { clearInterval(obj.timer); // 回調函數寫到定時器結束里面 callback && callback(); } obj.style.left = obj.offsetLeft + step + ’px’; }, time);}

JavaScript實現輪播圖效果

以上就是JavaScript實現輪播圖效果的詳細內容,更多關于JavaScript 輪播圖的資料請關注好吧啦網其它相關文章!

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 国产啪精品视频网给免丝袜 | 亚洲欧美综合网 | 91网站在线免费观看 | 视频偷拍一级视频在线观看 | 亚洲国产精品视频 | 亚洲精品免费在线观看 | 久久91精品国产91久久户 | 大黄网站在线观看 | 免费一级性片 | 深夜成人性视频免费看 | 日本高清色图 | 国产a三级三级三级 | 中国国产成人精品久久 | 中文字幕国产在线 | 中文精品爱久久久国产 | 在线免费看黄的网站 | 亚洲精品不卡在线 | 1区1区3区4区产品亚洲 | 插插插av | 97一级毛片全部免费播放 | 香蕉视频在线播放 | a免费网站| 欧美一级特黄aaa大片 | 亚洲1卡2卡三卡3卡4卡网站 | aa一级片| 免费视频观看在线www日本 | 精品亚洲成a人在线播放 | 黄色国产在线观看 | 尤物精品在线观看 | 黄色大全免费看 | 国产在线精品视频 | 狼色视频在线观看 | 又爽又黄又无遮挡的视频美女软件 | 另类国产精品一区二区 | 亚洲第一福利视频 | 在线国产毛片 | 6080yy午夜不卡一二三区 | 免费看午夜高清性色生活片 | 国产五月婷婷 | 在线看欧美日韩中文字幕 | 国产一级一片免费播放视频 |