一、原生JS
1-1 封装
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="css/index.css">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
</head>
<body>
<div class="content">
<div id="list">
<img src="images/01.png" alt="">
<img src="images/02.png" alt="">
<img src="images/03.png" alt="">
<img src="images/04.png" alt="">
<img src="images/05.png" alt="">
</div>
<button id="prev"></button>
<button id="next"></button>
<div id="btns">
<span class="current"></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</div>
<script>
var prev = document.getElementById("prev")
var next = document.getElementById("next")
var lists = document.querySelectorAll("#list>img")
var icons = document.querySelectorAll("#btns>span")
/* 为了记录下标 */
var index = 0;
next.onclick = function(){
index++;
if(index>4){
index=0;
}
show(index)
}
prev.onclick = function(){
index--;
if(index<0){
index=4;
}
show(index)
}
for(let i=0;i<icons.length;i++){
icons[i].index=i;
icons[i].onclick = function(){
let currentIndex = this.index;
index=currentIndex; //重置下标
show(currentIndex)
}
}
//封装
function show(index){
for(let i=0;i<lists.length;i++){
lists[i].style.display="none"
}
lists[index].style.display = "block"
for(let i=0;i<icons.length;i++){
icons[i].classList.remove("current")
}
icons[index].classList.add("current")
}
</script>
</body>
</html>
1-2 不封装
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="css/index.css">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
</head>
<body>
<div class="content">
<div id="list">
<img src="images/01.png" alt="">
<img src="images/02.png" alt="">
<img src="images/03.png" alt="">
<img src="images/04.png" alt="">
<img src="images/05.png" alt="">
</div>
<button id="prev"></button>
<button id="next"></button>
<div id="btns">
<span class="current"></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</div>
<script>
var prev = document.getElementById("prev")
var next = document.getElementById("next")
var lists = document.querySelectorAll("#list>img")
var icons = document.querySelectorAll("#btns>span")
var index = 0;
next.onclick = function () {
index++;
if (index > 4) {
index = 0;
}
for (let i = 0; i < lists.length; i++) {
lists[i].style.display = "none"
}
lists[index].style.display = "block"
for (let i = 0; i < icons.length; i++) {
icons[i].classList.remove("current")
}
icons[index].classList.add("current")
}
prev.onclick = function () {
index--;
if (index < 0) {
index = 4;
}
for (let i = 0; i < lists.length; i++) {
lists[i].style.display = "none"
}
lists[index].style.display = "block"
for (let i = 0; i < icons.length; i++) {
icons[i].classList.remove("current")
}
icons[index].classList.add("current")
}
for (let i = 0; i < icons.length; i++) {
icons[i].index = i;
icons[i].onclick = function () {
let currentIndex = this.index;
index = currentIndex;
for (let i = 0; i < lists.length; i++) {
lists[i].style.display = "none"
}
lists[index].style.display = "block"
for (let i = 0; i < icons.length; i++) {
icons[i].classList.remove("current")
}
icons[index].classList.add("current")
}
}
</script>
</body>
</html>
二、jQuery实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="css/index.css">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
</head>
<body>
<div class="content">
<div id="list">
<img src="images/01.png" alt="">
<img src="images/02.png" alt="">
<img src="images/03.png" alt="">
<img src="images/04.png" alt="">
<img src="images/05.png" alt="">
</div>
<button id="prev"></button>
<button id="next"></button>
<div id="btns">
<span class="current"></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</div>
<script>
var index = 0;
$("#next").click(function () {
// console.log($("#list>img").is(":animated"))
if (!$("#list>img").is(":animated")) {
index++;
if (index > 4) {
index = 0;
}
animate(index)
}
})
$("#prev").click(function () {
if (!$("#list>img").is(":animated")) {
index--;
if (index < 0) {
index = 4;
}
animate(index)
}
})
$("#btns span").click(function () {
if (!$("#list>img").is(":animated")) {
index = $(this).index()
}
animate(index)
})
function animate(index) {
$("#list img").eq(index).fadeIn(1000).siblings().fadeOut(1000) //fadeIn fadeOut 渐入渐出
$("#btns span").eq(index).addClass("current").siblings().removeClass("current")
}
</script>
</body>
</html>