6

移动端禁止下拉 露出网址或广告

 2 years ago
source link: https://www.fly63.com/article/detial/11570
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

更新日期: 2022-05-25阅读量: 21标签: 移动端分享

扫一扫分享

移动端禁止下拉 露出网址或广告

查找解决方案

document.body.addEventListener('touchmove', function (e) { e.preventDefault() }, { passive: false })

就很离谱,这样会导致scroll无法滚动。

我的解决方案

function stopDown(el) {
    let startX, startY;
    // el是最外层div 
    el.addEventListener("touchstart", (e) => {
        startX = e.changedTouches[0].pageX;
        startY = e.changedTouches[0].pageY;
    })
    el.addEventListener("touchmove", (e) => {
        //获取滑动屏幕时的X,Y
        let endX = e.changedTouches[0].pageX;
        let endY = e.changedTouches[0].pageY;
        //获取滑动距离
        let distanceX = endX - startX;
        let distanceY = endY - startY;
        //判断滑动方向
        if (Math.abs(distanceX) > Math.abs(distanceY) && distanceX > 0) {
            // console.log('往右滑动');
        } else if (Math.abs(distanceX) > Math.abs(distanceY) && distanceX < 0) {
            // console.log('往左滑动');
        } else if (Math.abs(distanceX) < Math.abs(distanceY) && distanceY < 0) {
            // console.log('往上滑动');
        } else if (Math.abs(distanceX) < Math.abs(distanceY) && distanceY > 0) {
            // 核心在这  
            // path 是从触摸点到最外层所有祖先节点的集合,不了解就log看下
            let path = e.path
            let is_scrollTop = false
            // 从触摸点到最外层所有祖先节点 scrollTop不为0就不禁止下拉 为0 就禁止下拉
            for (let dom of path) {
                if (dom.scrollTop) {
                    is_scrollTop = true
                }
            }
            if (!is_scrollTop) {
                e.preventDefault()
                return false
            }
            // console.log('往下滑动');
        } else {
            // console.log('点击未滑动');
        }
    })
}

目前自测安卓 微信内、uc、小米浏览器 效果都不错

来自:https://www.cnblogs.com/kslq/archive/2022/05/25/16261843.html

链接: https://www.fly63.com/article/detial/11570


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK