轮播图-仿jd-原生js编写
创建时间:2018-2-22 22:08:40 -最后修改时间:2018-2-22 22:08:40 -阅读:53 -评论:0
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: yellow;
}
#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;
}
html
<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>
javaScript
var timer = null;
//找人
var box = document.getElementById("box");
var screen = box.children[0];
var ul = screen.children[0];
var ulLis = ul.children;
var ol = screen.children[1];
var arr = document.getElementById("arr");
var left = document.getElementById("left");
var right = document.getElementById("right");
var imgWidth = screen.offsetWidth;
//动态生成结构
//生成ol中的按钮
for (var i = 0; i < ulLis.length; i++) {
var li = document.createElement("li");
li.innerHTML = i + 1;
ol.appendChild(li);
}
var olLis = ol.children;
olLis[0].className = "current";
//克隆第一张图
var firstImg = ulLis[0].cloneNode(true);
ul.appendChild(firstImg);
//鼠标经过按钮 按钮排他 移动ul到相应位置
for (var j = 0; j < olLis.length; j++) {
olLis[j].index = j;
olLis[j].onmouseover = function () {
//干掉所有人
for (var i = 0; i < olLis.length; i++) {
olLis[i].className = "";
}
//留下我自己
this.className = "current";
//移动ul
//目标和 当前按钮的索引有关 和图片宽度有关 而且是负数
var target = -this.index * imgWidth;
animate(ul, target);
//把记录当前显示的图片的索引的pic统一成当前按钮的索引
//把记录当前亮起的按钮的索引的square统一成当前按钮的索引
pic = square = this.index;
};
}
//点击箭头
box.onmouseover = function () {
arr.style.display = "block";
clearInterval(timer);
};
box.onmouseout = function () {
arr.style.display = "none";
timer = setInterval(playNext, 1000);
};
var pic = 0;//记录当前显示的图片的索引
var square = 0;//记录当前亮起的按钮的索引
//点击右箭头 移动ul到相应位置
right.onclick = function () {
//如果是最后一张 就应该 瞬间跳回开始 然后让ul从真的第一张渐渐地移动到第二张
if (pic === ulLis.length - 1) {//最后一张图片的索引
ul.style.left = 0;
pic = 0;//pic也要归零
}
pic++;//计算接下来要显示的图片的索引
//目标 和pic有关 和图片宽度有关 而且是负数
var target = -pic * imgWidth;
animate(ul, target);
//按钮也要跟着跑
if (square < olLis.length - 1) {
square++;//计算出接下来要亮起的按钮的索引
} else {
square = 0;
}
//干掉所有人
for (var i = 0; i < olLis.length; i++) {
olLis[i].className = "";
}
//留下对应的
olLis[square].className = "current";
};
left.onclick = function () {
//如果是第一张 就应该 瞬间跳回最后 然后让ul从假的第一张渐渐地移动到真的最后一张
if (pic === 0) {
ul.style.left = -(ulLis.length - 1) * imgWidth + "px";
pic = ulLis.length - 1;//pic要变到最后
}
pic--;//计算接下来要显示的图片的索引
//目标 和pic有关 和图片宽度有关 而且是负数
var target = -pic * imgWidth;
animate(ul, target);
//按钮也要跟着跑
if (square > 0) {
square--;//计算出接下来要亮起的按钮的索引
} else {
square = olLis.length - 1;
}
//干掉所有人
for (var i = 0; i < olLis.length; i++) {
olLis[i].className = "";
}
//留下对应的
olLis[square].className = "current";
};
//添加自动滚动
timer = setInterval(playNext, 1000);
function playNext() {
right.onclick();
}
function animate(obj, target) {
clearInterval(obj.timer);
obj.timer = setInterval(function () {
var leader = obj.offsetLeft;
var step = 30;
step = leader < target ? step : -step;
if (Math.abs(target - leader) >= Math.abs(step)) {
leader = leader + step;
obj.style.left = leader + "px";
} else {
obj.style.left = target + "px";
clearInterval(obj.timer);
}
}, 15);
}