原生JS實現(xiàn)的簡單輪播圖功能【適合新手】
本文實例講述了原生JS實現(xiàn)的簡單輪播圖功能。分享給大家供大家參考,具體如下:
經(jīng)過幾天的努力,終于攻克了這一難題,于是迫不及待的想要分享給大家,編寫之前,我也看了不少其他博主的博客,大多是用偏移量寫的,對新手來說,還是有些難以理解,雖然可能實現(xiàn)的需求不一樣,但我想先從簡入手,所以自己查閱資料,修改bug,終于完成。話不多說,上代碼:
<!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>chabaoo.cn JS輪播圖</title> <script src="jquery.js"></script> <style> *{ margin: 0px; padding:0px; list-style: none; text-decoration: none; } #flash{ /*根據(jù)圖片的大小來設置外層div的大小*/ width: 520px; height: 280px; margin: 0 auto; position: relative; /*設置div定位,方便之后圖片及圓點位置的設定*/ border:1px solid black; } #flash img{ width: 100%; height: 100%; position: absolute; /*設置所有圖片重疊*/ left: 0px; top: 0px; display: none; /*設置所有圖片隱藏,通過改變第一個圖片的行間樣式來使第一個圖片顯示*/ } #flash ul{ width: 150px; height: 25px; border-radius: 20px; background-color:rgba(255,255,255,0.5); position: absolute; left: 35%; bottom: 10%; } #flash ul li{ width: 12px; height: 12px; margin-top:5px; background-color: #fff; border-radius: 50%; margin-left: 15px; float: left; } #flash ul .li_1{ background-color: #f40; /*設置第一個圓點背景色為紅色*/ } #flash .span-r{ width: 50px; height: 50px; border-radius: 50%; position: absolute; right: 2%; top: 45%; background-color: rgba(255,255,255,0.5); } #flash .span-r span{ width: 100%; height:100%; color:rgba(0,0,0,0.5); font-size: xx-large; font-weight: 700; line-height: 50px; margin-left: 15px; cursor: pointer; } #flash .span-l{ width: 50px; height: 50px; border-radius: 50%; position: absolute; left: 2%; top: 45%; background-color: rgba(255,255,255,0.5); } #flash .span-l span{ width: 100%; height:100%; color:rgba(0,0,0,0.5); font-size: xx-large; font-weight: 700; line-height: 50px; margin-left: 15px; cursor: pointer; } </style> </head> <div id="flash"> <img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/Guardians-of-the-Galaxy-Poster-High-Res.jpg" alt="" style="display: block"> <img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/Blade-Runner-poster-art-Harrison-Ford.jpg" alt=""> <img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/2017_alien_covenant_4k-5120x2880-1920x1080.jpg" alt=""> <img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/robocop-1987-wallpaper-2.jpg" alt=""> <img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/sJALsDXak4EehSg2F2y92rt5hPe.jpg" alt=""> <ul> <li class="li_1"></li> <li></li> <li></li> <li></li> <li></li> </ul> <div class="span-r"> <span>></span> </div> <div class="span-l"> <span><</span> </div> </div> <body> <script> var div = document.getElementById('flash'); var img = div.getElementsByTagName('img'); /*選中div下所有的圖片*/ var ul = document.getElementsByTagName('ul')[0]; var li = ul.getElementsByTagName('li'); var div_r = document.getElementsByTagName('div')[1]; // var span_r = div_r.getElementsByTagName('span'); var div_l = document.getElementsByTagName('div')[2]; // var sapn_l = div_l.getElementsByTagName('span'); var len = img.length; var count = 0; /*設置count來顯示當前圖片的序號*/ function run(){ /*將定時器里的函數(shù)提取到外部*/ count++; count = count==5?0:count; /*當圖片加載到最后一張時,使其歸零*/ for(var i=0;i<len;i++){ img[i].style.display = 'none'; /*利用for循環(huán)使除當前count位其他圖片隱藏*/ } img[count].style.display = 'block'; /*顯示當前count的值所代表的圖片*/ for(var i=0;i<li.length;i++){ li[i].style.backgroundColor = "#fff"; /*原理同上*/ } li[count].style.backgroundColor = "#f40"; } var timer = setInterval(run,1000); /*定義定時器,使圖片每隔1s更換一次*/ div.onmouseover = function(){ clearInterval(timer); } div.onmouseleave = function(){ /*定義鼠標移出事件,當鼠標移出div區(qū)域,輪播繼續(xù)*/ timer = setInterval(run,1000); } for(var i=0;i<len;i++){ li[i].index = i; /*定義index記錄當前鼠標在li的位置*/ li[i].onmouseenter = function(){ /*定義鼠標經(jīng)過事件*/ for(var i=0;i<len;i++){ /*通過for循環(huán)將所有圖片隱藏,圓點背景設為白色*/ li[i].style.background = '#fff'; img[i].style.display = 'none'; } this.style.background = '#f40'; /*設置當前所指圓點的背景色*/ img[this.index].style.display = 'block'; /*使圓點對應的圖片顯示*/ } } div_r.onclick = function(){ /*因為span沒有設置寬高,直接把事件添加到他的父級*/ run(); /*直接調用現(xiàn)成的run函數(shù)*/ } function reverse(){ count--; count = count==-1?4:count; for(var i=0;i<len;i++){ img[i].style.display = 'none'; /*利用for循環(huán)使除當前count位其他圖片隱藏*/ } img[count].style.display = 'block'; /*顯示當前count的值所代表的圖片*/ for(var i=0;i<li.length;i++){ li[i].style.backgroundColor = "#fff"; /*原理同上*/ } li[count].style.backgroundColor = "#f40"; } div_l.onclick = function(){ reverse(); /*重新設置函數(shù)*/ } </script> </body> </html>
這里使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun,測試運行效果如下:
感興趣的朋友可以自己動手測試一下。
更多關于JavaScript相關內容感興趣的讀者可查看本站專題:《JavaScript圖片操作技巧大全》、《JavaScript切換特效與技巧總結》、《JavaScript運動效果與技巧匯總》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調試技巧總結》、《JavaScript數(shù)據(jù)結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數(shù)學運算用法總結》
希望本文所述對大家JavaScript程序設計有所幫助。
- 原生JS實現(xiàn)的輪播圖功能詳解
- JS使用tween.js動畫庫實現(xiàn)輪播圖并且有切換功能
- js原生實現(xiàn)移動端手指滑動輪播圖效果的示例
- JavaScript實現(xiàn)帶有子菜單和控件的slider輪播圖效果
- 無限循環(huán)輪播圖之運動框架(原生JS實現(xiàn))
- 使用html+js+css 實現(xiàn)頁面輪播圖效果(實例講解)
- 利用純js + transition動畫實現(xiàn)移動端web輪播圖詳解
- JS輪播圖實現(xiàn)簡單代碼
- js實現(xiàn)移動端輪播圖效果
- 原生js實現(xiàn)輪播圖的示例代碼
- js實現(xiàn)支持手機滑動切換的輪播圖片效果實例
相關文章
利用JavaScript實現(xiàn)靜態(tài)圖片局部流動效果
如果你有玩過《王者榮耀》、《陰陽師》?等手游,一定注意到過它的啟動動畫、皮膚立繪卡片等場景,經(jīng)常采用靜態(tài)底圖加局部液態(tài)流動效果的簡單動畫,本文將利用JavaScript實現(xiàn)這一效果,需要的可以參考一下2022-08-08