如何使用 jQuery animate() 隐藏和显示元素?
jqueryweb developmentfront end technology更新于 2026/1/28 15:22:17
使用 hide() 和 show() 方法以及 animate() 来隐藏和显示元素。
示例
您可以尝试运行以下代码,了解如何使用 animate() 方法隐藏和显示元素:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("#box").animate({height: "300px"}, 500, function() {
$(this).hide();
});
});
$("#btn2").click(function(){
$("#box").animate({height: "300px"}, 500, function() {
$(this).show();
});
});
});
</script>
</head>
<body>
<button id="btn1">Hide</button>
<button id="btn2">Show</button>
<div id="box" style="background:#000000;height:300px;width:100px;margin:10px;"></div>
</body>
</html>

