原生JS實(shí)現(xiàn)手動(dòng)輪播圖效果實(shí)例代碼
手動(dòng)輪播圖,為輪播圖中的一種,輪播圖主要有無縫輪播,手動(dòng)輪播,延遲輪播,切換輪播等等。。。
輪播圖主要用于展現(xiàn)圖片,新出商品,詞條,又能美觀網(wǎng)頁。給網(wǎng)頁中增加動(dòng)態(tài)效果。
手動(dòng)輪播,是小編認(rèn)為最簡單的一種輪播方式,既能左右翻頁,還能通過懸浮按鈕,快速預(yù)覽圖片,所以今天就給大家寫一個(gè)原生js手動(dòng)輪播圖。
一,利用JavaScript制作手動(dòng)輪播圖,首先排版。
引入默認(rèn)樣式表(可以手寫);
<link rel="stylesheet" href="css/Default style sheet.css" rel="external nofollow" rel="external nofollow" />//本博客有css默認(rèn)樣式表可以下載。
div排版布局:
<body> <div id="box"> <div id="lunbo"><img src="img/1.jpg" id="img"/></div> <div id="left"><</div> <div id="right">></div> <div id="radiu"> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> </div> </div> </body>
二,定義div的CSS樣式,給div設(shè)置寬高,定位。
<style> body{ background: darkturquoise; } #box{ height:320px; width:480px; margin: 3px auto; border: 2px solid red; } #lunbo{ height: 292px; width:453px; border: 1px solid yellow; margin: 0px auto; position: relative; } #left{ height: 60px; width: 60px; font-size: 60px; text-align: center; line-height: 60px; position: absolute; top:150px; left: 440px; color: black; } #right{ height: 60px; width: 60px; font-size: 60px; text-align: center; line-height: 60px; position: absolute; top:150px; left: 880px; color: black; } #radiu{ height: 30px; width: 240px; text-align: center; margin: 0px auto; } #radiu li{ float: left; background: white; height: 30px; width: 30px; line-height: 30px; border-radius:50% ; margin-right:6px; } .active{ background: orange; color: ; } </style> 原生js代碼: <script> window.onload=function(){ var ridiu=document.getElementById("radiu") var right=document.getElementById("right"); var left=document.getElementById("left") var img=document.getElementById("img"); var oli=ridiu.getElementsByTagName("li") var arry=['img/1.jpg','img/2.jpg','img/3.jpg','img/4.jpg','img/5.jpg'] var a=null; right.onclick=function(){ a++ if(a>arry.length-1){ a=0; } img.src=arry[a] } left.onclick=function(){ a-- if(a<0){ a=arry.length-1; } img.src=arry[a] } for (var i=0;i<oli.length;i++) { oli[i].onclick=function(){ a++ if(a==arry.length){ a=0; } img.src=arry[a]; } } } </script>
HTML全部效果代碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>手動(dòng)輪播圖</title> <link rel="stylesheet" href="css/Default style sheet.css" rel="external nofollow" rel="external nofollow" /> <style> body{ background: darkturquoise; } #box{ height:320px; width:480px; margin: 3px auto; border: 2px solid red; } #lunbo{ height: 292px; width:453px; border: 1px solid yellow; margin: 0px auto; position: relative; } #left{ height: 60px; width: 60px; font-size: 60px; text-align: center; line-height: 60px; position: absolute; top:150px; left: 440px; color: black; } #right{ height: 60px; width: 60px; font-size: 60px; text-align: center; line-height: 60px; position: absolute; top:150px; left: 880px; color: black; } #radiu{ height: 30px; width: 240px; text-align: center; margin: 0px auto; } #radiu li{ float: left; background: white; height: 30px; width: 30px; line-height: 30px; border-radius:50% ; margin-right:6px; } .active{ background: orange; color: ; } </style> <script> window.onload=function(){ var ridiu=document.getElementById("radiu") var right=document.getElementById("right"); var left=document.getElementById("left") var img=document.getElementById("img"); var oli=ridiu.getElementsByTagName("li") var arry=['img/1.jpg','img/2.jpg','img/3.jpg','img/4.jpg','img/5.jpg'] var a=null; right.onclick=function(){ a++ if(a>arry.length-1){ a=0; } img.src=arry[a] } left.onclick=function(){ a-- if(a<0){ a=arry.length-1; } img.src=arry[a] } for (var i=0;i<oli.length;i++) { oli[i].onclick=function(){ a++ if(a==arry.length){ a=0; } img.src=arry[a]; } } } </script> </head> <body> <div id="box"> <div id="lunbo"><img src="img/1.jpg" id="img"/></div> <div id="left"><</div> <div id="right">></div> <div id="radiu"> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> </div> </div> </body> </html>
效果圖:
body{ background: darkturquoise; } #box{ height:320px; width:480px; margin: 3px auto; border: 2px solid red; } #lunbo{ height: 292px; width:453px; border: 1px solid yellow; margin: 0px auto; position: relative; } #left{ height: 60px; width: 60px; font-size: 60px; text-align: center; line-height: 60px; position: absolute; top:150px; left: 440px; color: black; } #right{ height: 60px; width: 60px; font-size: 60px; text-align: center; line-height: 60px; position: absolute; top:150px; left: 880px; color: black; } #radiu{ height: 30px; width: 240px; text-align: center; margin: 0px auto; } #radiu li{ float: left; background: white; height: 30px; width: 30px; line-height: 30px; border-radius:50% ; margin-right:6px; } .active{ background: orange; color: ; }
相關(guān)文章
替代window.event.srcElement效果的可兼容性的函數(shù)
getEvent()方法可模擬window.event效果2009-12-12JavaScript 格式化數(shù)字、金額、千分位、保留幾位小數(shù)、舍入舍去
這篇文章主要介紹了JavaScript 格式化數(shù)字、金額、千分位、保留幾位小數(shù)、舍入舍去,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07關(guān)于javascript sort()排序你可能忽略的一點(diǎn)理解
最近在研究Javascript發(fā)現(xiàn)了其中一些之前忽略的問題,所以想著總結(jié)分享出來,下面這篇文章主要給大家介紹了關(guān)于javascript sort()排序你可能忽略的一點(diǎn)理解,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07如何在postman測試用例中實(shí)現(xiàn)斷言過程解析
這篇文章主要介紹了如何在postman測試用例中實(shí)現(xiàn)斷言過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07addEventListener()與removeEventListener()解析
這篇文章主要為大家詳細(xì)介紹了addEventListener()與removeEventListener(),用于處理指定和刪除事件處理程序操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04深入淺析JavaScript中數(shù)據(jù)共享和數(shù)據(jù)傳遞
這篇文章主要介紹了深入淺析JavaScript中數(shù)據(jù)共享和數(shù)據(jù)傳遞的相關(guān)資料,需要的朋友可以參考下2016-04-04