更好的 jquery 悬停编码
我有一个链接('#login_display')
,单击时可向下滑动子菜单 '('.box_login')`。我想要的是,当有人将鼠标移出链接或子菜单时,子菜单会滑回。
这段代码是有效的,但是有更好的方法吗?因为当鼠标离开子菜单('.box_login')进入链接('#login_display')时,它滞后一点,导致链接打开子菜单('.box_login')然后鼠标移开将其关闭...
谢谢,
这是脚本:
$('#login_display').hover(function() {
$('.box_login').slideDown('normal');
}, function() {});
$('.box_login').hover(function() {}, function() {
$('.box_login').slideUp('normal');
});
I have a link('#login_display')
that slides down a sub-menu '('.box_login')` on click. What I would like is when someone mouseout of the link OR the sub-menu, the sub-menu slides back up.
This code is working, but is there a better way to do it? Because when the mouse leave the submenu('.box_login') to go on the link('#login_display') it lags a little bit cause the link opens the sub-menu('.box_login') and the mouse-out close it...
Thanks
here is the script:
$('#login_display').hover(function() {
$('.box_login').slideDown('normal');
}, function() {});
$('.box_login').hover(function() {}, function() {
$('.box_login').slideUp('normal');
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会将您的链接和幻灯片 div 放在一个容器中。然后利用容器的mouseleave事件向上滑动。
http://jsfiddle.net/Whrec/1
I would place your link and your slide down div in a container. Then use the mouseleave event of the container to slide back up.
http://jsfiddle.net/Whrec/1
查看您的网站,这最适合您。试试这个。
演示
Looking at your site, this is what will work best for you. Try this.
Demo
那么,您可以通过仅使用
mouseenter
和mouseout
事件来简化。同时缓存$('.box_login')
。然后使用$(this)
而不是重复$('.box_login')
,最后删除'normal'
因为它是默认速度。你的表现会有所提高,但我认为除此之外你没有什么可以做的。发布您的 html 可能有助于获得更多性能改进。Well, you can simplify by using just the
mouseenter
andmouseout
events. Also cache$('.box_login')
. Then use$(this)
instead of repeating$('.box_login')
and finally remove'normal'
since it's the default speed. Your performance will improve a little bit, but I don't think there's a whole lot you can do aside from this. Posting your html might help to get more performance improvements.