js實(shí)現(xiàn)輪播圖制作方法
更新時間:2021年06月25日 13:36:22 作者:一個愛前端開發(fā)的小朋友
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)輪播圖的制作方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了js實(shí)現(xiàn)輪播圖展示的具體代碼,供大家參考,具體內(nèi)容如下
效果如圖所示
代碼如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> * { padding: 0; margin: 0; } .container { position: relative; width: 600px; height: 300px; margin: 30px auto; overflow: hidden; } .left { display: none; position: absolute; top: 50%; left: -20px; transform: translateY(-50%); width:50px; height: 50px; border-top-right-radius: 50%; border-bottom-right-radius: 50%; background-color: rgba(0,0,0,0.5); z-index: 999; } .left i { display: block; margin-top: 10px; margin-left: 20px; width: 30px; height: 30px; background: url(img/left.png) no-repeat; background-size: 30px 30px; } .right { display: none; position: absolute; top: 50%; right: -20px; transform: translateY(-50%); width:50px; height: 50px; border-top-left-radius: 50%; border-bottom-left-radius: 50%; background-color: rgba(0,0,0,0.5); z-index: 999; } .right i { display: block; margin-top: 10px; margin-right: 20px; width: 30px; height: 30px; background: url(img/right.png) no-repeat; background-size: 30px 30px; } ul li,ol li { list-style: none; } .picture { position: absolute; } .list { position: absolute; bottom: 10px; left: 10px; } .list li { float: left; margin-right: 10px; width: 10px; height: 10px; border-radius: 10px; background-color: rgba(0,0,0,0.5); cursor: pointer; } .list .current { background-color: #fff; } .picture li { position: absolute; width: 600px; height: 300px; } img { width: 100%; height: 100%; } </style> </head> <body> <div class="container"> <span class="left"><i></i></span> <span class="right"><i></i></span> <ul class="picture"> <li><img src="img/1.jpg" ></li> <li><img src="img/2.jpg" ></li> <li><img src="img/3.jpg" ></li> <li><img src="img/4.jpg" ></li> <li><img src="img/5.jpg" ></li> </ul> <ol class="list"> </ol> </div> <script type="text/javascript"> var picture = document.querySelector('.picture'); var list = document.querySelector('.list'); var num=0; var circle=0; for (i=0;i<picture.children.length;i++) { // 設(shè)置圖片的位置 picture.children[i].style.left = i*600 + 'px'; // 自動生成有序列表 var li = document.createElement('li'); li.setAttribute('index',i); list.appendChild(li); // 給li添加點(diǎn)擊事件 li.addEventListener('click',function () { for (var i=0;i<list.children.length;i++) { list.children[i].className = ''; } this.className = 'current'; var index = this.getAttribute('index'); num = index; circle = index; animate(picture,-index*600); }) } // 設(shè)置第一個ol孩子的類名 list.children[0].className = 'current'; var left = document.querySelector('.left'); var right = document.querySelector('.right'); var container = document.querySelector('.container'); // 設(shè)置鼠標(biāo)經(jīng)過離開事件 container.addEventListener('mouseover',function () { left.style.display = 'block'; right.style.display = 'block'; clearInterval(timer) timer = null; }) container.addEventListener('mouseleave',function () { left.style.display = 'none'; right.style.display = 'none'; timer = setInterval(function () { right.click(); },1000); }) // js動畫函數(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); if (callback) { callback(); } } obj.style.left = obj.offsetLeft + step + 'px'; },15) } var first = picture.children[0].cloneNode(true); picture.appendChild(first); picture.lastChild.style.left = (picture.children.length-1)*600 + 'px'; //右側(cè)點(diǎn)擊事件 right.addEventListener('click',function () { if (num==picture.children.length-1) { picture.style.left = 0; num = 0; } num++; animate(picture,-num*600); circle ++; if (circle == list.children.length) { circle = 0; } for (var i = 0;i<list.children.length;i++) { list.children[i].className = ''; } list.children[circle].className = 'current'; }) // 左側(cè)點(diǎn)擊事件 left.addEventListener('click',function () { if (num==0) { picture.style.left = -(picture.children.length-1)*600 +'px'; num = picture.children.length-1; } num--; animate(picture,-num*600); circle --; if (circle < 0) { circle = list.children.length-1; } for (var i = 0;i<list.children.length;i++) { list.children[i].className = ''; } list.children[circle].className = 'current'; }) var timer = setInterval(function () { // 手動調(diào)用 right.click(); },1000); </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- js實(shí)現(xiàn)輪播圖的完整代碼
- 原生js實(shí)現(xiàn)輪播圖的示例代碼
- js實(shí)現(xiàn)支持手機(jī)滑動切換的輪播圖片效果實(shí)例
- JS輪播圖實(shí)現(xiàn)簡單代碼
- js實(shí)現(xiàn)點(diǎn)擊左右按鈕輪播圖片效果實(shí)例
- JS實(shí)現(xiàn)自動輪播圖效果(自適應(yīng)屏幕寬度+手機(jī)觸屏滑動)
- JS實(shí)現(xiàn)左右無縫輪播圖代碼
- 使用html+js+css 實(shí)現(xiàn)頁面輪播圖效果(實(shí)例講解)
- 原生js實(shí)現(xiàn)無限循環(huán)輪播圖效果
- js實(shí)現(xiàn)從左向右滑動式輪播圖效果
相關(guān)文章
JavaScript正則表達(dá)式匹配 div style標(biāo)簽
這篇文章主要介紹了JavaScript正則表達(dá)式匹配<div><style>標(biāo)簽 的相關(guān)資料,需要的朋友可以參考下2016-03-03JavaScript鼠標(biāo)事件監(jiān)聽、觸發(fā)時機(jī)和觸發(fā)順序?qū)嵗v解
事件監(jiān)聽是Web開發(fā)中非常重要的一個概念,掌握了它的用法,可以讓我們實(shí)現(xiàn)更加豐富和動態(tài)的交互效果,這篇文章主要給大家介紹了關(guān)于JavaScript鼠標(biāo)事件監(jiān)聽、觸發(fā)時機(jī)和觸發(fā)順序的相關(guān)資料,需要的朋友可以參考下2024-01-01前端開發(fā)基礎(chǔ)javaScript的六大作用
這篇文章主要介紹了前端開發(fā)基礎(chǔ)javaScript的六大作用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08JavaScript代碼輕松實(shí)現(xiàn)網(wǎng)頁內(nèi)容禁止復(fù)制(代碼簡單)
有些時候我們寫的內(nèi)容不想被別人復(fù)制,在代碼中怎么實(shí)現(xiàn)的呢?下面小編給大家介紹javascript代碼輕松實(shí)現(xiàn)網(wǎng)頁內(nèi)容禁止復(fù)制,感興趣的童鞋一起看看吧2015-10-10解決window.history.back()返回上一頁有時候需要點(diǎn)擊多次問題
這篇文章主要介紹了解決window.history.back()返回上一頁有時候需要點(diǎn)擊多次問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03