Native H5 carousel

On mobile, swipe and tap the arrow.
Click on the arrow on the PC side

<!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>原生H5轮播图</title>
</head>


<style>
    html,
    body {
    
    
        margin: 0;
        padding: 0;
    }

    * {
    
    

        margin: 0;

        padding: 0;

    }

    .title_name {
    
    
        background-color: #9eaceb;
        font-size: 16px;
        height: 30px;
        color: #fff;
        font-weight: bold;
        line-height: 30px;
        padding: 5px;
    }

    .top {
    
    

        width: 100%;
        overflow: hidden;

    }

    #box {
    
    
        width: 100px;
        height: 100px;
        border: 1px solid black;
        position: relative;
        overflow: hidden;
    }

    #ul1 {
    
    
        width: 64rem;
        height: 7.5rem;
    }

    #ul1 li {
    
    
        width: 16rem;
        height: 7.5rem;
        float: left;
        font-size: 2rem;
        color: #fff;
        line-height: 7.5rem;
        text-align: center;
        list-style: none;
    }

    .s_left {
    
    
        position: relative;
        top: -4.7rem;
        color: #fff;
        left: 5px;
    }

    .s_right {
    
    
        position: relative;
        top: -4.7rem;
        color: #fff;
        left: 87%;
        right: 0px;

    }
</style>

<body>
    <div id="code">
        <div class="top">
            <div class="title_name"> 轮播 Tips</div>
            <ul id="ul1">
                <li style="background: red">1</li>
                <li style="background: yellow">2</li>
                <li style="background: green">3</li>
                <li style="background: blue">4</li>
            </ul>
            <span class="s_left" onclick="changeBanner(-1)">
                < </span>
                    <span class="s_right" onclick="changeBanner(1)">
                        >
                    </span>
        </div>
      
    </div>
</body>
<script>

    (function (win, doc) {
    
    
        function change() {
    
    
            doc.documentElement.style.fontSize = doc.documentElement.clientWidth * 20 / 320 + 'px';
        }
        change();
        win.addEventListener('resize', change, false);
    })(window, document);

    var x = 0;
    var iNow = 0;
    var oUl = document.getElementById('ul1');
    var aLi = oUl.getElementsByTagName('li');
    function changeBanner(num) {
    
    
        iNow += num;
        if (iNow < 0) {
    
    
            iNow = aLi.length - 1
        }
        if (iNow == aLi.length) {
    
    
            iNow = 0
        }

        x = -iNow * aLi[0].offsetWidth;
        oUl.style.transform = 'translateX(' + x + 'px)';
        oUl.style.transition = '20ms all ease';
    }
    window.addEventListener('DOMContentLoaded', function () {
    
    
        console.log(232323)


        oUl.addEventListener('touchstart', function (ev) {
    
    
            console.log(333333333333333333)

            oUl.style.transition = 'none';
            var disX = ev.targetTouches[0].pageX - x;
            var downX = ev.targetTouches[0].pageX;
            function fnMove(ev) {
    
    
                x = ev.targetTouches[0].pageX - disX;
                oUl.style.transform = 'translateX(' + x + 'px)';
            }

            function fnEnd(ev) {
    
    
                var upX = ev.changedTouches[0].pageX;
                if (Math.abs(upX - downX) > 50) {
    
    

                    if (upX - downX < 0) {
    
    
                        iNow++;
                        if (iNow == aLi.length) {
    
    
                            // iNow = aLi.length - 1 
                            iNow = 0
                        }
                    } else {
    
    

                        iNow--;
                        if (iNow == -1) {
    
    
                            //  iNow = 0;
                            iNow = aLi.length - 1
                        }
                    }
                }
                x = -iNow * aLi[0].offsetWidth;
                oUl.style.transform = 'translateX(' + x + 'px)';
                oUl.style.transition = '20ms all ease';
                document.removeEventListener('touchmove', fnMove, false);
                document.removeEventListener('touchend', fnEnd, false);
            }
            document.addEventListener('touchmove', fnMove, false);
            document.addEventListener('touchend', fnEnd, false);
            ev.preventDefault();
        }, false);
    }, false);

</script>

</html>

Guess you like

Origin blog.csdn.net/weixin_43506403/article/details/128404254