简单实现图片的自动轮播,需要的朋友可以参考一下。
html代码:
<div class="cut">
<img class="img" src="./images/1.webp" alt="">
<div class="contral prev"><</div>
<div class="contral next">></div>
<ul class="btn"><!-- 圆点 -->
<li class="active"></li>
<li></li>
<li></li>
</ul>
</div>
css代码:
<style type="text/css">
.cut {
position: relative;
width: 590px;
height: 470px;
background-color: antiquewhite;
margin: 0 auto;
}
.contral {
position: absolute;
top: 50%;
width: 24px;
height: 40px;
line-height: 40px;
color: #fff;
text-align: center;
font-size: 14px;
background-color: rgba(0, 0, 0, .2);
cursor: pointer;
}
.prev {
left: 0;
}
.next {
right: 0;
}
.btn {
position: absolute;
bottom: 20px;
left: 30px;
width: 150px;
height: 15px;
}
.cut>ul>li {
float: left;
width: 10px;
height: 10px;
margin-right: 4px;
border: 1px solid rgba(0, 0, 0, .05);
border-radius: 50%;
background-color: rgba(255, 255, 255, 0.4);
list-style: none;
}
.cut .active {
position: relative;
top: -1px;
width: 10px;
height: 10px;
border:2px solid rgba(0, 0, 0, .1) ;
background-color: #fff;
}
</style>
js代码:
<script type="text/javascript">
let img = document.querySelector('.img');
let prev = document.querySelector('.prev');
let next = document.querySelector('.next');
let cut = document.querySelector('.cut');
let lis = document.querySelectorAll('.btn li');
let imgAll = ['1.webp', '2.webp', '3.webp'];
let count = 0;
function cutt() {
img.src = './images/' + imgAll[count];
for (let i = 0; i < lis.length; i++) {
lis[i].className = '';
}
lis[count].className = 'active';
}
let timer = setInterval(function () {
count++;
if (count > imgAll.length - 1) {
count = 0;
}
cutt();
}, 1500);
next.onclick = function () {
count++;
if (count > imgAll.length - 1) {
count = 0;
}
cutt();
}
prev.onclick = function () {
count--;
if (count < 0) {
count = imgAll.length - 1;
}
cutt();
}
cut.onmouseover = function () {
clearInterval(timer);
}
cut.onmouseout = function () {
timer = setInterval(function () {
count++;
if (count > imgAll.length - 1) {
count = 0;
}
cutt();
}, 1500);
}
for (let i = 0; i < lis.length; i++) {
lis[i].onmouseover = () => {
count = i;
cutt();
};
}
</script>
效果图:文章来源:https://www.toymoban.com/news/detail-519856.html
文章来源地址https://www.toymoban.com/news/detail-519856.html
到了这里,关于实现自动轮播图(html+js)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!