<!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>
*{
margin: 0;
padding: 0;
}
ul,ol,li{
list-style: none;
}
.banner{
width: 600px;
height: 400px;
border: 10px solid #333;
margin: 100px auto;
position: relative;
overflow: hidden;
}
.banner>ul{
width: 500%;
overflow: hidden;
height: 100%;
position: absolute;
left: 0;
top: 0;
}
.banner>ul>li{
width: 600px;
height: 100%;
float: left;
font-size: 100px;
color: #fff;
line-height: 400px;
text-align: center;
}
.banner>ol{
width: 175px;
height: 25px;
background-color: rgba(0, 0, 0, .5);
border-radius: 15px;
position: absolute;
left: 50%;
bottom: 30px;
margin-left: -93px;
padding-top: 5px;
padding-right: 15px;
}
.banner>ol>li{
width: 20px;
height: 20px;
background-color: #fff;
border-radius: 50%;
float: left;
margin-left: 15px;
cursor: pointer;
}
.banner>ol>li.active{
background-color: orange;
}
.banner>div{
width: 100%;
height: 60px;
position: absolute;
left: 0;
top: 50%;
margin-top: -30px;
}
.banner>div>p{
float: left;
width: 40px;
height: 100%;
background-color: rgba(0, 0, 0, .5);
line-height: 60px;
text-align: center;
font-size: 30px;
columns: #fff;
cursor: pointer;
}
.banner>div>p:last-child{
float: right;
}
</style>
</head>
<body>
<div class="banner">
<ul class="img_box">
<li style="background-color: pink;">1</li>
<li style="background-color: orange;">2</li>
<li style="background-color: lightgreen;">3</li>
<li style="background-color: skyblue;">4</li>
<li style="background-color: purple;">5</li>
</ul>
<!-- 焦点盒子 -->
<ol class="point_box"></ol>
<!-- 左右切换盒子 -->
<div>
<p class="left"><</p>
<p class="right">></p>
</div>
</div>
<script src="../jquery/jquery.min.js"></script>
<script>
let timer=0
let index=1
let flag=true
// 生成焦点
setPoint()
function setPoint(){
const num=$('.img_box>li').length
let str=''
for(var i=0;i<num;i++){
str+=`<li class="${i===0?'active':''}"></li>`
}
$('.point_box')
.html(str)
.css('width',num*35)
.css('marginLeft',$('.point_box').width()/-2)
}
// 复制元素
copyEle()
function copyEle(){
const first=$('.img_box').children().first().clone()
const last=$('.img_box').children().last().clone()
$('.img_box')
.append(first)
.prepend(last)
.css({
width:$('.img_box>li').length*100+'%',
left:$('.banner').width()*-1
})
}
// 自动轮播
autoPlay()
function autoPlay(){
timer=setInterval(()=>{
index++
$('.img_box').animate({left:-index*$('.banner').width()},500,moveEnd)
},1000)
}
// 运动结束
function moveEnd(){
if(index>=$('.img_box>li').length-1){
index=1
$('.img_box').css('left',-index*$('.banner').width())
}
if(index<=0){
index=$('.img_box>li').length-2
$('.img_box').css('left',-index*$('.banner').width())
}
$('.point_box>li').removeClass('active').eq(index-1).addClass('active')
flag=true
}
// 移入移出与点击事件
bindEvent()
function bindEvent(){
$('.banner')
.mouseenter(()=>clearInterval(timer))
.mouseleave(()=>autoPlay())
.on('click','.left',()=>{
if(!flag)return
flag=false
index--
$('.img_box').animate({left:-index*$('.banner').width()},500,moveEnd)
})
.on('click','.right',()=>{
if(!flag)return
flag=false
index++
$('.img_box').animate({left:-index*$('.banner').width()},500,moveEnd)
})
.on('click','.point_box>li',function(){
if(!flag)return
flag=false
index=$(this).index()+1
$('.img_box').stop().animate({left:-index*$('.banner').width()},500,moveEnd)
})
}
// 切换标签页
changeTab()
function changeTab(){
$(document).on('visibilitychange',function(){
if(document.visibilityState==='hidden')clearInterval(timer)
else if(document.visibilityState==='visible')autoPlay()
})
}
</script>
</body>
</html>
轮播图
最新推荐文章于 2024-05-17 14:37:34 发布