移动端webview点击延迟处理

2022年9月29日 673点热度 0人点赞 0条评论

移动端webview在处理click事件的时候会有一定的延迟。我们可以用fastclick来解决,但fastclick未压缩23k,压缩后8k,比较重,这里有一个简易的解决方案:

function onFastClick(element ,handler) {
    var delay = 300,
        offset = 10,
        still = true,
        startX = 0,
        startY = 0,
        touch;
    function longClick() {//排除长按
        still = false;
    }
    function start(e) {
        still = true;
        touch = e.touches[0];
        startX = touch.pageX;
        startY = touch.pageY;
        setTimeout(longClick ,delay);
    }
    function move(e) {
        touch = e.touches[0];
        if(touch.pageX - startX >= offset || touch.pageY - startY >= offset) {//排除滑动
            still = false;
        }
    }
    function end(e) {
        clearTimeout(longClick);
        still && handler();
    }
    element.addEventListener("touchstart" ,start);
    element.addEventListener("touchmove" ,move);
    element.addEventListener("touchend" ,end);
    element.addEventListener("touchcancel" ,end);
}
onFastClick(document ,function() {
    console.log("fastclick")
})

 

⚠️此文章发表时间为2013年,内容已失效,仅做备份


原文标题:移动端webview点击延迟处理
原文链接:http://hai.li/2013/09/05/android-webview-click-problem.html
原文快照:无

帮助教程

提供最新的帮助教程,方便使用。

文章评论