如何使用 jQuery 判断鼠标是否位于元素的特定部分上?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
工作示例: http://jsfiddle.net/XBnjJ/3/
Try this:
Working example: http://jsfiddle.net/XBnjJ/3/
也许您可以尝试将一个元素放置在该区域中,并进行绝对定位,并检测鼠标悬停在该区域上。当然,该元素可以是透明的 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...
在函数中使用事件和元素偏移量。
完整说明请参阅:
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