演示效果
轮播图代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.banner {
width: 500px;
height: 300px;
position: relative;
margin: 100px auto;
}
.banner .wrap {
width: 100%;
}
.banner .wrap .item {
width: 100%;
}
.item img {
width: 500px;
height: 300px;
vertical-align: top;
position: absolute;
}
.div1 {
position: absolute;
left: 0;
top: 50%;
transform: translatey(-50%);
cursor: pointer;
width: 41px;
height: 69px;
font-size: 30px;
line-height: 70px;
text-align: center;
color: #D6D8D4;
background-color: rgba(0, 0, 0, 0.3);
}
.div2 {
position: absolute;
right: 0;
top: 50%;
transform: translatey(-50%);
cursor: pointer;
width: 41px;
height: 69px;
font-size: 30px;
line-height: 70px;
text-align: center;
color: #D6D8D4;
background-color: rgba(0, 0, 0, 0.3);
}
.pagenation {
position: absolute;
left: 50%;
transform: translateX(-50%);
display: flex;
bottom: 40px;
}
.pagenation>div {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: white;
margin-right: 10px;
cursor: pointer;
}
.pagenation>div:last-child {
margin-right: 0;
}
</style>
</head>
<body>
<div class="banner">
<div class="warp">
<div class="item"><img src="./img/1.jpg" alt=""></div>
<div class="item"><img src="./img/2.jpg" alt=""></div>
<div class="item"><img src="./img/3.jpg" alt=""></div>
<div class="item"><img src="./img/4.jpg" alt=""></div>
</div>
<div class="div1">
<</div> <div class="div2 ">>
</div>
<div class="pagenation">
<div id="pagenation-item0"></div>
<div id="pagenation-item1"></div>
<div id="pagenation-item2"></div>
<div id="pagenation-item3"></div>
</div>
</div>
<script>
var index = 0;
var banner = document.getElementsByClassName("banner")[0];
var itemList = document.getElementsByClassName("item");
var pagenationList = document.querySelectorAll(".pagenation>div");
var pagenation = document.querySelector(".pagenation");
itemList[0].style.opacity = 1;
pagenationList[0].style.background = "blue"
var up = document.getElementsByClassName("div1")[0];
var next = document.getElementsByClassName("div2")[0];
function nextFn() {
index = index >= itemList.length - 1 ? 0 : ++index;
for (var i = 0; i < itemList.length; i++) {
itemList[i].style.opacity = 0;
pagenationList[i].style.background = "white "
}
itemList[index].style.transition = "opacity 1s ease .2s"
itemList[index].style.opacity = 1;
pagenationList[index].style.background = "blue"
}
next.onclick = nextFn;
up.onclick = function () {
index = index <= 0 ? itemList.length - 1 : --index;
for (var i = 0; i < itemList.length; i++) {
itemList[i].style.opacity = 0;
pagenationList[i].style.background = "white"
}
itemList[index].style.transition = "opacity 1s ease .2s"
itemList[index].style.opacity = 1;
pagenationList[index].style.background = "blue"
}
var t1 = setInterval(nextFn, 2000)
banner.onmouseover = function () {
clearInterval(t1);
}
banner.onmouseout = function () {
t1 = setInterval(nextFn, 2000)
}
pagenation.onclick = function (event) {
event = window.event || event
console.log(event);
if (event.target.className == "pagenation") {
console.log("点击的是父元素");
} else {
var id = event.target.id;
var photoIndex = null;
for (var i = 0; i < pagenationList.length; i++) {
pagenationList[i].style.background = "white"
if (id.indexOf(i) >= 0) {
photoIndex = i;
}
}
event.target.style.background = "blue"
index = photoIndex;
for (var i = 0; i < itemList.length; i++) {
itemList[i].style.opacity = 0;
}
itemList[index].style.transition = "opacity 1s ease .2s"
itemList[photoIndex].style.opacity = 1;
}
}
</script>
</body>
</html>
|