本文实例讲述了JS+CSS实现自动改变切换方向图片幻灯切换效果的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下: JS+CSS自动改变切换方向的图片幻灯切换效果 <br /> body,div,ul,li{margin:0;padding:0;}<br /> ul{list-style-type:none;}<br /> body{background:#000;text-align:center;font:12px/20px Arial;}<br /> #box{position:relative;width:322px;height:172px;background:#fff;border-radius:5px;border:8px solid #fff;margin:10px auto;}<br /> #box .list{position:relative;width:320px;height:240px;overflow:hidden;border:1px solid #ccc;}<br /> #box .list li{position:absolute;top:0;left:0;width:320px;height:240px;opacity:0;filter:alpha(opacity=0);}<br /> #box .list li.current{opacity:1;filter:alpha(opacity=100);}<br /> #box .count{position:absolute;right:0;bottom:5px;}<br /> #box .count li{color:#fff;float:left;width:20px;height:20px;cursor:pointer;margin-right:5px;overflow:hidden;background:#F90;opacity:0.7;filter:alpha(opacity=70);border-radius:20px;}<br /> #box .count li.current{color:#fff;opacity:1;filter:alpha(opacity=100);font-weight:700;background:#f60;}<br /> #tmp{width:100px;height:100px;background:red;position:absolute;}<br /> <br /> window.onload = function ()<br /> {<br /> var oBox = document.getElementById("box");<br /> var aUl = document.getElementsByTagName("ul");<br /> var aImg = aUl[0].getElementsByTagName("li");<br /> var aNum = aUl[1].getElementsByTagName("li");<br /> var timer = play = null;<br /> var i = index = 0; <br /> var bOrder = true;<br /> //切换按钮<br /> for (i = 0; i < aNum.length; i++)<br /> {<br /> aNum[i].index = i;<br /> aNum[i].onmouseover = function ()<br /> {<br /> show(this.index)<br /> }<br /> }<br /> //鼠标划过关闭定时器<br /> oBox.onmouseover = function ()<br /> {<br /> clearInterval(play) <br /> };<br /> //鼠标离开启动自动播放<br /> oBox.onmouseout = function ()<br /> {<br /> autoPlay()<br /> }; <br /> //自动播放函数<br /> function autoPlay ()<br /> {<br /> play = setInterval(function () {<br /> //判断播放顺序<br /> bOrder ? index++ : index--; <br /> //正序<br /> index >= aImg.length && (index = aImg.length - 2, bOrder = false);<br /> //倒序<br /> index <= 0 && (index = 0, bOrder = true);<br /> //调用函数<br /> show(index)<br /> },2000); <br /> }<br /> autoPlay();//应用<br /> function show (a)<br /> {<br /> index = a;<br /> var alpha = 0;<br /> for (i = 0; i < aNum.length; i++)aNum[i].className = "";<br /> aNum[index].className = "current";<br /> clearInterval(timer); <br /> for (i = 0; i < aImg.length; i++)<br /> {<br /> aImg[i].style.opacity = 0;<br /> aImg[i].style.filter = "alpha(opacity=0)"; <br /> }<br /> timer = setInterval(function () {<br /> alpha += 2;<br /> alpha > 100 && (alpha =100);<br /> aImg[index].style.opacity = alpha / 100;<br /> aImg[index].style.filter = "alpha(opacity = " + alpha + ")";<br /> alpha == 100 && clearInterval(timer)<br /> },20);<br /> }<br /> };<br /> 1 2 3 4 5 希望本文所述对大家的javascript程序设计有所帮助。