简体   繁体   English

启用 event.preventDefault 时允许使用 touchstart 垂直滚动

[英]Allow vertical scrolling with touchstart when event.preventDefault enabled

I am currently having an issue in allowing vertical scrolling when event.preventdefault is enabled.我目前在启用 event.preventdefault 时允许垂直滚动出现问题。

I am trying to add swipe functionality to my mobile page, I have tried may frameworks like hammer.js, swipe.js etc, and all of them require event.preventDefault enabled to detect left and right swipes.我正在尝试向我的移动页面添加滑动功能,我尝试过像hammer.js、swipe.js 等这样的框架,并且所有这些框架都需要启用 event.preventDefault 来检测左右滑动。

When event.preventDefault is enabled, the swipes detect perfectly, however you lose the ability to vertical scroll when you are on that element.当 event.preventDefault 启用时,滑动检测完美,但是当您在该元素上时,您将失去垂直滚动的能力。 ie you cannot move the screen up or down on a mobile device, when your finger starts on the swipe element.即,当您的手指开始滑动元素时,您无法在移动设备上上下移动屏幕。

I have tried building my own little script which works well, but again has the issue of vertical scrolling, that is an issue.我尝试构建自己的小脚本,效果很好,但又出现了垂直滚动的问题,这是一个问题。

var el = document.getElementById('navigation');
el.ontouchstart = function(e){
        e.preventDefault();
        el.innerHTML = "touch start";
};
el.ontouchend = function(e){
        el.innerHTML = "touch end";
};
el.ontouchmove = function(e){
        el.innerHTML = "touch moved";
};

el.ontouchcancel = function(e){
        el.innerHTML = "touch cancel";
};

Any ideas???有任何想法吗???

It's a common issue where you want the native browser behaviour to work alongside the interaction behaviour that people come to expect from a touchscreen device.这是一个常见的问题,您希望本机浏览器行为与人们期望从触摸屏设备获得的交互行为一起工作。

If you want to use a library you might need to hack it open as you WILL need to prevent the defaults to prevent the page from jumping all over the place when using touch events, but at other times you want to omit it as you want to prevent the page from remaining in a static position, obscuring other content.如果您想使用一个库,您可能需要将其打开,因为您将需要阻止默认设置以防止页面在使用触摸事件时到处跳转,但在其他时候,您想根据需要省略它防止页面停留在静止位置,遮挡其他内容。

Ideally you want add a clause to the handler that instructs them whether or not to prevent the default behaviour the browser executes upon receiving the event.理想情况下,您希望向处理程序添加一个子句,指示它们是否阻止浏览器在接收到事件时执行的默认行为。

Swiping for instance, is a behaviour that should occur in a short time frame (if you are taking for instance one whole second in moving your finger from one area to the other instead of, let's say, 120 ms, you're not actually swiping, but dragging. Thinking about time frames may help you here, for instance:例如,滑动是一种应该在短时间内发生的行为(例如,如果您将手指从一个区域移动到另一个区域需要一整秒而不是 120 毫秒,那么您实际上并不是在滑动, 但拖动。考虑时间框架可能会在这里帮助你,例如:

var threshold = 150, interactionStart;

el.ontouchstart = function( e )
{
    // store timestamp of interaction start
    interactionStart = +new Date;
};

el.touchmove = function( e )
{
    // get elapsed time in ms since start
    var delta = +new Date - interactionStart;

    if ( delta > threshold )
    {
        e.preventDefault();
    }
    else {
        // super cool magic here
    }
};

Whether 150 ms is the threshold you want depends on the action, as you see there is no fixed answer for your question as the actual implementation depends on what your application needs in terms of touch interactions. 150 毫秒是否是您想要的阈值取决于操作,正如您所看到的,您的问题没有固定答案,因为实际实现取决于您的应用程序在触摸交互方面的需求。

You could also consider not blocking the events default when the user is scrolling more along the vertical axis (ie compare whether the delta position of the events Y offset (relative to the start Y offset) is larger than the events X offset (relative to the start X offset) to detect whether the users is moving left or right or up or down (for instance if you have a carousel that can swipe horizontally (where the default behaviour is blocked so the page won't move up/down during the horizontal scroll) but want the page to scroll vertically when the user is obviously dragging the page among the vertical axis).您还可以考虑在用户沿垂直轴滚动更多时不阻止事件默认值(即比较事件 Y 偏移量的增量位置(相对于起始 Y 偏移量)是否大于事件 X 偏移量(相对于开始 X 偏移)来检测用户是向左或向右或向上或向下移动(例如,如果您有一个可以水平滑动的轮播(默认行为被阻止,因此页面在水平期间不会向上/向下移动)滚动)但希望页面在用户明显在垂直轴之间拖动页面时垂直滚动)。

The library jquery.touchSwipe solves that.库 jquery.touchSwipe 解决了这个问题。

The library: https://github.com/mattbryson/TouchSwipe-Jquery-Plugin图书馆: https : //github.com/mattbryson/TouchSwipe-Jquery-Plugin

An example page where swiping and scrolling are combined: http://labs.rampinteractive.co.uk/touchSwipe/demos/Page_scrolling.html滑动和滚动相结合的示例页面: http : //labs.rampinteractive.co.uk/touchSwipe/demos/Page_scrolling.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM