需求
- 鼠标不在图片上方时,进行自动轮播,并且左右箭头不会显示;
- 当鼠标放在图片上方时,停止轮播,并且左右箭头会显示;
- .图片切换之后,图片中下方的小圆点会同时进行切换,并且点击相应的小圆点可以切换到相应的图片上;
- 点击左右箭头可以进行左右图片的切换;
- 图片上需有介绍的文字,会随图片切换一起进行切换。
效果展示
JS实现轮播图效果
实现原理
- 与TAB选项卡同理给图片绑定active样式使其能够显示在最上层,利用goIndex给图片和对应的小圆点同时加上active
- 设置监听鼠标移动事件,在鼠标移入时清除定时器
- 用getAttribute拿到刚才给li标签绑定的data-index属性,绑定图片index = pointindex,就能实现点击小圆点图片切换了
具体代码
JS部分
<script>
var items = document.querySelectorAll(".item");
var points = document.querySelectorAll(".point")
var left = document.getElementById("leftBtn");
var right = document.getElementById("rightBtn");
var all = document.querySelector(".box")
var index = 0;
var time = 0;
//清除active
var clearActive = function () {
for (i = 0; i < items.length; i++) {
items[i].className = 'item';
}
for (j = 0; j < points.length; j++) {
points[j].className = 'point';
}
}
//改变active
var goIndex = function () {
clearActive();
items[index].className = 'item active';
points[index].className = 'point active'
}
//左按钮事件
var goLeft = function () {
if (index == 0) {
index = 4;
} else {
index--;
}
goIndex();
}
//右按钮事件
var goRight = function () {
if (index < 4) {
index++;
} else {
index = 0;
}
goIndex();
}
//绑定点击事件监听
left.addEventListener('click', function () {
goLeft();
time = 0;//计时器跳转清零
})
right.addEventListener('click', function () {
goRight();
time = 0;//计时器跳转清零
})
for(i = 0;i < points.length;i++){
points[i].addEventListener('click',function(){
var pointIndex = this.getAttribute('data-index')
index = pointIndex;
goIndex();
time = 0;//计时器跳转清零
})
}
var timer;
function play(){
timer = setInterval(() => {
time ++;
if(time == 20 ){
goRight();
time = 0;
}
},100)
}
play();
//移入清除计时器
all.onmousemove = function(){
clearInterval(timer)
}
//移出启动计时器
all.onmouseleave = function(){
play();
}
</script>
完整代码
<!DOCTYPE html>
<html>
<head>
<style>
*{
margin: 0;
padding: 0;
list-style: none;
}
.box {
margin: 30px 100px;
width: 1350px;
height: 800px;
position: relative;
}
.list {
width: 1350px;
height: 800px;
position: relative;
padding-left: 0px;
}
.item {
width: 100%;
height: 450px;
padding-top: 350px;
position: absolute;
left: 0;
color: rgb(36, 35, 79);
font-size: 50px;
text-align: center;
transition: all .8s;
}
.item:nth-child(1) {
background-color: palevioletred;
}
.item:nth-child(2) {
background-color: yellowgreen;
}
.item:nth-child(3) {
background-color: khaki;
}
.item:nth-child(4) {
background-color: pink;
}
.item:nth-child(5) {
background-color: orange;
}
.item.active {
z-index: 10;
}
.btn {
width: 50px;
height: 200px;
top: 280px;
position: absolute;
opacity: 0.5;
z-index: 100;
}
#leftBtn {
left: 0px;
}
#rightBtn {
right: 0px;
}
.pointList {
list-style: none;
padding-right: 340px;
position: absolute;
right: 20px;
bottom: 20px;
z-index: 200;
}
.point {
width: 10px;
height: 10px;
background-color: antiquewhite;
border-radius: 100%;
float: left;
margin-right: 80px;
border-style: solid;
border-width: 2px;
border-color: #b9b9b9;
cursor: pointer;
}
.point.active{
background-color: rgb(92, 92, 92);
}
</style>
</head>
<body>
<div class="box">
<ul class="list">
<li class="item active">first slide</li>
<li class="item">second slide</li>
<li class="item">third slide</li>
<li class="item">fourth slide</li>
<li class="item">fifth slide</li>
</ul>
<ul class="pointList">
<li class="point active" data-index = 0></li>
<li class="point" data-index = 1></li>
<li class="point" data-index = 2></li>
<li class="point" data-index = 3></li>
<li class="point" data-index = 4></li>
</ul>
<button class="btn" id="leftBtn"> < </button>
<button class="btn" id="rightBtn"> > </button>
</div>
<script>
var items = document.querySelectorAll(".item");
var points = document.querySelectorAll(".point")
var left = document.getElementById("leftBtn");
var right = document.getElementById("rightBtn");
var all = document.querySelector(".box")
var index = 0;
var time = 0;
//清除active
var clearActive = function () {
for (i = 0; i < items.length; i++) {
items[i].className = 'item';
}
for (j = 0; j < points.length; j++) {
points[j].className = 'point';
}
}
//改变active
var goIndex = function () {
clearActive();
items[index].className = 'item active';
points[index].className = 'point active'
}
//左按钮事件
var goLeft = function () {
if (index == 0) {
index = 4;
} else {
index--;
}
goIndex();
}
//右按钮事件
var goRight = function () {
if (index < 4) {
index++;
} else {
index = 0;
}
goIndex();
}
//绑定点击事件监听
left.addEventListener('click', function () {
goLeft();
time = 0;//计时器跳转清零
})
right.addEventListener('click', function () {
goRight();
time = 0;//计时器跳转清零
})
for(i = 0;i < points.length;i++){
points[i].addEventListener('click',function(){
var pointIndex = this.getAttribute('data-index')
index = pointIndex;
goIndex();
time = 0;//计时器跳转清零
})
}
var timer;
function play(){
timer = setInterval(() => {
time ++;
if(time == 20 ){
goRight();
time = 0;
}
},100)
}
play();
//移入清除计时器
all.onmousemove = function(){
clearInterval(timer)
}
//移出启动计时器
all.onmouseleave = function(){
play();
}
</script>
</body>
</html>