《javaScript100例|03》自写javaScript+CSS轮显效果
生活随笔
收集整理的這篇文章主要介紹了
《javaScript100例|03》自写javaScript+CSS轮显效果
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
效果圖
代碼示例
源碼地址:自寫Js+CSS輪顯效果.rar-互聯(lián)網(wǎng)文檔類資源-CSDN下載https://download.csdn.net/download/weixin_41937552/37380520
效果圖
代碼示例
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>JS圖片輪顯切換</title> <style type="text/css"> *{ margin:0; padding:0} #wapper{ position:relative;height:280px; width:316px; overflow:hidden} #slideshow{width:316px; height:300px; overflow:hidden; position:relative;} #imglist{width:1264px; height:100%; float:left; padding:0; margin:0;position:absolute; left:0} #imglist li{ width:25%;height:100%; float:left} #nextlist{position:absolute; z-index:9} #nextlist li{ float:left; display:inline; height:30px; padding:10px 0; width:79px;} #nextlist li.current{background:#c00} #bg{ position:absolute; z-index:1;background:#ff0;filter:alpha(opacity=20);opacity:0.2; height:50px; width:316px} </style> </head> <body> <div id="wapper"> <div id="slideshow"> <ul id="imglist"> <li style=" background:#f0c;">1</li> <li style=" background:#cf0;">2</li> <li style=" background:#09e;">3</li> <li style=" background:#efc;">4</li> </ul> </div> <div id="nextlist"> <ul> <li class="current">111</li> <li>222</li> <li>333</li> <li>444</li> </ul> </div> <div id="bg"></div> </div> <script type="text/javascript"> var num = 0; function nextElement(eleObj,aa){ var obj = document.getElementById(eleObj);if (obj.movement) {clearTimeout(obj.movement);}if (!obj.style.bottom) {obj.style.bottom = "-50px";}var xpos = parseInt(obj.style.bottom);if (xpos == aa) {return false;}if (xpos > aa) {var dist = Math.ceil((xpos - aa)/5);xpos = xpos - dist;}if (xpos < aa) {var dist = Math.ceil((aa - xpos)/5);xpos = xpos + dist;}obj.style.bottom = xpos + "px";var repeat = "nextElement('"+eleObj+"','"+aa+"')";obj.movement = setTimeout(repeat,30); } function moveElement() { var elem = document.getElementById("slideshow"); var nextElem = document.getElementById("nextlist"); var imgElem = document.getElementById("imglist"); var lis = elem.getElementsByTagName("li"); var nextLis = nextElem.getElementsByTagName("li");if (imgElem.movement) {clearTimeout(imgElem.movement);}if (!imgElem.style.left) {imgElem.style.left = "0";}var xpos = parseInt(imgElem.style.left);var naa = num * -316;if (xpos == naa) {if(num==(lis.length - 1) || num>lis.length){num=0;return false;}else{num++;return false;}}for(var i=0;i<nextLis.length;i++){nextLis[i].className="";nextLis[num].className="current";}if (xpos > naa) {var dist = Math.ceil((xpos - naa)/4);xpos = xpos - dist;}if (xpos < naa) {var dist = Math.ceil((naa - xpos)/4);xpos = xpos + dist;}imgElem.style.left = xpos + "px";var repeat = "moveElement()";imgElem.movement = setTimeout(repeat,30);} function checkMove(){ var elem = document.getElementById("slideshow"); var nextElem = document.getElementById("nextlist"); var imgElem = document.getElementById("imglist"); var bgElem = document.getElementById("wapper"); var lis = elem.getElementsByTagName("li"); var nextLis = nextElem.getElementsByTagName("li");bgElem.onmouseover=function(){nextElement("nextlist",0);nextElement("bg",0);}bgElem.onmouseout=function(){nextElement("nextlist",-50);nextElement("bg",-50);}for(var i=0;i<nextLis.length;i++){nextLis[i].onmouseover=function(){clearInterval(changebox);thisMove(this);}nextLis[i].onmouseout=function(){startMove();}lis[i].onmouseover=function(){clearInterval(changebox);//thisStop(this);}lis[i].onmouseout=function(){startMove();}}function startMove(){changebox = setInterval("moveElement()",4000);}function thisMove(obj){for(var i=0;i<nextLis.length;i++){nextLis[i].className="";if(nextLis[i]==obj){nextLis[i].className="current";num=i;setTimeout("moveElement()",0);}else{nextLis[i].className="";}}} } changebox = setInterval("moveElement()",4000); checkMove(); </script> </body> </html>源碼地址:自寫Js+CSS輪顯效果.rar-互聯(lián)網(wǎng)文檔類資源-CSDN下載https://download.csdn.net/download/weixin_41937552/37380520
總結(jié)
以上是生活随笔為你收集整理的《javaScript100例|03》自写javaScript+CSS轮显效果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《javaScript100例|02》超
- 下一篇: 《javaScript100例|04》自