使用Javascript模拟滚动事件
我正在尝试使用 Mobile Safari 的 Javascript 模拟滚动事件。 我使用以下代码
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("scroll", true, true, window,0, 0, 0, 0, 0, false, false, false, false, 0, null);
上面的代码是 jQuery 插件 jQuery.UI.Ipad 的一部分,它基本上映射触摸事件,如 touchstart
、touchmove
、touchend 到鼠标事件,如
mouseover
、mousedown
等
但是由于某些原因,模拟滚动事件的代码无法正常工作...请帮助我。所以本质上我的问题是如何模拟滚动事件。
I am trying to Simulate a scroll event using Javascript for Mobile Safari.
I am using the following code
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("scroll", true, true, window,0, 0, 0, 0, 0, false, false, false, false, 0, null);
The above code is part of a jQuery plugin jQuery.UI.Ipad which basically maps touch events like touchstart
, touchmove
, touchend
to mouse events like mouseover
, mousedown
, etc
However for some reasons the code for simulating a scroll event is not working... Please help me. So essentially my question is how do I simulate the scroll event.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为人们对为什么要覆盖滚动控件感到困惑。我可以理解为什么你想模仿鼠标事件,但滚动也许不应该是其中之一。
通常对于滚动更改,您只需使用以下方法获取滚动:
并设置:
I think people are confused as to why you would overide the scroll control. I can see why you want to imitate mouse events, but scroll maybe should not be one of them.
Usually for scroll changes you can just get the scroll with:
And set with:
所以,我知道我也需要模拟它。对我来说,当你有一个带有溢出框的灯箱时,你就会需要它。我能想到的众多案例中只有一个。寻找答案。只是想我会分享我所在的位置,你不是与 jQuery.Ui.Ipad 我会谷歌那个..但这是我到目前为止所得到的并且确实有效但并不完美。
再次不完美并希望修复它..但它至少有效。现在请注意,这是一个使用 jQuery UI 滑块作为滚动条的 div,如
http://jqueryui.com/demos/slider/#side-scroll
希望能激发一些想法。如果我得到一个超级稳定的答案,我会回来。
干杯-杰里米
So, I know I' need to simulate it too. for me it's when you have a lightbox with a overflow box that you would need it. Just one case of many I can think of. looking to for an answer. Just thought I'd share where I'm at, thou not with the jQuery.Ui.Ipad I will Google that.. but here is what I got so far and does work but not perfectly.
Again not perfect and looking to fix it.. but it's at the least working. Now note, this is a div that is using the jQuery UI slider for a scroll bar as (thou in mine it's vertical) shown in
http://jqueryui.com/demos/slider/#side-scroll
Hope that spurs some ideas. If I get a super stable answer I'll get back.
Cheers -Jeremy
我需要这个来编写一个单元测试,为此我需要模拟滚动事件
有关更多详细信息:
I needed this to write a unit test , for which i need to simulate a scroll event
For more details : https://developer.mozilla.org/en-US/docs/Web/API/event.initUIEvent
对我有用的事件是鼠标滚轮,第 5 个参数需要为向下滚动为正,向上滚动为负。
请注意,FireFox 的事件类型为
DOMMouseScroll
。The event that worked for me was
mousewheel
, and the 5th parameter needs to be positive for scrolldown and negative for scrollup.Note that the event type is
DOMMouseScroll
for FireFox.现在应该使用
CustomEvent
创建事件,例如new CustomEvent("wheel", {detail: { deltaY: 1 } })
。document.createEvent
已弃用https:// developer.mozilla.org/en-US/docs/Web/API/CustomEvent
我构建了这个实用程序函数,每帧滚动 1 个像素
您还需要修改事件侦听器以使用详细信息,如
deltaY
将是未定义的Events should now be created with
CustomEvent
as suchnew CustomEvent("wheel", { detail: { deltaY: 1 } })
.document.createEvent
is deprecatedhttps://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
I built this utility function that scrolls by 1 pixel every frame
You will also need to modify the event listener to use detail, as
deltaY
will be undefined