微信浏览器 点击 出现卡顿现象及解决方案

做的小游戏在手机微信中打开测试和在UC浏览器中测试, 点击时候微信浏览器出现明显的卡顿现象, 查阅发现是:

主要是由于200ms超时导致内核不一定会一直处理touchmove事件,一旦超时会将后续所有的事件转交给UI处理,导致touchmove不会一直触发。系统浏览器也存在同样的问题,为了解决开发者需要,建议开发者在touchstart时调用event.preventDefault,这样就可以保证内核会一起触发touchmove事件了。

的问题。但同时新版Chrome浏览器会提示

Unable to preventDefault inside passive event listener due to target being treated as passive.

最后在js中修改成添加

document.ontouchstart = function(e) {
		// 阻止默认事件
		var ev = e || window.event;
		// ev.preventDefault();
        if (ev.cancelable) {
            // 判断默认行为是否已经被禁用
            if (!ev.defaultPrevented) {
                ev.preventDefault();
            }
        }
    }

并在后续需要的操作中还原了默认事件

document.ontouchstart = function(e) {
        // 还原默认事件
        e.returnValue = true;
    }

成功解决, 全剧终。

猜你喜欢

转载自blog.csdn.net/qq_35081500/article/details/83275753