此次轮播图的实现,先看看效果图:

android viewpager滑动图片轮播 html手动滑动图片轮播_css


咱先来分析分析功能:首先是左右切换的按键在右下方;然后是小圆圈在左下方;点击小圆圈可以实现图片切换;图片在切换时被选中的小圆圈会有一个吃东西的小动画。先来看看写出来的成果图:


分析:可以看到,小圆圈的动画效果有两种,一种是自动播放和点击向右滑动按钮的动画,张口在右边;另一种是点击向左滑动的按钮,张口在左边。所以要把这两种动画区分开来。

实现的功能有:
(1)自动播放
(2)点击小圆圈能实现图片切换
(3)鼠标放在图片上停止自动播放

完整代码(含详细注释):

<!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;
        }

        li {
            list-style: none;
        }

        .big_box {
            position: relative;
            width: 500px;
            height: 400px;
            margin: 100px auto;
            overflow: hidden;
        }

        .big_box ul {
            position: absolute;
            left: 0;
            width: 700%;
            height: 400px;
        }

        .big_box ul li {
            float: left;
        }

        .big_box ul li img {
            width: 500px;
            height: 400px;
        }

        .left,
        .right {
            position: absolute;
            top: 80%;
            transform: translateY(-50%);
            background-color: rgba(255, 255, 255, 0.3);
            width: 30px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            color: white;
            border-radius: 5px;
            font-weight: bolder;
        }

        .left {
            right: 50px;
        }

        .right {
            right: 10px;
        }

        .big_box ol {
            position: absolute;
            bottom: 0;
            /* 这里别给ol限制宽度,让小圆圈的个数自动把ol撑大 */
            left: 16%;
            transform: translateX(-50%);
            height: 10%;
            line-height: 10%;
            text-align: left;
        }

        .big_box ol li {
            float: left;
            width: 15px;
            height: 15px;
            background-color: rgba(255, 255, 255, .3);
            border-radius: 50%;
            margin-left: 5px;
        }


        .left,
        .right,
        ol li {
            cursor: pointer;
            /* z-index: 99; */
        }
		/*自动播放或者点击向右滑动的按键时的动画,张口在右边*/
        .big_box ol .circleAnimation {
            margin-top: -2.5px;
            width: 20px;
            height: 20px;
            position: relative;
            background-color: transparent;
        }

        .circleAnimation::before,
        .circleAnimation::after {
            content: "";
            width: 100%;
            height: 50%;
            background-color: #fff;
        }

        .circleAnimation::before {
            position: absolute;
            top: 0;
            transform-origin: 50% 100%;
            border-radius: 10px 10px 0 0;
            animation: animate_top .3s linear 2;
            /* transform: rotate(-25deg); */
        }

        .circleAnimation::after {
            position: absolute;
            bottom: 0;
            transform-origin: 50% 0;
            border-radius: 0 0 10px 10px;
            animation: animate_bottom .3s linear 2;
            /* transform: rotate(25deg); */
        }

        @keyframes animate_top {
            50% {
                transform: rotate(-45deg);
            }
        }

        @keyframes animate_bottom {
            50% {
                transform: rotate(45deg);
            }
        }

        /* 点击向左滑动按钮的动画样式,张口在左边*/
        .big_box ol .circleAnimation2 {
            margin-top: -2.5px;
            width: 20px;
            height: 20px;
            position: relative;
            background-color: transparent;
        }

        .circleAnimation2::before,
        .circleAnimation2::after {
            content: "";
            width: 100%;
            height: 50%;
            background-color: #fff;
        }
		/*两个伪类元素的动画样式不一样,注意区分*/
        .circleAnimation2::before {
            position: absolute;
            top: 0;
            transform-origin: 50% 100%;
            border-radius: 10px 10px 0 0;
            animation: animate_top2 .3s linear 2;
        }

        .circleAnimation2::after {
            position: absolute;
            bottom: 0;
            transform-origin: 50% 0;
            border-radius: 0 0 10px 10px;
            animation: animate_bottom2 .3s linear 2;
        }
		
        @keyframes animate_top2 {
            50% {
                transform: rotate(45deg);
            }
        }

        @keyframes animate_bottom2 {
            50% {
                transform: rotate(-45deg);
            }
        }
    </style>
</head>

<body>
    <div class="big_box">
        <!-- 轮播的图片 -->
        <ul>
            <li>
                <img src="imgs/picture1.png" alt="">
            </li>
            <li>
                <img src="imgs/picture7.png" alt="">
            </li>
            <li>
                <img src="imgs/picture3.png" alt="">
            </li>
            <li>
                <img src="imgs/picture4.png" alt="">
            </li>
            <li>
                <img src="imgs/picture5.png" alt="">
            </li>
            <li>
                <img src="imgs/picture6.png" alt="">
            </li>
        </ul>
        <!-- 左右按键 -->
        <div class="left"><</div>
        <div class="right">></div>
        <!-- 小圆圈 -->
        <ol>

        </ol>
    </div>
    <script>
        var timer;
        var big_box = document.querySelector('.big_box');
        var ul = big_box.querySelector('ul');
        var ol = big_box.querySelector('ol');
        var lis_img = ul.querySelectorAll('li');
        var left = document.querySelector('.left');
        var right = document.querySelector('.right');
        var num = 0;  //记录要滑到第几张图片

        function animate(obj, target) {
            var timer1 = setInterval(function () {
                var current = obj.offsetLeft;
                var step = 10;
                step = current > target ? -step : step;
                // 下面要包括等于的情况,否则会发生抖动
                if (Math.abs(current - target) <= Math.abs(step)) {
                    clearInterval(timer1);
                    obj.style.left = target + 'px';
                }
                else {
                    obj.style.left = current + step + 'px';
                }
            }, 10)
        }

        //小圆圈样式改变
        function circlechange_right(circles, circle) {
            if (circle == lis_img.length) {
                circle = 0;
            }
            //排他思想设置小圆圈样式
            //排他思想第一步:先把所有的小圆圈样式去掉
            for (var i = 0; i < circles.length; i++) {
                circles[i].className = "";
            }
            //排他思想第二步:把当前图片对应的小圆圈设置样式
            circles[circle].className = "circleAnimation";
        }

        function circlechange_left(circles, circle) {
            if (circle == lis_img.length) {
                circle = 0;
            }
            //排他思想设置小圆圈样式
            //排他思想第一步:先把所有的小圆圈样式去掉
            for (var i = 0; i < circles.length; i++) {
                circles[i].className = "";
            }
            //排他思想第二步:把当前图片对应的小圆圈设置样式
            circles[circle].className = "circleAnimation2";
        }

        //在页面刚加载进来就执行代码
        window.addEventListener('load', function () {
            //设置小圆点的个数
            for (var i = 0; i < lis_img.length; i++) {
                var li = document.createElement('li');
                ol.appendChild(li);
                // 给小圆圈添加自定义属性
                li.setAttribute('index', i);
                //一开始第一个小圆圈就是被选中状态
                if (i == 0) {
                    li.className = "circleAnimation";
                }
                //给小圆圈添加点击处理事件
                li.addEventListener('click', function () {
                    //排他思想实现小圆圈样式改变
                    for (var j = 0; j < ol.children.length; j++) {
                        ol.children[j].className = "";
                    }
                    this.className = "circleAnimation";
                    //实现点击小圆圈后图片滑动
                    var index = this.getAttribute('index');
                    animate(ul, -index * big_box.offsetWidth);
                    // 在图片滑动的同时对应的小圆圈样式也要发生改变,所以调用animate函数同时调用circlechange函数
                    circlechange(circles, index);
                })
            }
            //为了实现无缝衔接的切换图片,要把第一张图片克隆到最后一张图片的附近
            var circles = ol.querySelectorAll('li');
            // cloneNode函数若括号里面是true,则是深拷贝,false则是浅拷贝
            var li_img = ul.children[0].cloneNode(true);
            ul.appendChild(li_img);

            //点击右箭头向右滑动
            right.addEventListener('click', function () {
                //下面if代码是实现向右滑动的无缝衔接,不懂的建议自己手动模拟一遍
                if (num >= lis_img.length) {
                    num = 0;
                    //注意改变属性left的值后面一定要跟px,否则没有效果
                    ul.style.left = 0 + 'px';
                }
                num++;
                animate(ul, -num * big_box.offsetWidth);
                circlechange_right(circles, num);
            })
            //点击左箭头向左滑动
            left.addEventListener('click', function () {
                //下面if代码是实现向左滑动的无缝衔接,不懂的建议自己手动模拟一遍
                if (num <= 0) {
                    num = lis_img.length;
                    ul.style.left = -lis_img.length * big_box.offsetWidth + 'px';
                }
                num--;
                animate(ul, -num * big_box.offsetWidth);
                circlechange_left(circles, num);
            })

            //实现自动播放----因为自动播放的功能和向右滑动的功能一样,所以直接调用向右滑动的函数
            timer = setInterval(function () {
                right.click();
            }, 2000)

            //鼠标放到盒子上停止自动播放
            big_box.addEventListener('mouseover', function () {
                clearInterval(timer);
            })

            //鼠标离开自动播放
            big_box.addEventListener('mouseout', function () {
                clearInterval(timer);
                //在重新创建一个定时器时最好先清除一下定时器
                timer = setInterval(function () {
                    right.click();
                }, 2000)
            })
        })
    </script>
</body>

</html>

所用到的图片素材: