如何使 jQuery 选择器在整个浏览器窗口中工作
您好,我正在使用 jQuery 鼠标滚轮扩展
和它使用以下绑定
$('html').bind('mousewheel', function(event, delta) {
window.scrollBy(-120 * delta, 0);
return false;
});
增量由鼠标滚轮移动和页面水平滚动决定。
一切都很好,除了当您将光标移动到没有文本或图像的空白区域(通常没有 html 元素)时,鼠标滚轮移动突然没有响应。我确信这是因为绑定的工作方式。
我制作了一个不可见的 100% x 100% 固定 pos div 来测试我的理论,并且滚动可以完美地工作。虽然这对我来说似乎是一种黑客攻击,但我想知道这段代码的正确实现。
我怎样才能让这个函数在整个浏览器页面上被调用?
太感谢了!
更新:David主动在这里用他的代码做了一个测试页面。谁能告诉我这对你是否和对他一样有效?它对我不起作用,也就是说,只有当我的指针位于红色矩形上方时,我才能水平滚动页面。
Hi i'm using the jQuery mousewheel extension
and it uses the following bind
$('html').bind('mousewheel', function(event, delta) {
window.scrollBy(-120 * delta, 0);
return false;
});
The delta is determined by the mouse wheel movement and the page scrolls horizontally.
All is fine except when you move the cursor on an empty space where there is no text or image, generally no html element then the mousewheel movement is suddenly unresponsive. I'm positive this is because of how the bind is working.
I made an invisible 100% by 100% fixed pos div to test my theory and the scrolling would work perfectly. Though this seems like a hack to me, and i was wondering of the proper implementations of this code.
How can i make this function be called all over the browser page?
Thank you so much!
Update: David took the initiative to make a test page with his code here. Can anyone tell me if it works for you as it does for him? It doesn't work for me, that is i can only scroll the page horizontally only if my pointer is over the red rectangle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试将其绑定到
document
或window
?Did you try binding it to the
document
orwindow
?我遇到了完全相同的问题并设法解决,请参阅此示例。
CSS
请注意,您需要将鼠标滚轮事件应用于#wrapper 元素。
这允许包装元素保留在依赖 html 或 body 之前可能未占用的空间。
您可能会注意到宽度是由表格定义的,我发现这是水平网页中的最佳方法。
I encountered exactly the same problem and managed to resolve, see this eg.
CSS
Please note you need to apply the mousewheel event to the #wrapper element.
This allows the wrapper element to hold the space that may be left unoccupied when before relying on html or body.
You may note that the width is defined by a table, which I have found is the best approach in horizontal web pages.