轮播图

在这里插入图片描述

<!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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值