<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> * { padding: 0; margin: 0; list-style: none; border: 0; } .all { width: 500px; height: 200px; padding: 7px; border: 1px solid #ccc; margin: 100px auto; position: relative; } .screen { width: 500px; height: 200px; overflow: hidden; position: relative; } .screen li { width: 500px; height: 200px; overflow: hidden; float: left; } .screen ul { position: absolute; left: 0; top: 0px; width: 3000px; } .all ol { position: absolute; right: 10px; bottom: 10px; line-height: 20px; text-align: center; } .all ol li { float: left; width: 20px; height: 20px; background: #fff; border: 1px solid #ccc; margin-left: 10px; cursor: pointer; } .all ol li.current { background: #DB192A; } #arr { display: none; } #arr span { width: 40px; height: 40px; position: absolute; left: 5px; top: 50%; margin-top: -20px; background: #000; cursor: pointer; line-height: 40px; text-align: center; font-weight: bold; font-family: '黑体'; font-size: 30px; color: #fff; opacity: 0.3; border: 1px solid #fff; } #arr #right { right: 5px; left: auto; } </style> </head> <body> <div class="all" id='box'> <div class="screen"><!--相框--> <ul> <li><img src="images/1.jpg" width="500" height="200"/></li> <li><img src="images/2.jpg" width="500" height="200"/></li> <li><img src="images/3.jpg" width="500" height="200"/></li> <li><img src="images/4.jpg" width="500" height="200"/></li> <li><img src="images/5.jpg" width="500" height="200"/></li> </ul> <ol> </ol> </div> <div id="arr"><span id="left"><</span><span id="right">></span></div> </div> <script src="common.js"></script> <script> //1. 获取所有的元素 //获取大盒子 var box = my$("box"); //获取相框 var screen = box.children[0]; //获取相框的宽度 var imgWidth = screen.offsetWidth; //获取ul var ulObj = screen.children[0]; //获取所有的li var list = ulObj.children; //获取ol var olObj = screen.children[1]; //获取左右焦点的div var arr = my$("arr"); var hhh = 0;//全局变量,存储ol中li的索引 //2. 根据ul中li的个数创建小按钮 for (var i = 0; i < list.length; i++) { //2.1 创建li,追加ol中 var liObj = document.createElement("li"); olObj.appendChild(liObj); //2.2 给li添加文本 liObj.innerHTML = i + 1; //2.4 给ol中每个li添加自定义的属性,用于存储索引 liObj.setAttribute("index", i); //2.3 给ol中每个li添加鼠标移入事件 liObj.onmouseover = function () { //① 排他,两件事:1.将所有的li样式还原,2.当前的li样式突出 for (var j = 0; j < olObj.children.length; j++) { olObj.children[j].removeAttribute("class"); } this.className = "current"; //② 鼠标移入哪一个li,那ul移动对应的距离,轮播实现 //1. 获取当前移入ol中的的li的索引===当前ul中的li的索引 hhh = this.getAttribute("index"); //2.移动ul,移动的距离=当前ul中的li的索引*图片的宽度 animate(ulObj, -hhh * imgWidth); } } //3. 手动设置ol中第一个li的背景颜色 olObj.firstElementChild.className = "current"; //4. 克隆ul中的第一个li,追加到ul中 ulObj.appendChild(list[0].cloneNode(true)); //8. 自动播放 var timeId = setInterval(clickHandle, 2000); // 5.鼠标移入移出事件,显示隐藏arr //5.1 鼠标移入 box.onmouseover = function () { arr.style.display = "block"; //8.2 清除定时器 clearInterval(timeId); }; //5.2 鼠标移出 box.onmouseout = function () { arr.style.display = "none"; //8.3 开启定时器 timeId = setInterval(clickHandle, 2000); }; //6. 点击右边按钮 my$("right").onclick = clickHandle; function clickHandle() { //6.3 当显示最后一个图片时,hhh=5,但是ol中li索引最大是4,报错 //又知 当图片显示最后一张时,给用户看到的是第一张, //所以,当用户再次点击按钮,应该看到第二个图片 if (hhh == list.length - 1) { //把第6个图片,跳转到第一个图片 hhh = 0; ulObj.style.left = "0px";//把ul位置还原 } //6.1 索引++,移动ul,完成轮播 hhh++; animate(ulObj, -hhh * imgWidth); //6.2 设置小圆点同步 // console.log(hhh); //当图片显示最后一张时,给用户看到的是第一张, // 所以,应该把ol中第一个li高亮显示,去除第五个li的样式 if (hhh == list.length - 1) { olObj.children[0].className = "current"; olObj.children[olObj.children.length - 1].removeAttribute("class"); } else { //排他 for (var j = 0; j < olObj.children.length; j++) { olObj.children[j].removeAttribute("class"); } olObj.children[hhh].className = "current"; } }; //7. 点击左边按钮 my$("left").onclick = function () { //7.3 如果是第一张图片,则直接蹦到最后一个图片 //所以,当用户再次点击按钮,应该看到倒数二个图片 if (hhh == 0) { hhh = list.length - 1; ulObj.style.left = -hhh * imgWidth + "px"; } //7.1 hhh--,移动ul,完成轮播 hhh--; animate(ulObj, -hhh * imgWidth); //7.2 设置小圆点样式同步 //排他 for (var j = 0; j < olObj.children.length; j++) { olObj.children[j].removeAttribute("class"); } olObj.children[hhh].className = "current"; }; </script> </body> </html>
轮播图
最新推荐文章于 2022-12-13 14:25:50 发布
