欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

仿小米官网轮播图(Banner)的实现(代码教程)

程序员文章站 2022-04-23 19:27:33
本篇是仿照小米官网所做的轮播图,注意事项(图片命名规则为 banner-img-0.jpg,banner-img-1.jpg 依此类推); 实例代码如下:

本篇是仿照小米官网所做的轮播图,注意事项(图片命名规则为 banner-img-0.jpg,banner-img-1.jpg 依此类推);

仿小米官网轮播图(Banner)的实现(代码教程)

实例代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">

        #banner{
            margin: 100px auto;
            position: relative;
            width: 813px;
            height: 320px;
        }

        #banner>img{
            height: 100%;
            width: 100%;
            cursor: pointer;
        }

        #banner>p{
            position: absolute;
            bottom: 20px;
            right: 20px;
        }

        #banner>p>span{
            display: block;
            width: 10px;
            height: 10px;
            float: left;
            border: #333333 solid 2px;
            border-radius: 50%;
            background-color: #717987;
            margin-left: 10px;
            cursor: pointer;
        }

        #banner>p>span:first-child{
            background-color: white;
        }

    </style>
</head>
<body>

<p id="banner">
    <img src="img/banner-img-0.jpg"/>
    <p>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </p>
</p>

<script type="text/javascript" src="js/jquery-1.12.3.js" ></script>
<script type="text/javascript">

    let $bannerImg = $("#banner").children("img");
    let $bannerPoints = $("#banner>p");
    let prePoint = 0;//记录上一个被点击的小点
    $bannerPoints.delegate("span", "click mouseenter", function () {
        clearInterval(bannerInterval);//每次点击时,先停止轮播
        let $2 = $(this);
        if (prePoint !== $2.index()) { //防止鼠标移入时 用户点击了 重复两次走相同的逻辑赋值
            currentPoint = $2.index();//赋值-- 关联到图片的循环设置
            $2.css("background-color", "white").parent().children(":eq(" + prePoint + ")").css("background-color", "#717987");
            $bannerImg.attr("src", "img/banner-img-" + $2.index() + ".jpg");
            prePoint = $2.index(); //记录上一个点击的小点
        }
    });
    $bannerPoints.delegate("span", "mouseleave", function () { //鼠标离开时 继续轮播
        startBannerInterval();
    });
    let bannerInterval;
    let currentPoint = 0;
    let pointSize = $bannerPoints.children().length - 1;//用来重置小点的判断
    function startBannerInterval() {
        let children = $bannerPoints.find("span");
        bannerInterval = setInterval(function () {
            children.eq(prePoint).css("background-color", "#717987");
            prePoint = currentPoint; //赋值-- 关联到图片的循环设置
            if (currentPoint > pointSize) {
                currentPoint = 0;
                prePoint = 0;
            }
            children.eq(currentPoint).css("background-color", "white");
            $bannerImg.attr("src", "img/banner-img-" + currentPoint + ".jpg");
            currentPoint++;
        }, 2000);
    }
    startBannerInterval();//开始轮播

</script>

</body>
</html>