如何使用 jQuery 判断鼠标是否位于元素的特定部分上?

发布于 2024-09-26 17:38:18 字数 2057 浏览 0 评论 0原文

我有一个 980px 宽的 div 元素,如果鼠标位于 div 的最后 100 像素左右(我想滑动它),我希望能够触发一个事件。我已经知道如何滑动它,但我无法做出一些东西来告诉我鼠标何时位于前 100 像素中,以便它向左滚动或在 880 像素之后,以便它可以向右滚动。这是我的标记:

<div class="menuProductosNav">
    <ul>
        <li><a href="computadoras.html"><img src="assets/img/menuProductos/producto0.png" alt="" /></a></li>
        <li><a href="consumibles.html"><img src="assets/img/menuProductos/producto11.png" alt="" /></a></li>
        <li><a href="impresoras.html"><img src="assets/img/menuProductos/producto2.png" alt="" /></a></li>
        <li><a href="apple.html"><img src="assets/img/menuProductos/producto1.png" alt="" /></a></li>
        <li><a href="productos.html"><img src="assets/img/menuProductos/producto3.png" alt="" /></a></li>
        <li><a href="redescomu.html"><img src="assets/img/menuProductos/producto4.png" alt="" /></a></li>
        <li><a href="proyecciones.html"><img src="assets/img/menuProductos/producto5.png" alt="" /></a></li>
        <li><a href="accesorios.html"><img src="assets/img/menuProductos/producto6.png" alt="" /></a></li>
        <li><a href="energia.html"><img src="assets/img/menuProductos/producto7.png" alt="" /></a></li>
        <li><a href="refacciones.html"><img src="assets/img/menuProductos/producto8.png" alt="" /></a></li>
        <li><a href="software.html"><img src="assets/img/menuProductos/producto9.png" alt="" /></a></li>
        <li><a href="pdv.html"><img src="assets/img/menuProductos/producto10.png" alt="" /></a></li>
    </ul>
</div>

老实说,我对此感到困惑,有人能让我知道该怎么做吗?我所能得到的只是元素的宽度:

var $nav = $('.menuProductosNav');
$nav.bind('mouseover',function(){
 alert($nav.width());
});

I have a div element that is 980px wide and I'd like to be able to trigger an event if the mouse is in the last 100 pixels or so of the div (I want to slide it). I already know how to slide it, but I haven't been able to make something that tells me when the mouse is either in the first 100pixels so it scrolls left or after the 880 so it can go right. This is my markup:

<div class="menuProductosNav">
    <ul>
        <li><a href="computadoras.html"><img src="assets/img/menuProductos/producto0.png" alt="" /></a></li>
        <li><a href="consumibles.html"><img src="assets/img/menuProductos/producto11.png" alt="" /></a></li>
        <li><a href="impresoras.html"><img src="assets/img/menuProductos/producto2.png" alt="" /></a></li>
        <li><a href="apple.html"><img src="assets/img/menuProductos/producto1.png" alt="" /></a></li>
        <li><a href="productos.html"><img src="assets/img/menuProductos/producto3.png" alt="" /></a></li>
        <li><a href="redescomu.html"><img src="assets/img/menuProductos/producto4.png" alt="" /></a></li>
        <li><a href="proyecciones.html"><img src="assets/img/menuProductos/producto5.png" alt="" /></a></li>
        <li><a href="accesorios.html"><img src="assets/img/menuProductos/producto6.png" alt="" /></a></li>
        <li><a href="energia.html"><img src="assets/img/menuProductos/producto7.png" alt="" /></a></li>
        <li><a href="refacciones.html"><img src="assets/img/menuProductos/producto8.png" alt="" /></a></li>
        <li><a href="software.html"><img src="assets/img/menuProductos/producto9.png" alt="" /></a></li>
        <li><a href="pdv.html"><img src="assets/img/menuProductos/producto10.png" alt="" /></a></li>
    </ul>
</div>

I honestly am lost at this, could anyone let me know how to do this? all I can get is the element's width:

var $nav = $('.menuProductosNav');
$nav.bind('mouseover',function(){
 alert($nav.width());
});

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

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

发布评论

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

评论(3

海未深 2024-10-03 17:38:18

试试这个:

var $nav = $('.menuProductosNav');
var nav_width = $nav.width();
var THRESHOLD = 100;
$nav.bind('mouseover mousemove',function(e) {
    var offset = nav_width - (e.clientX - this.offsetLeft);
    console.log(offset);
    if(offset < THRESHOLD) {
      //mouse is within bounds...
      alert('within bounds');
    } 
});​

工作示例: http://jsfiddle.net/XBnjJ/3/

Try this:

var $nav = $('.menuProductosNav');
var nav_width = $nav.width();
var THRESHOLD = 100;
$nav.bind('mouseover mousemove',function(e) {
    var offset = nav_width - (e.clientX - this.offsetLeft);
    console.log(offset);
    if(offset < THRESHOLD) {
      //mouse is within bounds...
      alert('within bounds');
    } 
});​

Working example: http://jsfiddle.net/XBnjJ/3/

青巷忧颜 2024-10-03 17:38:18

也许您可以尝试将一个元素放置在该区域中,并进行绝对定位,并检测鼠标悬停在该区域上。当然,该元素可以是透明的 div 等。

另一种解决方案是使用 jQuery 检测鼠标位置,将其与 div 位置进行比较并触发滑动。但这会有点棘手。

如果您能在某个地方进行演示,那将会非常有帮助......

Maybe you could try placing an element in that area, with absolute positioning, and detect mouse hover on that. Of course, the element would be a transparent div or such.

Another solution is using jQuery to detect the mouse position, compare that with the div position and trigger the slide. But this would be a little bit trickier.

It would be very helpful if you would have a demo of that somewhere...

半﹌身腐败 2024-10-03 17:38:18

在函数中使用事件和元素偏移量。

完整说明请参阅:

http://jquery-howto .blogspot.com/2009/07/identifying-locating-mouse-position-in.html

Use the event in your function and the element offset.

Full description see:

http://jquery-howto.blogspot.com/2009/07/identifying-locating-mouse-position-in.html

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