开发者

HTML with event.preventDefault

开发者 https://www.devze.com 2023-01-11 20:30 出处:网络
If 开发者_运维百科the html document is disabled. (no scrolling...) document.ontouchmove = function(event){ event.preventDefault(); }

If 开发者_运维百科the html document is disabled. (no scrolling...)

document.ontouchmove = function(event){ event.preventDefault(); }

Can I selectively enabled elements in the html document? such as a div, span What javascript should I use to do that?


Here’s a fairly hacky solution.

Change your touchmove handler as follows:

document.ontouchmove = function (event) {
    if (!event.elementIsEnabled) {
        event.preventDefault();
    }
};

Then for an element that you want to enable:

element.ontouchmove = function (event) {
    event.elementIsEnabled = true;
};

This works because:

  • When a touchmove event is fired on the element, both of these event handlers handle the event in the bubbling phase. This means that the element touchmove handler fires first, because it is closest to the source of the event.
  • When two event handlers handle the same event, they receive the same event object.
  • You’re allowed to set your own arbitrary properties on an event object.

So, the element touchmove handler just sets a custom property on the event object, and then the document touchmove handler checks for that custom property to determine whether the default action should be prevented.

0

精彩评论

暂无评论...
验证码 换一张
取 消