Javascript:如果光标经过可滚动的div,则不要停止滚动窗口

发布于 2024-11-24 10:33:31 字数 535 浏览 2 评论 0原文

我正在构建一个网络应用程序,它有一个由许多小型可滚动 div 组成的网格(实际上是 Ace 编辑器),并且这个网格有足够的元素,它比窗口大。当用户开始在空白区域滚动时,我希望他们滚动窗口本身;当用户开始在网格元素内滚动时,我希望他们滚动那里的 div 内容。问题是,如果用户开始在空白区域上滚动,然后滚动以使鼠标移到网格元素上,则可滚动 div 捕获所有滚动事件,中断用户在网格上的流动并将它们“捕获”在网格内元素。

我无法手动捕获 onmousewheel 事件,因为据我所知,无法单独捕获水平鼠标滚轮移动和垂直移动,并且我希望 Mac OS X 上的用户能够向所有方向滚动。我考虑过使用 JS 在第一个 onscroll 事件上添加一个具有非常高 z-index 的不可见 div,并在 onscroll 事件不发生时将其删除触发一段时间。尚未对此进行编码,但我想知道是否有更好的解决方案,或者是否存在我没​​有想到的潜在陷阱。任何帮助或建议都会很棒!谢谢!

I'm building a web app that has a grid of many small scrollable divs (actually, Ace editors), and this grid has enough elements that it is larger than the window. When a user begins scrolling over empty space, I want them to be scrolling the window itself; when a user begins scrolling inside a grid element, I want them to scroll the div contents there. The thing is, if a user begins scrolling over empty space, and then scrolls such that their mouse goes over a grid element, that scrollable div captures all the scrolling events, interrupting the user's flow over the grid and "trapping" them inside the grid element.

I can't manually capture onmousewheel events, since AFAIK there's no way to capture horizontal mouse wheel movement separately from vertical, and I want users on Mac OS X to be able to scroll in all directions. I've thought about using JS to add an invisible div with a very high z-index on the first onscroll event, and removing it as soon as onscroll events aren't triggered for a certain period of time. Haven't yet coded this up, but I'm wondering if there's a better solution, or if there are any potential pitfalls that I haven't thought of. Any help or advice would be great! Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苯莒 2024-12-01 10:33:31

我认为由于浏览器支持和实际的解决方案,这个问题的解决方案将非常困难,这可能类似于计算滚动、回溯 div 并将滚动应用到页面。

可以做这样的事情:

$('div').scroll(function(e){
    // figure out how much it has scrolled
    window.scrollBy(0,howmuch);
});

不过我一点也不推荐这个解决方案,我认为更好的选择是将div设置为overflow:hidden;并选择一个可靠的滚动插件,并使用它来自定义 div 上的滚动行为。

I think a solution for this would be incredibly difficult due to browser support, and the actual solution, which would probably be something like calculating the scroll, backtracking the div, and applying the scroll to the page.

You could do something like this:

$('div').scroll(function(e){
    // figure out how much it has scrolled
    window.scrollBy(0,howmuch);
});

I don't recommend this solution in the slightest though, I think the better option would be to set the divs to overflow:hidden; and pick up a solid scroll plugin, and use that to customize the scroll behavior on the divs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文