1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| <head> <meta charset="UTF-8"> <title>轮播图</title> <style> *{ list-style:none; } #box{ width: 400px; height: 300px; margin: 0 auto; border: 1px solid #000; } #container{ position:relative; } #imglist img{ width: 400px; height: 300px; } #iconlist{ position: absolute; right:110px; bottom:10px; } #iconlist ul li{ width: 30px; height: 30px; border-radius: 50%; float: left; background: rgba(0,200,255,0.4); line-height: 30px; text-align: center; margin-left: 10px; cursor: pointer; }
</style> </head> <body> <h1>轮播图</h1> <hr> <div id="box"> <div id="container"> <div id="imglist"> <img src="./imgs/mm041.jpg" style="display:block"> <img src="./imgs/mm042.jpg" style="display:none"> <img src="./imgs/mm043.jpg" style="display:none"> <img src="./imgs/mm044.jpg" style="display:none"> <img src="./imgs/mm045.jpg" style="display:none"> </div> <div id="iconlist"> <ul> <li style="color:red;background: rgba(0,255,255,1);">1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> </div> </div> <button onclick="previous()">previous</button> <button onclick="next()">next</button> </div>
<script> var imgs = imglist.children; var lis = document.getElementsByTagName('li'); console.log(lis);
var t = 0; function run(){ t++; if (t>=imgs.length) { t=0; } showPic(t); showBtn(t); console.log(t); } var cl1 = setInterval(run,1000);
function showPic(t){ for (var i = 0; i < imgs.length; i++) { imgs[i].style.display="none"; } imgs[t].style.display="block"; }
function showBtn(t){ for (var i = 0; i < lis.length; i++) { lis[i].setAttribute('style',''); } lis[t].setAttribute('style',"color:red;background: rgba(0,255,255,1);"); }
// 鼠标划入时,解除定时器 container.onmouseover = function(){ clearInterval(cl1); } container.onmouseout = function(){ cl1 = setInterval('run()',1000); }
// 鼠标滑过按钮时,切换为绑定页面 for (var i = 0; i < lis.length; i++) { (function(i){ lis[i].onmouseover = function(){ t=i; showPic(t); showBtn(t); }
})(i); }
// 按钮控制下一张上一张 function previous(){ clearInterval(cl1); console.log(t); t--; if (t<0) { t=(imgs.length-1); } showPic(t); showBtn(t); cl1 = setInterval(run,1000); } function next(){ clearInterval(cl1); t++; if (t>(imgs.length-1)) { t=0; } showPic(t); showBtn(t); cl1 = setInterval(run,1000); }
</script>
</body>
|