编程的核心是思路。
1 <div id="box"> 2 <div id="wrap"> 3 </div> 4 </div>
1 <style> 2 body{ 3 margin: 0; 4 background-color: #5e5e5e; 5 } 6 #box{ 7 width: 857px; 8 height: 574px; 9 padding-top: 126px; 10 padding-left:143px; 11 background: url(img/bg.png) no-repeat; 12 margin: 0px auto; 13 } 14 #wrap{ 15 width: 700px; 16 height: 420px; 17 transform-style: preserve-3d; 18 perspective: 800px; 19 } 20 #wrap div{ 21 width: 10px; 22 height: 420px; 23 background: url(img/01.png) no-repeat; 24 float: left; 25 transition: .5s; 26 } 27 </style>
<script type="text/javascript"> window.onload = function(){ var wrap = document.getElementById("wrap"); var str = "" for (var i = 0; i <70; i++) { str += "<div style ='background-Position-X :"+(-10*i)+"px'+></div>" } wrap.innerHTML = str; var divs = wrap.getElementsByTagName("div"); var num = 0; var timer ; var imgnum = 2; //从第二张开始变 go(); function go() { timer = setInterval(function(){ tab(num); num++; if(num == 71){ //为什么是71 因为 第 divs[n -1] = divs [69] n = 70 clearInterval(timer);//不清定时器的后果是 动画速度累加 一次结束加一次 console.log("一次结束了") imgnum++; if(imgnum == 6){ imgnum = 1; } num = 0; setTimeout(go,500); } },80) } function tab ( n) //切换的效果是 前一张变不透明 当前一张透明 { if (divs[n-1]) { divs[n-1].style.opacity = 1; } if(divs[n]) { divs[n].style.opacity = 0; divs[n].style.backgroundImage = "url(img/0"+imgnum+".png)"; } } } </script>