利用透明度来实现轮播图效果
话不多说直接上代码:
<!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>
* {
padding: 0;
margin: 0;
}
*::selection {
background: none;
}
div {
width: 800px;
height: 500px;
margin: 50px auto;
position: relative;
}
ul li {
list-style: none;
opacity: 0;
position: absolute;
transition: all .8s;
}
ul li:nth-child(1) {
opacity: 1;
}
ol {
position: absolute;
bottom: 20px;
left: 35%;
}
ol li {
list-style: none;
width: 20px;
height: 20px;
border: 1px solid white;
line-height: 20px;
float: left;
margin-right: 10px;
text-align: center;
border-radius: 50%;
cursor: pointer;
}
.yellow {
background-color: yellow;
}
img {
width: 800px;
height: 500px;
}
.prve,
.next {
position: absolute;
width: 50px;
height: 20px;
background-color: rgba(1, 0, 0, .7);
top: 50%;
margin-top: -25px;
color: white;
text-align: center;
line-height: 20px;
display: none;
}
.next {
right: 0;
}
.prve:hover,
.next:hover {
cursor: pointer;
background-color: rgba(1, 0, 0, .5);
}
.prve:active,
.next:active {
background-color: rgba(1, 0, 0, .3);
}
</style>
</head>
<body>
<div>
<ul>
<li><img src="./img/f1.jpg" alt=""></li>
<li><img src="./img/f2.jpg" alt=""></li>
<li><img src="./img/f3.jpg" alt=""></li>
<li><img src="./img/f4.jpg" alt=""></li>
<li><img src="./img/f5.jpg" alt=""></li>
</ul>
<ol>
<li class="yellow">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ol>
<div class="prve">prve</div>
<div class="next">next</div>
</div>
<script>
var prve = document.getElementsByClassName('prve')[0]
var next = document.getElementsByClassName('next')[0]
var ullis = document.getElementsByTagName('ul')[0].getElementsByTagName('li')
var ollis = document.getElementsByTagName('ol')[0].getElementsByTagName('li')
var div = document.getElementsByTagName('div')[0]
var index = 0
div.onmouseenter = function () {
prve.style.display = 'block'
next.style.display = 'block'
clearInterval(timer)
}
div.onmouseleave = function () {
prve.style.display = 'none'
next.style.display = 'none'
timer = setInterval(function () {
next.click()
}, 1000)
}
next.onclick = function () {
index++
if (index == ullis.length) {
index = 0
}
for (var i = 0; i < ullis.length; i++) {
ullis[i].style.opacity = 0
ollis[i].className = ''
}
ullis[index].style.opacity = 1
ollis[index].className = 'yellow'
}
prve.onclick = function () {
if (index == 0) {
index = ullis.length
}
index--
for (var i = 0; i < ullis.length; i++) {
ullis[i].style.opacity = 0
ollis[i].className = ''
}
ullis[index].style.opacity = 1
ollis[index].className = 'yellow'
}
for (var i = 0; i < ollis.length; i++) {
ollis[i].setAttribute('index', i)
ollis[i].onclick = function () {
for (var i = 0; i < ollis.length; i++) {
index = this.getAttribute('index')
ullis[index].style.opacity = 1
ollis[i].className = ''
}
this.className = 'yellow'
}
}
var timer = setInterval(function () {
next.click()
}, 1000)
</script>
</body>
</html>