移动端左滑右滑

<!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>
</head>
<style>
    html,
    body {
        height: 100%;
        overflow: hidden;
    }

    .pro-list {
        position: relative;
        color: white;
        text-align: center;
        margin-top: 100px;
        padding: 2rem;
        box-sizing: border-box;
        line-height: 4;
    }

    .pro-list>.li-pro-main {
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        z-index: 2;
        background: pink;
        transition: .4s;
    }

    .pro-list>.li-del-btn {
        position: absolute;
        right: 0;
        top: 0;
        width: 20%;
        height: 100%;
        background-color: red;
        z-index: 1;
        transition: .4s;
    }
</style>

<body>
    <div class="li-module li-row pro-list">
        <div class="li-pro-main">
            左滑试试
        </div>
        <div class="li-del-btn">
            删除
        </div>
    </div>
</body>
<script>
    let startX, startY, moveEndX, moveEndY;
    const bodyDom = document.querySelector('body');
    const moceDom = document.querySelector('.li-pro-main');
    bodyDom.addEventListener('touchstart', function (e) {
        e.preventDefault();
        startX = e.touches[0].pageX;
        startY = e.touches[0].pageY;
    })
    bodyDom.addEventListener('touchmove', function (e) {
        e.preventDefault();
        moveEndX = e.touches[0].pageX;
        moveEndY = e.touches[0].pageY;
    })
    bodyDom.addEventListener('touchend', function (e) {
        e.preventDefault();
        console.log(startX, startY, moveEndX, moveEndY,)
        X = moveEndX - startX;
        Y = moveEndY - startY;
        if (Math.abs(X) > Math.abs(Y) && X < 100) {
            moceDom.style = 'left:-74px'
        }
        if (Math.abs(X) > Math.abs(Y) && X > 100) {
            moceDom.style = 'left:0px'
        }
        // moceDom.style = 'top:' + Y + 'px'
        console.log('差值', X, Y)
    })
</script>

</html>

 

posted @ 2022-12-06 17:17  玖捌  阅读(48)  评论(0)    收藏  举报