色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

基于JavaScript實(shí)現(xiàn)簡(jiǎn)單的輪播圖

瀏覽:3日期:2023-06-07 17:38:35

本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)簡(jiǎn)單的輪播圖的具體代碼,供大家參考,具體內(nèi)容如下

js輪播圖實(shí)現(xiàn)思路

1、先獲取元素 盒子 左右按鈕2、鼠標(biāo)經(jīng)過(guò) 顯示/隱藏 左右側(cè)按鈕3、動(dòng)態(tài)生成小圓圈、 添加自定義屬性4、小圓圈點(diǎn)擊事件 添加current類(lèi)名5、添加動(dòng)畫(huà)事件 animate 等于 -索引號(hào)*focusWidth6、克隆第一張圖片到最后面7、添加右側(cè)/左側(cè)按鈕點(diǎn)擊事件8、設(shè)置定時(shí)器 手動(dòng)調(diào)用右側(cè)按鈕點(diǎn)擊事件

html代碼部分

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta http-equiv='X-UA-Compatible' content='IE=edge'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Document</title> <link rel='stylesheet' href='http://www.lshqa.cn/bcjs/css/index.css' > <script src='http://www.lshqa.cn/bcjs/js/index.js'></script></head><body> <div class='focus'> <a href='javascript:;' class='arrow_r'>></a> <a href='javascript:;' class='arrow_l'> <</a> <ul> <li> <a href='http://www.lshqa.cn/bcjs/14158.html'><img src='http://www.lshqa.cn/bcjs/images/focus.jpg' alt=''></a> </li> <li> <a href='http://www.lshqa.cn/bcjs/14158.html'><img src='http://www.lshqa.cn/bcjs/images/focus1.jpg' alt=''></a> </li> <li> <a href='http://www.lshqa.cn/bcjs/14158.html'><img src='http://www.lshqa.cn/bcjs/images/focus2.jpg' alt=''></a> </li> <li> <a href='http://www.lshqa.cn/bcjs/14158.html'><img src='http://www.lshqa.cn/bcjs/images/focus3.jpg' alt=''></a> </li> </ul> <ol class='circle'> </ol> </div></body></html>

css樣式部分

* { padding: 0; margin: 0; } li { list-style: none; } img { border: 0; vertical-align: top; } a { text-decoration: none; } .focus { position: relative; width: 721px; height: 455px; margin: 100px auto; overflow: hidden; } .focus ul { position: absolute; top: 0; left: 0; width: 600%; } .focus ul li { float: left; } .arrow_r, .arrow_l { display: none; position: absolute; top: 50%; transform: translateY(-50%); width: 40px; height: 40px; line-height: 40px; text-align: center; font-size: 24px; background: rgba(0, 0, 0, .2); color: #fff; z-index: 1; } .arrow_r { right: 0; } .circle { position: absolute; bottom: 10px; left: 50px; } .circle li { float: left; width: 8px; height: 8px; border: 2px solid rgba(255, 255, 255, .5); border-radius: 50%; margin: 0 3px; cursor: pointer; } .current { background-color: #fff; }

javascript部分

window.addEventListener(’load’, function() { //獲取元素 var focus = document.querySelector(’.focus’); var arrow_r = document.querySelector(’.arrow_r’); var arrow_l = document.querySelector(’.arrow_l’); var focusWidth = focus.offsetWidth; // 鼠標(biāo)經(jīng)過(guò)focus盒子 顯示/隱藏 左右側(cè)按鈕 暫停輪播 focus.addEventListener(’mouseenter’, function() { arrow_r.style.display = ’block’; arrow_l.style.display = ’block’; clearInterval(timer); timer = null; }); focus.addEventListener(’mouseleave’, function() { arrow_r.style.display = ’none’; arrow_l.style.display = ’none’; timer = setInterval(function() { //調(diào)用點(diǎn)擊事件 arrow_r.click(); }, 2000); }); //動(dòng)態(tài)生成小圓圈 var ul = focus.querySelector(’ul’); var ol = focus.querySelector(’.circle’); for (var i = 0; i < ul.children.length; i++) { var li = document.createElement(’li’); li.setAttribute(’index’, i); ol.appendChild(li); //小圓圈點(diǎn)擊事件 li.addEventListener(’click’, function() { for (var i = 0; i < ol.children.length; i++) { ol.children[i].className = ’’; } var index = this.getAttribute(’index’); num = index; circle = index; this.className = ’current’; animate(ul, -index * focusWidth); }) } ol.children[0].className = ’current’; //克隆第一張圖片放到最后一張 var fis = ul.children[0].cloneNode(true); ul.appendChild(fis); //右側(cè)按鈕點(diǎn)擊事件 var num = 0; var circle = 0; arrow_r.addEventListener(’click’, function() { if (num === ul.children.length - 1) { ul.style.left = 0; num = 0; } num++; animate(ul, -num * focusWidth); circle++; if (circle === ul.children.length - 1) { circle = 0; } for (var i = 0; i < ol.children.length; i++) { ol.children[i].className = ’’; } ol.children[circle].className = ’current’; }); //左側(cè)按鈕點(diǎn)擊事件 arrow_l.addEventListener(’click’, function() { if (num == 0) { num = ul.children.length - 1; ul.style.left = -num * focusWidth + ’px’; } num--; animate(ul, -num * focusWidth); circle--; circle = circle < 0 ? ol.children.length - 1 : circle; // 調(diào)用函數(shù) circleChange(); }); //清除其余小圓圈的current類(lèi)名 function circleChange() { for (var i = 0; i < ol.children.length; i++) { ol.children[i].className = ’’; } // 留下當(dāng)前的小圓圈的current類(lèi)名 ol.children[circle].className = ’current’; } //動(dòng)畫(huà)函數(shù) function animate(obj, target, 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’; }, 15); } //自動(dòng)輪播放輪播圖 var timer = setInterval(function() { //調(diào)用點(diǎn)擊事件 arrow_r.click(); }, 2000);});

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 精品亚洲永久免费精品 | 狠狠色狠狠综合久久 | 国产农村乱子伦精品视频 | 99精品在线| 国产免费人视频在线观看免费 | 国产女厕偷窥系列在线视频 | 久久久精品久久视频只有精品 | 午夜一级片 | 欧美成人亚洲欧美成人 | 亚洲国产精品欧美日韩一区二区 | 欧美5g影院天天爽天天看 | 蜜臀91精品国产高清在线观看 | 久久国产精品女 | 国产精品久久久久网站 | 97在线视频免费 | 久久亚洲精品国产精品777777 | 三级黄色毛片网站 | 丁香伊人五月综合激激激 | 亚洲欧美卡通成人制服动漫 | 草草视频在线免费观看 | 国产成年女一区二区三区 | 亚洲精品久久久久久久福利 | 二区视频在线 | 国产视频三区 | 国产亚洲精品一区二区在线观看 | 91专区在线 | 亚洲九九视频 | 日本一级高清不卡视频在线 | 欧美日韩一区二区不卡三区 | 人妖欧美一区二区三区四区 | 亚洲欧美日韩一区 | 亚洲国产精品线播放 | 久久人| 在线亚洲精品国产波多野结衣 | 国产永久精品 | 免费毛片a | 一区二区影视 | 狠狠色综合网站久久久久久久 | 新体操真 | 在线观看国产一级强片 | 91久久国产视频 |