简单的显示隐藏(带小圆圈和左右按钮,js写)
<!DOCTYPE html>
<html lang = "en" >
<head >
<meta charset = "UTF-8" >
<meta content = "IE=edge" http-equiv = "X-UA-Compatible" >
<meta content = "width=device-width, initial-scale=1.0" name = "viewport" >
<title >Document</title >
<script src = "https:
<style >
* {
padding: 0;
margin: 0;
list-style: none;
text-decoration: none;
}
section {
width: 100%;
height: 500px;
position: relative;
}
main {
width: 300px;
height: 200px;
border: 1px solid;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
div {
width: 300px;
height: 200px;
display: none;
}
.xs {
display: block;
}
main > a:nth-child(1) {
position: absolute;
left: 0;
top: 50%;
}
main > a:nth-child(2) {
position: absolute;
right: 0;
top: 50%;
}
ul {
display: flex;
position: absolute;
bottom: 0;
}
li {
width: 20px;
height: 20px;
border-radius: 50%;
border: 1px solid;
background-color: #ccc;
}
.red {
background-color: red;
}
a {
width: 20px;
height: 20px;
opacity: 0;
color: white;
}
div:nth-of-type(1) {
background-color: khaki;
}
h2{
color: white;
}
div:nth-of-type(2) {
background-color: deepskyblue;
}
div:nth-of-type(3) {
background-color: blue;
}
div:nth-of-type(4) {
background-color: yellow;
}
div:nth-of-type(5) {
background-color: pink;
}
div:nth-of-type(6) {
background-color: green;
}
div:nth-of-type(7) {
background-color: orange;
}
div:nth-of-type(8){
background-color: darkviolet;
}
</style >
</head >
<body >
<section >
<main >
<a href = "javascript:;" ><</a >
<a href = "javascript:;" >></a >
<div class = "xs" ><h2 >第一张图片信息</h2 ></div >
<div ><h2 >第二张图片信息</h2 ></div >
<div ><h2 >第三张图片信息</h2 ></div >
<div ><h2 >第四张图片信息</h2 ></div >
<div ><h2 >第五张图片信息</h2 ></div >
<div ><h2 >第六张图片信息</h2 ></div >
<div ><h2 >第七张图片信息</h2 ></div >
<div ><h2 >第八张图片信息</h2 ></div >
<ul >
</ul >
</main >
</section >
<script >
var zb = document.querySelector("main>a:nth-child(1)")
var a = document.querySelectorAll("a")
var yb = document.querySelector("main>a:nth-child(2)")
var main = document.querySelector("main")
var div = document.querySelectorAll("div")
var ul = document.querySelector("ul")
let index = 0;
var xj = 0;
function tupian() {
document.querySelector(".xs").classList.remove("xs")
div[index].classList.add("xs")
}
for (let i = 0; i < div.length; i++) {
var li = document.createElement("li")
ul.appendChild(li)
li.setAttribute('index', i);
li.addEventListener("click", function () {
for (var j = 0; j < ul.children.length; j++) {
ul.children[j].className = " "
}
this.className = "red"
index = this.getAttribute("index")
xj = index
tupian()
})
}
ul.children[0].className = "red"
zb.addEventListener("click", function () {
index--;
xj--;
if (index < 0) {
index = div.length - 1
xj = div.length - 1
}
for (var j = 0; j < ul.children.length; j++) {
ul.children[j].className = " "
}
ul.children[xj].className = "red"
tupian()
})
yb.addEventListener("click", function () {
index++;
xj++;
if (index > div.length - 1) {
index = 0;
xj = 0
}
for (var j = 0; j < ul.children.length; j++) {
ul.children[j].className = " "
}
ul.children[xj].className = "red"
tupian()
})
let temer = setInterval(function () {
yb.click()
}, 1000)
main.addEventListener("mouseover", function () {
zb.style.opacity = "1"
yb.style.opacity = "1"
clearInterval(temer)
})
main.addEventListener("mouseout", function () {
zb.style.opacity = "0"
yb.style.opacity = "0"
temer = setInterval(function () {
yb.click()
}, 1000)
})
</script >
</body >
</html >
简单的轮播图(无左右按钮和小圆圈,简单切换隐藏显示,jq写的)
<!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>
<link rel="stylesheet" href="">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
}
.swiper {
width: 400px;
height: 300px;
position: relative;
}
.swiper img {
width: 100%;
height: 100%;
position: absolute;
opacity: 0;
transition: all 1s;
}
img.activeImg {
opacity: 1;
}
</style>
</head>
<body>
<main class="swiper">
<img class="activeImg" src="../../../01-JQuary练习/jq学习/images/冬裙.jpg" alt="">
<img src="../../../01-JQuary练习/jq学习/images/呢大衣.jpg" alt="">
<img src="../../../01-JQuary练习/jq学习/images/围巾.jpg" alt="">
<img src="../../../01-JQuary练习/jq学习/images/女包.jpg" alt="">
</main>
<script>
let imgIndex = 0;
let timer = null;
function play() {
$(".swiper img").eq(imgIndex).removeClass("activeImg");
imgIndex++;
if (imgIndex == $(".swiper img").length) {
imgIndex = 0;
}
$(".swiper img").eq(imgIndex).addClass("activeImg");
}
timer = setInterval(play, 1000)
</script>
</body>
</html>
下面这个是jQuery写的无缝轮播图,非隐藏显示,带左右按钮和圆圈
<!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>
<link rel="stylesheet" href="">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
ul,
ol {
list-style: none;
}
.box {
width: 800px;
height: 500px;
margin: 0 auto;
position: relative;
overflow: hidden;
box-shadow: 2px 2px 15px #333;
}
ul {
height: 100%;
position: absolute;
display: flex;
}
li,
li>img {
width: 800px;
height: 100%;
}
button {
width: 70px;
height: 50px;
position: absolute;
z-index: 10;
top: calc(50% - 25px);
font-size: 16px;
opacity: 0;
transition: all .5s;
}
.right {
right: 0;
}
.box:hover button {
opacity: 1;
}
ol {
width: 120px;
margin: 0 auto;
position: absolute;
bottom: 20px;
left: calc(50% - 60px);
display: flex;
justify-content: space-between;
}
ol li {
width: 10px;
height: 10px;
background-color: gray;
border-radius: 5px;
}
.ac {
width: 25px;
background-color: blue;
transition: all 1s;
}
</style>
</head>
<body>
<div class="box">
<ul>
<li><img class="activeImg" src="../../../01-JQuary练习/jq学习/images/冬裙.jpg" alt=""></li>
<li><img src="../../../01-JQuary练习/jq学习/images/呢大衣.jpg" alt=""></li>
<li><img src="../../../01-JQuary练习/jq学习/images/围巾.jpg" alt=""></li>
<li><img src="../../../01-JQuary练习/jq学习/images/女包.jpg" alt=""></li>
<li><img src="../../../01-JQuary练习/jq学习/images/棉服.jpg" alt=""></li>
<li><img class="activeImg" src="../../../01-JQuary练习/jq学习/images/冬裙.jpg" alt=""></li>
</ul>
<button class="left">left</button>
<button class="right">right</button>
<ol class="dots">
<li class="ac"></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ol>
</div>
<script>
let width = $("img").width();
console.log(width);
let index = 0;
let imglen = $("img").length - 1;
console.log(imglen);
let timer;
let flag = false;
function dotCHange(index) {
$("ol li").removeClass("ac");
$("ol li").eq(index).addClass("ac")
}
function play() {
if (flag) {
return;
}
flag = true;
index++;
if (index == imglen) {
$("ul").animate({
left: -index * width
}, 1000, function () {
$("ul").css("left", 0)
flag = false;
})
index = 0
} else {
$("ul").animate({
left: -index * width
}, 1000, function () {
flag = false;
})
}
dotCHange(index)
}
timer = setInterval(play, 1000)
$(".box").hover(function () {
clearInterval(timer);
timer = null;
}, function () {
timer = setInterval(play, 1000)
})
$("ol li").click(function () {
let dotidx = $(this).index();
index = dotidx;
console.log(dotidx);
$("ul").stop().animate({
left: -dotidx * width
}, 1000)
dotCHange(dotidx)
})
$(".right").on("click", function () {
play();
})
$(".left").on("click", function () {
if (flag) {
return;
}
flag = true;
index--;
if (index < 0) {
index = imglen - 1;
$("ul").css("left", -imglen * width);
$("ul").animate({
left: -index * width
}, 1000, function () {
flag = false
})
} else {
$("ul").animate({
left: -index * width
}, 1000, function () {
flag = false
})
}
dotCHange(index)
})
</script>
</body>
</html>