jQuery:如何创建 jQuery 悬停功能?
我想创建两个淡入和淡出元素的函数。
这就是我到目前为止所做的,但问题是,如果您在悬停元素中移动鼠标,它就会开始振动:|我该如何让它不振动并正常工作?
<script language="javascript" type="text/javascript">
function hoverIn(target, speed){
$(target).fadeIn(speed);
}
function hoverOut(target, speed){
$(target).fadeOut(speed);
}
</script>
I want to create two functions what fades in and out an element.
This is what I have done so far, but the problem with this is that if you move the mouse while you are in the hovered element it starts vibrating :| How should I make it to not vibrate and work correctly?
<script language="javascript" type="text/javascript">
function hoverIn(target, speed){
$(target).fadeIn(speed);
}
function hoverOut(target, speed){
$(target).fadeOut(speed);
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更好的方法是使用不显眼的脚本,您想要的主要内容是调用
.stop()
来停止前面的动画,如下所示:这仍然是一个问题,因为
鼠标悬停
和mouseout
在进入/离开孩子时触发。但是,.hover()
使用mouseenter
和mouseleave
不会遇到同样的问题,并且会消除您的“振动”问题。不显眼的版本如下所示:
您可以在此处查看,或者更短的版本:
您可以在此处进行测试,请注意,所有这些不显眼的方法都不使用任何内联 JavaScript,并且适用于多个子菜单。
A better way to do this would be using unobtrusive script, the main thing you want is a
.stop()
call to stop the previous animation, like this:This is still a problem because
mouseover
andmouseout
fire when entering/leaving a child. However,.hover()
usesmouseenter
andmouseleave
which don't suffer the same problem, and will eliminate your "vibration" problem.The unobtrusive version looks like this:
You can see it here, or the even shorter version:
You can test that here, note that all of these unobtrusive approaches don't use any inline javascript, and work for multiple child menus.