您也可以在 HTML+JS 端执行此操作,前提是 HTML 文档不高于 WebView 视口(在 JS 中也称为 window.height)。您可以通过在适当的时间(即当元素及其所有父元素在用户开始移动手指的方向上没有可滚动的内容时)在“touchmove”事件上调用 preventDefault 来做到这一点。
我将向您展示实际代码来执行此操作,而无需使用 jQuery ...但您必须自己实现 Q.addEventListener 和 Q.removeEventListener(或使用 jQuery)。
function _touchScrollingHandler(event) {
var p = event.target;
var pos;
var scrollable = null;
do {
if (!p.computedStyle) {
continue;
}
var overflow = p.computedStyle().overflow;
var hiddenHeight = p.scrollHeight - p.offsetHeight;
var s = (['hidden', 'visible'].indexOf(overflow) < 0);
if ((s || p.tagName === 'HTML') && hiddenHeight > 0) {
if ((Q.Pointer.movement.positions.length == 1)
&& (pos = Q.Pointer.movement.positions[0])) {
var sy = Q.Pointer.getY(event)
+ Q.Pointer.scrollTop();
if ((sy > pos.y && p.scrollTop == 0)
|| (sy < pos.y && p.scrollTop >= hiddenHeight)) {
continue;
}
}
scrollable = p;
break;
}
} while (p = p.parentNode);
if (!scrollable) {
Q.Pointer.preventDefault(event);
}
}
var Q = {
preventTouchScrolling: function () {
Q.addEventListener(window, 'touchmove', _touchScrollingHandler);
},
restoreTouchScrolling: function () {
Q.removeEventListener(window, 'touchmove', _touchScrollingHandler);
}
};