简体   繁体   English

在iPad或iPhone上jQuery捕获滚动事件与Web浏览器不一致

[英]jQuery capture of scroll event on iPad or iPhone not consistent with web browsers

In the site I am building, there is an effect where the top navigation "unlocks" from being a fixed element when you scroll past a certain point. 在我正在构建的网站中,当您滚动经过某个点时,顶部导航会从固定元素“解锁”。 It works very smoothly on my computer. 它在我的电脑上运行非常顺利。 However, on iPad or iPhone, the scroll event, which looks like this: 但是,在iPad或iPhone上,滚动事件看起来像这样:

$(window).on('scroll', function(){...});

...if you flick to scroll the screen, the scrolling happens automatically, and the event doesn't fire until the scrolling comes to a stop. ...如果您轻弹滚动屏幕,则会自动滚动,直到滚动停止时才会触发事件。 If you move your finger to scroll, the event doesn't fire until you let go. 如果您移动手指滚动,则直到您松开才会触发事件。 In other words, it does not fire as you move (ie, scroll) the screen. 换句话说,当您移动(即滚动)屏幕时,它不会触发。

Is there some way I can capture both the movement of the user's finger as the screen is scrolled, and also as the "inertia" movement is happening? 有什么方法可以在屏幕滚动时捕捉用户手指的移动,还有“惯性”运动正在发生? If my script would run when those events happen, the screen should be updated along the way, and it should all happen smoothly like it does on my computer. 如果我的脚本会在这些事件发生时运行,那么屏幕应该在整个过程中进行更新,并且它应该像在我的计算机上一样顺利进行。

I assume this has something to do with iOs. 我认为这与iOs有关。 I don't have an Android device to test with... not sure if it is also an issue there or not. 我没有Android设备进行测试...不确定它是否也是一个问题。

Would appreciate any advice. 非常感谢任何建议。 Thank you! 谢谢!

you could try using the touchmove event instead for mobile users. 您可以尝试使用touchmove事件代替移动用户。 that way the code runds when they move their finger instead of after they let go. 这样,代码在他们移动手指时而不是在他们放开之后就会变坏。

$(document).on('touchmove', function(){...});

more info here: https://developer.mozilla.org/en-US/docs/Web/Events/touchmove 更多信息: https//developer.mozilla.org/en-US/docs/Web/Events/touchmove

Like intelligentbean said, you could use the "touchmove" event and listen to it, you could also use touchstart and touchend if you want to do anything special before or after the touch happened. 就像intelligentbean说的那样,你可以使用“touchmove”事件并听取它,你也可以使用touchstart和touchend,如果你想在触摸发生之前或之后做任何特别的事情。

Also, the jQuery event is also available for touch events, but its not the event passed on the parameter of the listener function, but rather on one of its properties: 此外,jQuery事件也可用于触摸事件,但它不是传递给侦听器函数的参数的事件,而是它的一个属性:

$(document).on('touchmove',function(e){
    touchEvent = e.originalEvent.touches[0]; //this is your usual jQuery event, with its properties such as pageX and pageY properties
});

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

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