Two-fingered scroll js on touchpad

爷,独闯天下 提交于 2019-12-13 15:03:30

问题


We have developed a site whcih has a horizontal orientation and are wanting to implement touchpad control with two fingers move left/right.

When you move two fingers left/right on touchpad, the site page is being scrolled left/right. Now we have implemented touchpad control with two fingers move up/down and page scrolled left/right.

How can we change touchpad control with two fingers move from up/down to left/right to scroll site page left/right using js or jQuery?


回答1:


I may be a little late but had the same question before I stumbled over this question. A little further investigation lead me to think that the best bet to capture trackpad scrolling would be the wheel event.

function doScroll(e) {
    // positive deltas are top and left
    // down and right are negative

    // horizontal offset    e.deltaX
    // vertical offset      e.deltaY

    console.log(`x:${e.deltaX} y:${e.deltaY}`);

    e.preventDefault(); // disable the actual scrolling
}

window.addEventListener("wheel", doScroll, false);

I have prepared a fiddle that tells you the scroll direction and offset values but prevents the scrolling itself.

The wheel event has a delta property that (at least in Chrome) is sensitive to momentum and gives you the current relative scroll offset rather than the absolute scroll position available in the scroll event.




回答2:


Usually when you want to take over touch events in script, you add something like this to prevent the usual scroll and zoom:

$("body").bind("touchstart", function(e) {
    e.preventDefault();
})



回答3:


What you need to do is change what can be scrolled. If your page is big enough where left/right scrolling makes sense, the browser will allow it be scrolled that way.

Basically, if you only want them scrolling in a certain direction, only make content in that direction. If necessary, you can achieve this by having a container div of the specific size you want with overflow set to none.



来源:https://stackoverflow.com/questions/9977876/two-fingered-scroll-js-on-touchpad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!