JS实现slideDown()和slideUp()效果

JS实现slideDown()和slideUp()效果

这两天在写一个小Demo,页面顶部需要实现webAPP那种将导航收起,通过按钮点击显示与隐藏的效果。实现过程中也花费时间挺久的,现在单独将这部分拎出来记录一下。废话太多了,直接上代码:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>响应式</title>
<style type="text/css">
*{
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}
a{
	text-decoration: none;
	color: inherit;
}
ul li{
	list-style-type: none;
}

header{
	width: 100%;
	text-align: center;
}
.codeHead{
	width: 100%;
	height: 70px;
	line-height: 70px;
	color: #fff;
	display: inline-block;
	text-align: center;
	font-size: 22px;
	position: relative;
	z-index: 99;
	background-color: #7ea6b1;
}
#nav-btn{
	height: 20px;
	width: 20px;
	display: inline-block;
	border: 1px solid #fff;
	position: absolute;
	top: 25px;
	right: 30px;
	color: #fff;
	background-color: #7ea6b1;
}
#nav-list{
	width: 100%;
	background-color:#7ea6b1;
	position: relative;
	z-index: 2;
	height: 0;
	overflow: hidden;
}
#nav-list li{
	width: 100%;
	color: white;
	font-size: 14px;
	line-height: 30px;
	text-align: center;
	border-top: 1px solid #fff;
}	
</style>
</head>
<body>
	<header>
		<nav>
			<div class="codeHead">
				<div>let's code</div>
				<button id="nav-btn">=</button>
			</div>
			<ul id="nav-list">
				<li>首页</li>
				<li>Java</li>
				<li>ios</li>
				<li>Android</li>
				<li>php</li>
			</ul>
		</nav>
	</header>
</body>
<script type="text/javascript">
	function menu(){
		var navList = document.getElementById('nav-list');
		console.log(navList);
		var navBtn = document.getElementById('nav-btn');
		console.log(navBtn);	

		navBtn.onclick = function(){
			var i = 156;
			var j = 0;
			if(!navList.offsetHeight){
				var idOut = setInterval(frameOut, 1);
				function frameOut() {
					if (j == 156) {
						clearInterval(idOut);
					} else {
						j++; 
						navList.style.height = j + 'px';  
					}
					console.log("j==155:" + navList.offsetHeight);
				}
			}else {
				var idIn = setInterval(frameIn, 1);
				function frameIn() {
					if (i == 0) {
						clearInterval(idIn);
					} else {
						i--; 
						navList.style.height = i + 'px';
					}
					console.log("i==0:" + navList.offsetHeight);
				}
			}
			console.log("click");
		}
		console.log("menu.navList.style.height:",navList.offsetHeight);
	} 

	menu();

</script>
</html>
实现过程中遇到的坑:

z-index无效的情况,一共有三种:

  1. 父标签 position属性为relative;
  2. 问题标签无position属性(不包括static);
  3. 问题标签含有浮动(float)属性。

关于div高度
document.style.height只能获取内联样式的元素的高度。
document.offsetHeight获取除margin外的宽和高。比较常用。但是通过其设置div的宽高没设置成功,建议设置高度使用document.style.height。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用原生 JavaScript 实现 jQuery 中 slideUp 和 slideDown 效果的示例: HTML 代码: ```html <button id="btn">Toggle</button> <div id="box" style="height: 200px; background-color: yellow;">Content</div> ``` JavaScript 代码: ```javascript var box = document.getElementById('box'); var btn = document.getElementById('btn'); function slideUp() { var height = box.offsetHeight; box.style.transitionProperty = 'height, margin, padding'; box.style.transitionDuration = '0.5s'; box.style.boxSizing = 'border-box'; box.style.height = height + 'px'; box.offsetHeight; box.style.overflow = 'hidden'; box.style.height = 0; } function slideDown() { var height = box.offsetHeight; box.style.transitionProperty = 'height, margin, padding'; box.style.transitionDuration = '0.5s'; box.style.boxSizing = 'border-box'; box.style.height = 0; box.offsetHeight; box.style.overflow = 'hidden'; box.style.height = height + 'px'; } btn.addEventListener('click', function() { if (box.classList.contains('open')) { box.classList.remove('open'); slideUp(); } else { box.classList.add('open'); slideDown(); } }); ``` 在这个例子中,我们使用了 `transition` 属性来实现动画效果,并使用 `setTimeout` 函数来确保 CSS 的变化应用于 DOM 元素。由于 JavaScript 与 CSS 的执行顺序不同,我们需要使用 `offsetHeight` 属性来强制 JavaScript 引擎重新计算元素的高度,以便确保动画效果可以正确应用。 注意,在这个示例中,我们使用了一个 `.open` 类来标识盒子是否处于打开状态。这个类可以用来在 JavaScript 中切换 slideUp 和 slideDown 效果

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值