js原生滚动轮播图
演示图如上,这是标准的轮播图,录了一段视频,过几天会传上来。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 300px;
height: 300px;
margin: 100px auto;
border: 5px solid greenyellow;
border-radius: 5px;
position: relative;
overflow: hidden;
}
* {
margin: 0;
padding: 0;
}
ul,
ol,
li {
margin: 0;
padding: 0;
list-style: none;
}
img {
width: 100%;
height: 100%;
}
.hello {
width: 600%;
height: 100%;
position: absolute;
left: 0;
}
.hello li {
width: 300px;
height: 100%;
float: left;
}
.btn {
width: 300px;
height: 30px;
position: absolute;
top: 135px;
display: none;
}
.btn .left,
.btn .right {
width: 30px;
height: 30px;
border-radius: 50%;
float: left;
border: 3px solid skyblue;
}
.btn .right {
float: right;
}
.dian {
width: 90px;
height: 10px;
position: absolute;
top: 260px;
left: 105px;
}
.dian li {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: tan;
margin-right: 10px;
float: left;
}
.dian li:first-child {
background-color: tomato;
}
.dian li:last-child {
margin-right: 0;
}
</style>
</head>
<body>
<div class="box">
<ul class="hello">
<li><img src="巧儿001.jpg" alt=""></li>
<li><img src="巧儿002.jpg" alt=""></li>
<li><img src="巧儿003.jpg" alt=""></li>
<li><img src="张功001.jpg" alt=""></li>
<li><img src="i_98.jpg.png" alt=""></li>
<li><img src="巧儿001.jpg" alt=""></li>
</ul>
<ol class="btn">
<li class="left"></li>
<li class="right"></li>
</ol>
<ol class="dian">
<li index="0"></li>
<li index="1"></li>
<li index="2"></li>
<li index="3"></li>
<li index="4"></li>
</ol>
</div>
<script>
var btn = document.getElementsByClassName("btn")[0];
var box = document.getElementsByClassName("box")[0];
var hello = document.getElementsByClassName("hello")[0];
var lans = document.querySelectorAll(".btn .left,.btn .right,.dian li");
var dianlis = document.querySelectorAll(".dian li");
var timer;
var index = 0;
//鼠标悬停在ele1才会把ele2显示出来
function aaa(ele1, ele2) {
ele1.addEventListener("mouseenter", function() {
ele2.style.display = "block";
});
ele1.addEventListener("mouseleave", function() {
ele2.style.display = "none";
});
}
function easaymove(ele, target, fn) {
clearInterval(timer);
timer = setInterval(function() {
var c = ele.offsetLeft;
var step = Math.ceil(Math.abs(parseInt(target) - c) / 10);
if (c > parseInt(target)) {
step = -step;
}
ele.style.left = c + step + "px";
if (c == parseInt(target)) {
clearInterval(timer);
}
}, 40);
if (Object.prototype.toString.call(fn) == "[object Function]") {
fn();
}
}
function lan() { //函数名叫做懒
if (this.className == "left") {
if (index != 0) {
index--;
} else {
hello.style.left = -1500 + "px";
index = 4;
}
} else if (this.className == "right") {
if (index != 5) {
index++;
} else {
hello.style.left = 0 + "px";
index = 1;
}
} else {
if (index == 5) {
hello.style.left = 0 + "px";
}
index = +this.getAttribute("index");
}
easaymove(hello, -index * 300 + "px", function() {
Array.prototype.forEach.call(dianlis, function(value, i) {
if (i == index) {
value.style.backgroundColor = "tomato";
} else {
value.style.backgroundColor = "tan";
}
});
if (index == 5) {
dianlis[0].style.backgroundColor = "tomato";
}
});
}
for (var i = 0; i < lans.length; i++) {
lans[i].onclick = lan;
}
aaa(box, btn);
</script>
</body>
</html>