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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="move.js"></script> <style> *{ margin: 0; padding: 0; } a { text-decoration: none; } ul li { list-style-type: none; } img { width: 400px; height: 300px; } .main{ width: 400px; height: 300px; position: absolute; top: 50%; left: 50%; margin-left: -225px; margin-top: -175px; background-color: black; overflow: hidden; } ul{ position: absolute; top: 0; left: 0; width: 400%; } ul li{ float: left; } .left-arrow { position: absolute; font-size: 32px; color: black; top: 50%; background: rgba(0,0,0,.3); display: none; z-index: 9999; } .right-arrow { position: absolute; font-size: 32px; color: black; top: 50%; right: 0; background: rgba(0,0,0,.3); display: none; z-index: 9999; } .circle { position: absolute; bottom: 0px; left: 50px; } .circle li{ list-style-type: none; float: left; width: 8px; height: 8px; border: 2px solid rgba(255,255,255,0.5); margin: 0 3px; border-radius: 50%; cursor: pointer; } .current{ background: white; } </style> </head> <body> <div class="main"> <div class="left"> <a href="javascript:;" class="left-arrow"><</a> </div> <div class="right"> <a href="javascript:;" class="right-arrow">></a> </div> <ul> <li><img src="../../../jpgs/01.jpg" alt=""></li> <li><img src="../../../jpgs/02.jpg" alt=""></li> <li><img src="../../../jpgs/03.jpg" alt=""></li> </ul> <ol class="circle"> <!-- <li></li>--> <!-- <li class="current"></li>--> <!-- <li></li>--> </ol> </div>
<script> window.addEventListener('load',function () { var btnl = document.querySelector('.left-arrow'); var btnr = document.querySelector('.right-arrow'); var div = document.querySelector('.main'); var divWidth = div.offsetWidth; //鼠标经过图片显示与隐藏按钮 div.addEventListener('mouseenter',function (){ btnl.style.display = 'block'; btnr.style.display = 'block'; //鼠标经过停止定时器 clearInterval(timer); timer = null;//清除定时器变量 }); div.addEventListener('mouseleave',function () { btnl.style.display = 'none'; btnr.style.display = 'none'; //鼠标离开开始定时器 timer = setInterval(function (){ //手动调用点击事件 btnr.click(); },2000) }) //动态生成小圆圈 var ul = div.querySelector('ul'); var ol = div.querySelector('.circle'); for (var i = 0; i < ul.children.length; i++){ //创建一个li var li = document.createElement('li'); //通过自定义属性记录当前小圆圈的索引号 li.setAttribute('index',i); //把li插入ol ol.appendChild(li); //小圆圈排他思想 顺便在生成小圆圈的同时绑定点击事件 li.addEventListener('click',function () { //清除所有li的current类名 for (var i = 0; i < ol.children.length; i++){ ol.children[i].className = ''; } //当前li设置current类名 this.className = 'current'; //点击小圆圈,移动图片,移动的是ul //ul的移动距离就是小圆圈的索引号*图片宽度 //点击某个小圆圈就得到当前的索引号index var index = this.getAttribute('index'); //当点击了小圆圈,就把索引号给num num = index; //当点击了小圆圈,就把索引号给move move = index; animate(ul,-index * divWidth); }) } //设置ol里的第一个li的类名为 current ol.children[0].className = 'current'; //克隆第一张图片,放到ul最后 var firstli = ul.children[0].cloneNode(true); ul.appendChild(firstli);
//点击右侧按钮,图片滚动一张 var num = 0; //move控制小圆圈的播放 var move = 0; //节流阀 var flag = true; btnr.addEventListener('click',function () { if (flag) { flag = false;//关闭节流阀 //如果走到最后一张,ul快速复原 left改为0 if (num == ul.children.length - 1){ ul.style.left = 0; num = 0; } num++; animate(ul,-num * divWidth,function() { flag = true;//打开节流阀 }); //点击一次按钮,小圆圈发生变化 move++; //如果move == 3,说明走到了最后克隆的那张图片,复原 if (move == ol.children.length){ move = 0; } clearmove(); } });
//左按钮 btnl.addEventListener('click',function () { if (flag){ flag = false; //如果走到第一张 if (num == 0){ num = ul.children.length - 1; ul.style.left = - num * divWidth + 'px'; } num--; animate(ul,-num * divWidth,function () { flag = true; }); //点击一次按钮,小圆圈发生变化 move--; //如果move < 0,说明第一张图片,小圆圈要改为最后一个 if (move < 0){ move = ol.children.length - 1; } clearmove(); } }); function clearmove(){ //清除小圆圈的类名 for (var i = 0; i < ol.children.length; i++){ ol.children[i].className = ''; } //留下当前小圆圈的类名 ol.children[move].className = 'current'; }
//自动播放 var timer = setInterval(function (){ //手动调用点击事件 btnr.click(); },2000) }) </script> </body> </html>
|