纯原生js轮播图
html代码
<div class="box">
<!-- 左右焦点div -->
<div id="focus" class="clearfix">
<span id="left"><</span>
<span id="right">></span>
</div>
<!-- 相册 -->
<ul id="rahmen" class="clearfix">
<li><a href="#"><img src="images/1.jpg"></a></li>
<li><a href="#"><img src="images/2.jpg"></a></li>
<li><a href="#"><img src="images/3.jpg"></a></li>
<li><a href="#"><img src="images/4.jpg"></a></li>
<li><a href="#"><img src="images/5.jpg"></a></li>
</ul>
<!-- 小圆点 -->
<ol></ol>
</div>
CSS代码
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.clearfix:after {
content: "";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix {
*zoom: 1;
}
.box {
width: 600px;
height: 400px;
margin: 60px auto;
position: relative;
overflow: hidden;
box-shadow: 2px 10px 13px 16px rgba(0, 0, 0, .4);
}
img {
display: block;
}
#focus {
width: 100%;
height: 100%;
position: absolute;
display: none;
}
#focus span {
width: 40px;
height: 40px;
position: absolute;
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;
z-index: 10;
}
#focus #right {
right: 0;
}
#focus span:hover {
opacity: 0.8;
background-color: skyblue;
}
li {
list-style: none;
float: left;
}
#rahmen {
width: 600%;
position: absolute;
}
#rahmen img {
width: 600px;
height: 400px;
}
.box ol {
width: 100%;
height: 20px;
position: absolute;
bottom: 20px;
left: 40px;
}
.box ol li {
display: inline-block;
width: 12px;
height: 12px;
margin-right: 15px;
border: 1px solid #fff;
border-radius: 6px;
cursor: pointer;
}
.box ol li:last-child {
margin-right: 0;
}
.current {
background-color: #FFFFFF;
}
js代码
var box = document.querySelector(".box");
var focus = box.querySelector("#focus");
var right = document.getElementById("right");
var left = document.getElementById("left");
var ulObj = document.getElementById("rahmen");
var list = ulObj.querySelectorAll("li");
var olObj = box.querySelector("ol");
var imgWidth = box.offsetWidth;
var aObjs = ulObj.querySelectorAll("a");
var flag = true;
for (var i = 0; i < aObjs.length; i++) {
aObjs[i].onclick = function() {
return false;
};
}
var walk = 0;
for (var i = 0; i < list.length; i++) {
var liObj = document.createElement("li");
olObj.appendChild(liObj);
liObj.setAttribute("index", i);
olObj.firstElementChild.className = "current";
liObj.addEventListener("mouseover", mouseoverManage)
}
function mouseoverManage() {
if (flag) {
flag = false;
for (var i = 0; i < olObj.children.length; i++) {
olObj.children[i].removeAttribute("class");
}
this.className = "current";
walk = this.getAttribute("index");
animate(ulObj, -walk * imgWidth, function() {
flag = true;
});
}
}
ulObj.appendChild(ulObj.firstElementChild.cloneNode(true));
box.onmouseover = function() {
focus.style.display = "block";
clearInterval(timer);
timer = null;
};
box.onmouseout = function() {
focus.style.display = "none";
timer = setInterval(clickManage, 1500);
};
right.onclick = clickManage;
function clickManage() {
if (flag) {
flag = false;
if (walk == olObj.children.length) {
walk = 0;
ulObj.style.left = 0 + "px";
}
walk++;
animate(ulObj, -walk * imgWidth, function() {
flag = true;
});
if (walk == olObj.children.length) {
olObj.children[olObj.children.length - 1].className = "";
olObj.firstElementChild.className = "current";
} else {
for (var i = 0; i < olObj.children.length; i++) {
olObj.children[i].removeAttribute("class");
}
olObj.children[walk].className = "current";
}
}
};
left.onclick = function() {
if (flag) {
flag = false;
if (walk == 0) {
walk = olObj.children.length;
ulObj.style.left = -walk * imgWidth + "px";
}
walk--;
animate(ulObj, -walk * imgWidth, function() {
flag = true;
});
for (var i = 0; i < olObj.children.length; i++) {
olObj.children[i].removeAttribute("class");
}
olObj.children[walk].className = "current";
}
};
var timer = setInterval(clickManage, 1500);
function animate(element, target, fn) {
clearInterval(element.timeId);
element.timeId = setInterval(function() {
var current = element.offsetLeft;
var step = 10;
step = current > target ? -step : step;
if (Math.abs(current - target) > Math.abs(step)) {
element.style.left = current + step + "px";
} else {
clearInterval(element.timeId);
element.style.left = target + "px";
if (fn) {
fn();
}
}
}, 1)
}