定时器防抖案例

70 篇文章 1 订阅

HTML:

<body>
		<div class="wrapper">
			<div class="center">

			</div>
			<div class="main">
				<div class="left">
					<a href="#" class="sy">首页</a>
					<a href="#" class="jcss clickColor">基础设施</a>
					<a href="#" class="sjzy">数据资源</a>
				</div>
				<div class="right">
					<a href="#" class="yyzc">应用支撑</a>
					<a href="#" class="bzgf">标准规范</a>
					<a href="#" class="aqbz">安全保障</a>
				</div>
			</div>
		</div>

	</body>

CSS:

body {
	background-color: #000000;
}

.wrapper {
	width: 1920px;
	height: 90px;
	overflow: hidden;
	position: relative;
}
.center{
	width: 25%;
	height: 85%;
	position: absolute;
	left: 50%;
	top: 0%;
	transform: translate(-50%,0%);
	cursor: pointer;
}

.main {
	width: 100%;
	height: 100%;
	display: flex;
	justify-content: space-around;
	align-items: center;
	background-color: transparent;
	background: url(../img/nav.png) center no-repeat;
	background-size: contain;
	overflow: hidden;
	transition: transform 1.5s;
}
/*初始化移动出去*/
.move {
	transform: translateY(-70%);
}
.clickColor{
	color: #FED340 !important;
}

.left,
.right {
	width: 26%;
	height: 100%;
	display: flex;
	justify-content: center;
	align-items: center;
	overflow: hidden;
}

.left {
	margin-right: 140px;
}

.main a {
	flex: 1;
	font-family: "微软雅黑";
	font-size: 24px;
	color: #A5D6FE;
	text-align: center;
}

a:hover {
	color: #FED340;
}

.jcss,
.bzgf {
	border-left: 1.7px solid #44B4EB;
	border-right: 1.7px solid #44B4EB;
	border-radius: 2px;
}

需求:

鼠标划入导航出现,鼠标移出导航2秒内隐藏。

 

核心JS:

	let enterTime;
	let leaveTime;
	let timer;
	$(".wrapper").hover(function() {
		enterTime = Date.now(); // 鼠标划入的时的时间
		// 如果鼠标刚离开又瞬间划入,并且setTimeOut还没有执行完,
		// 此时鼠标第二次移入的时间减去第一次离开的时间间隔小于2000毫秒时,
		// 清除定时器(表示刚离开又想看添加“后悔”的过程)
		if((enterTime - leaveTime) <= 2000) {
			clearTimeout(timer);
		}
		$(".main").removeClass("move");
	}, function() {
		leaveTime = Date.now(); // 鼠标离开的时间
		timer = setTimeout(function() {
			$(".main").addClass("move");
		}, 2000);
	});

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

图图小淘气_real

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

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

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

打赏作者

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

抵扣说明:

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

余额充值