简单的轮播图效果
<body>
<div id="slider" class="slider">
<ul>
<li class="ax">
<a href="#">
<img src="./img/01.jpg" alt="" />
<p>这是第1张图片的文字说明</p>
</a>
</li>
<li>
<a href="#">
<img src="./img/02.jpg" alt="" />
<p>这是第2张图片的文字说明</p>
</a>
</li>
<li>
<a href="#">
<img src="./img/04.jpg" alt="" />
<p>这是第3张图片的文字说明</p>
</a>
</li>
<li>
<a href="#">
<img src="./img/4.jpg" alt="" />
<p>这是第4张图片的文字说明</p>
</a>
</li>
<li>
<a href="#">
<img src="./img/5.jpg" alt="" />
<p>这是第5张图片的文字说明</p>
</a>
</li>
<li>
<a href="#">
<img src="./img/6.jpg" alt="" />
<p>这是第6张图片的文字说明</p>
</a>
</li>
</ul>
<div class="arrow">
<span id="arrow-left" class="arrow-left"><</span>
<span id="arrow-right" class="arrow-right">></span>
</div>
</div>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
img {
display: block;
}
.slider {
height: 470px;
width: 590px;
margin: 100px auto;
position: relative;
overflow: hidden;
}
.slider li {
position: absolute;
display: none;
}
.slider li:first-child {
display: block;
}
.slider li p {
position: absolute;
width: 100%;
padding: 10px 0;
text-indent: 2em;
background-color: rgba(0, 0, 0, 0.6);
bottom: 0;
left: 0;
font-size: 18px;
transform: translate3d(0, 100%, 0);
transition: all 0.8s;
}
.slider li.ax p {
transform: translate3d(0, 0, 0);
}
.slider li a {
color: #fff;
}
.arrow {
display: none;
}
.slider:hover .arrow {
display: block;
}
.arrow-left,
.arrow-right {
font-family: 'SimSun', '宋体';
width: 30px;
height: 60px;
background-color: rgba(0, 0, 0, 0.1);
position: absolute;
top: 50%;
margin-top: -30px;
cursor: pointer;
text-align: center;
line-height: 60px;
color: #fff;
font-weight: 700;
font-size: 30px;
}
.arrow-left:hover,
.arrow-right:hover {
background-color: rgba(0, 0, 0, 0.5);
}
.arrow-left {
left: 0;
}
.arrow-right {
right: 0;
}
</style>
<script>
var spanLeft = document.getElementById('arrow-left')
var spanRight = document.getElementById('arrow-right')
var divobj = document.getElementById('slider')
var liobj = divobj.firstElementChild.children
var times = ''
var index = 0
var prev = 0
spanLeft.onclick = function () {
prev = index
index--
if (index < 0) index = liobj.length - 1
change()
}
spanRight.onclick = function () {
prev = index
index++
if (index == liobj.length) index = 0
change()
}
function outplay() {
times = setInterval(() => {
spanRight.onclick()
}, 2000)
}
outplay()
divobj.onmouseover = function () {
clearInterval(times)
}
divobj.onmouseout = function () {
outplay()
}
function change() {
liobj[index].classList.add('ax')
liobj[index].style.display = 'block'
liobj[index].style.opacity = 1
liobj[prev].classList.remove('ax')
liobj[prev].style.opacity = 0
}
</script>