javascript 在鼠标悬停时向左向右滚动 div 内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7404242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:00:42  来源:igfitidea点击:

Scroll div content left right on mouseover

javascriptjquerycssscroll

提问by Ignaty Nikulin

Good day.

再会。

I need script that will follow the mouse and will scroll content depending on what side mouse is. If left then left, if right then right.

我需要跟随鼠标的脚本,并根据鼠标的位置滚动内容。左则左,右则右。

I need it for cases when line of hrefs will exceed width of block, like here http://cloud.ignatynikulin.com/45121918090s3R15193i

当 href 行超过块的宽度时,我需要它,就像这里http://cloud.ignatynikulin.com/45121918090s3R15193i

If the width is exceed then you will be able to scroll it when you put your mouse to right.

如果超过宽度,那么当您将鼠标向右移动时,您将能够滚动它。

Something that will do that: http://codecanyon.net/item/jquery-mouse-slider/full_screen_preview/143061

可以做到这一点的东西:http: //codecanyon.net/item/jquery-mouse-slider/full_screen_preview/143061

I tried that script, but the problem with it is that it needs to rely on some width that I don't have.

我试过那个脚本,但它的问题是它需要依赖一些我没有的宽度。

Any plugin or ideas suggestion?

任何插件或想法建议?

Thank you!

谢谢!

回答by Dave Hilditch

Check out the answer here from StackOverflow - it's a perfect solution:

在 StackOverflow 上查看答案 - 这是一个完美的解决方案:

Continuous scroll on hover [performance]

悬停时连续滚动 [性能]

回答by Praxis Ashelin

Make the containing div positioned absolute. Then in jQuery, catch the onMouseMove event, calculate the mouse position relative to the containing div (using .width() this works even with dynamic width), and change the left/right property of your containing div according to this.

使包含的 div 定位为绝对。然后在 jQuery 中,捕获 onMouseMove 事件,计算鼠标相对于包含 div 的位置(使用 .width() 这甚至适用于动态宽度),并根据此更改包含 div 的左/右属性。

Reference: jQuery get mouse position within an element

参考:jQuery 获取元素内的鼠标位置