Jquery:解决鼠标移入移出多次触发的问题

jq的鼠标移入移除事件:




mouseover()鼠标移入(移入子元素也触发)


mouseout()鼠标移出(离开子元素也触发)


mouseenter()鼠标移入(移入子元素不触发)


mouseleave()鼠标移出(离开子元素不触发)


hover()鼠标移入移出(mouseenter与mouseleave的缩写)

示例代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>鼠标移入移除</title>
		<style type="text/css">
			.parent {
				width: 200px;
				height: 100px;
				margin: 100px auto;
				padding: 20px 0;
				background-color: pink;
				cursor: pointer;
			}
			.child {
				width: 80px;
				height: 20px;
				margin: 0 auto;
				background-color: yellow;
				text-align: center;
			}
			.drop {
				display: none;
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%,-50%);
				width: 300px;
				height: 200px;
				background-color: blue;
			}
		</style>
	</head>
	<body>
		<div class="parent">
			<div class="child">我是子元素</div>
			<div class="drop">我是鼠标移入显示的内容</div>
		</div>
		<script type="text/javascript" src="https://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
		<script type="text/javascript">
			// mouseover 进入子元素也触发
			// $('.parent').on('mouseover',function(){
			// 	$(this).parent().find('.drop').fadeIn();
			// })
			// $('.parent').on('mouseout',function(){
			// 	$(this).parent().find('.drop').fadeOut();
			// })
			// mouseover 进入子元素不触发
			// $('.parent').on('mouseenter',function(){
			// 	$(this).parent().find('.drop').fadeIn();
			// })
			// $('.parent').on('mouseleave',function(){
			// 	$(this).parent().find('.drop').fadeOut();
			// })
			$('.parent').hover(function(){
				$(this).parent().find('.drop').stop(false, true).fadeIn();
			},function(){
				$(this).parent().find('.drop').stop(false, true).fadeOut();
			})
		</script>
	</body>
</html>

分析:


可以看出我的代码中使用了stop(),如果不使用效果则是移入移出多次,就会不停的显示显示隐藏,stop的作用就是结束当前动画。

stop的在JQ中的使用场景:


stop();//停止当前动画,继续下一个动画


stop(true);//清除元素的所有动画


stop(false, true);//让当前动画直接到达末状态 ,继续下一个动画


stop(true, true);//清除元素的所有动画,让当前动画直接到达末状态

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上网的虫不叫网虫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值