第一种:直接切换隐藏的。
思路:让所有的图片进行绝对定位,然后默认显示第一张,然后通过修改display:block属性,切换隐藏。
<!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>
img {
width: 300px;
height: 150px;
}
ul {
list-style: none;
display: flex;
flex-direction: row;
margin: 0;
padding: 0;
/* overflow: hidden; */
}
ul li {
position: absolute;
display: none;
}
.item1 {
display: block;
}
#box {
width: 300px;
height: 150px;
/* overflow: hidden; */
padding: 0;
position: relative;
left: 500px;
}
.indicator {
display: flex;
flex-direction: row;
position: relative;
top: 130px;
left: 90px;
}
.in-item:first-of-type {
background-color: red;
}
.in-item {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: rgb(230, 225, 225);
margin: 5px;
cursor: pointer;
}
.left {
width: 30px;
height: 50px;
font-size: 30px;
position: relative;
top: 30px;
z-index: 99;
opacity: 0.7;
background-color: rgb(219, 219, 219);
cursor: pointer;
}
.right {
width: 30px;
height: 50px;
font-size: 30px;
position: relative;
z-index: 99;
left: 270px;
top: -20px;
opacity: 0.7;
background-color: rgb(219, 219, 219);
cursor: pointer;
}
</style>
</head>
<body>
<div id="box">
<ul>
<li class="item1"><img src="https://t7.baidu.com/it/u=3711280171,2587919442&fm=193&f=GIF" alt=""></li>
<li class="item2"><img src="https://t7.baidu.com/it/u=2537853557,2209379599&fm=193&f=GIF" alt=""></li>
<li class="item3"><img src="https://t7.baidu.com/it/u=522964964,1058451158&fm=193&f=GIF" alt=""></li>
<li class="item4"><img src="https://t7.baidu.com/it/u=631924955,996383527&fm=193&f=GIF" alt=""></li>
<li class="item5"><img src="https://t7.baidu.com/it/u=1459961089,2797408733&fm=193&f=GIF" alt=""></li>
</ul>
<div class="indicator">
<div class="in-item"></div>
<div class="in-item"></div>
<div class="in-item"></div>
<div class="in-item"></div>
<div class="in-item"></div>
</div>
<div class="left" onclick="back()"></div>
<div class="right" onclick="next()">></div>
</div>
<script>
let lis = document.querySelectorAll("li");
let indicators = document.querySelectorAll(".in-item");
let box = document.getElementById("box");
let left = document.getElementsByClassName("left")
left[0].innerText = "<"
let timer = null;//保存定时器的变量
box.addEventListener("mouseover", stop)//监听鼠标移入
box.addEventListener("mouseout", begin)//监听鼠标移除
for (let i = 0; i < indicators.length; i++) {//用循环给每个指示器添加点击事件
indicators[i].addEventListener("click", function () { toItem(i) })//用一个匿名函数包裹,实现参数传递
}
let index = 0;//初始化索引为0
timer = setInterval(init, 2000)
//初始化函数,然后图片自动轮播
function init() {
if (index == 4) {
clearStyle()
index = 0;
show(index)
} else {
clearStyle()
++index
show(index)
}
};
//清除所有的样式
function clearStyle() {
for (let i = 0; i < lis.length; i++) {
lis[i].style.display = "none";
indicators[i].style.backgroundColor = "rgb(230, 225, 225)"
}
};
//切换指示器和图片的显示
function show(index) {
lis[index].style.display = "block"
indicators[index].style.backgroundColor = "red"
};
//点击指示器显示到该图片
function toItem(ind) {
index = ind;
clearStyle();
show(ind)
}
//鼠标移除清除定时器
function stop() {
clearInterval(timer)
};
//鼠标移除开启定时器
function begin() {
timer = setInterval(init, 2000)
};
//点击切换上一个
function back() {
if (index == 0) return;
clearStyle()
--index;
show(index)
}
//点击切换下一个
function next() {
if (index == 4) return;
clearStyle()
++index;
show(index)
}
</script>
</body>
</html>
第二种:可以实现拖拽,可以通过滑动图片进行切换图片。用一个大的ul便签,然后ul大标签实现绝对定位,然后修改left进行滑动和配合定时器,然后用一个大的DIV进行包裹,设置overflow: hidden;CSS属性,然超出DIV宽度的内容隐藏。
代码:
<!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>
img {
width: 300px;
height: 150px;
}
ul {
position: absolute;
list-style: none;
display: flex;
flex-direction: row;
margin: 0;
padding: 0;
}
.item1 {
display: block;
}
#box {
width: 300px;
height: 150px;
overflow: hidden;
padding: 0;
position: relative;
left: 500px;
}
.indicator {
display: flex;
flex-direction: row;
position: absolute;
top: 130px;
left: 100px;
}
.in-item:first-of-type {
background-color: red;
}
.in-item {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: rgb(230, 225, 225);
margin: 5px;
cursor: pointer;
}
.left {
width: 30px;
height: 50px;
font-size: 30px;
position: absolute;
top: 50px;
z-index: 99;
opacity: 0.7;
background-color: rgb(219, 219, 219);
cursor: pointer;
}
.right {
width: 30px;
height: 50px;
font-size: 30px;
position: absolute;
z-index: 99;
left: 270px;
top: 50px;
opacity: 0.7;
background-color: rgb(219, 219, 219);
cursor: pointer;
}
</style>
</head>
<body>
<div id="box">
<ul>
<li class="item1"><img src="https://t7.baidu.com/it/u=3711280171,2587919442&fm=193&f=GIF" alt=""></li>
<li class="item2"><img src="https://t7.baidu.com/it/u=2537853557,2209379599&fm=193&f=GIF" alt=""></li>
<li class="item3"><img src="https://t7.baidu.com/it/u=522964964,1058451158&fm=193&f=GIF" alt=""></li>
<li class="item4"><img src="https://t7.baidu.com/it/u=631924955,996383527&fm=193&f=GIF" alt=""></li>
<li class="item5"><img src="https://t7.baidu.com/it/u=1459961089,2797408733&fm=193&f=GIF" alt=""></li>
</ul>
<div class="indicator">
<div class="in-item"></div>
<div class="in-item"></div>
<div class="in-item"></div>
<div class="in-item"></div>
<div class="in-item"></div>
</div>
<div class="left" onclick="back()"></div>
<div class="right" onclick="next()">></div>
</div>
<script>
let status, dragX, start;
let lis = document.querySelectorAll("li");
let ul = document.querySelector("ul")
let indicators = document.querySelectorAll(".in-item");
indicators.forEach((item, ind) => {
item.addEventListener("click", function () { toItem(ind) })
})
let box = document.getElementById("box");
let left = document.getElementsByClassName("left")
let index = 0, step = 0;
left[0].innerText = "<"
let timer1 = null, timer2 = null, timer3 = null, timer4 = null;//保存定时器的变量
box.addEventListener("mouseover", stop)//监听鼠标移入
box.addEventListener("mouseout", begin)//监听鼠标移除
timer1 = setInterval(init, 3000)//调用定时器每3s切换图片
//初始化让图片自动动起来
function init() {
if (index == 4) {
clearStyle()//调用函数清除指示器的样式
index = 0;
showIndicators(index)//显示第几个指示器
timer3 = setInterval(toFirst, 10)//到最后一个就回到最前面
} else {
clearStyle()
index++
showIndicators(index)
timer2 = setInterval(move, 10)
}
};
//清除指示器样式
function clearStyle() {
for (let i = 0; i < indicators.length; i++) {
indicators[i].style.backgroundColor = "rgb(230, 225, 225)";
}
};
//回到第一个
function toFirst() {
if (step == 0) {
clearInterval(timer3);
return
}
step += 20;
ul.style.left = step + "px";
};
//每个图片自动的移动
function move() {
if (step == -index * 300 && step !== 0) {
clearInterval(timer2)
return
}
step -= 5;
ul.style.left = step + "px"
};
//显示某个指示器
function showIndicators(index) {
indicators[index].style.backgroundColor = "red"
};
//点击下一个
function next() {
if (index == 4) return
clearStyle();
index++;
showIndicators(index);
step = -(index - 1) * 300;
timer4 = setInterval(() => {
if (step == -index * 300) {
clearInterval(timer4);
return
}
step -= 10;
ul.style.left = step + "px";
}, 10)
};
//返回上一个
function back() {
if (index == 0) return;
clearStyle();
index--;
showIndicators(index);
step = -(index + 1) * 300;
timer4 = setInterval(() => {
if (step == -index * 300) {
clearInterval(timer4);
return
}
step += 10;
ul.style.left = step + "px";
}, 10)
}
//移入就清除定时器
function stop() {
clearInterval(timer1);
};
///鼠标移出开启定时器
function begin() {
timer1 = setInterval(init, 3000)
};
//点击指示器直接跳到该图片
function toItem(ind) {
clearStyle();
showIndicators(ind);
index = ind
step = -ind * 300;
ul.style.left = step + "px";
};
//监听鼠标的按下、移动、鼠标松开,实现鼠标按下的拖拽
box.addEventListener("mousedown", down);
box.addEventListener("mouseup", up);
box.addEventListener("mousemove", mouseMove)
function down(e) {
status = 1;//鼠标按下就修改这个变量,
start = e.clientX;
};
function up() {
if (dragX < 50 && dragX > -50) {//判断鼠标拖拽的距离,距离太小就不进行滑动
ul.style.left = step + "px";
status = 0
return
}
//这是回去上一张
if (dragX > 50) {
back()
} else {//回去下一张
if (dragX < -50) {
next()
}
}
status = 0//起来就还原该变量
};
function mouseMove(e) {
e.preventDefault();//阻止浏览器默认的拖拽
//鼠标的移动首先判断该变量,如果是1说明按下了没松开,就可以就行拖拽
if (status == 1) {
dragX = e.clientX - start
if (step + dragX <= -1200) return;
if (step + dragX >= 0) return
ul.style.left = step + dragX + "px";
}
}
</script>
</body>
</html>