在jQuery中,"淡出"(fade out)是指通过动画效果逐渐降低元素的不透明度直到完全透明,"删除"(remove)则是将元素从DOM中完全移除。
$("#yourDivId").fadeOut();
$("#yourDivId").fadeOut(function() {
$(this).remove();
});
$("#yourDivId").fadeOut(1000, function() { // 1000毫秒(1秒)的淡出动画
$(this).remove();
});
duration
:动画持续时间,可以是毫秒数或字符串"slow"、"fast"easing
:动画的缓动函数(可选)complete
:动画完成后的回调函数<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
#myDiv {
width: 200px;
height: 200px;
background-color: #3498db;
color: white;
text-align: center;
line-height: 200px;
}
</style>
</head>
<body>
<div id="myDiv">点击我淡出并删除</div>
<button id="fadeBtn">淡出并删除</button>
<script>
$(document).ready(function() {
$("#fadeBtn").click(function() {
$("#myDiv").fadeOut(1000, function() {
$(this).remove();
alert("元素已被删除");
});
});
// 也可以直接点击div触发
$("#myDiv").click(function() {
$(this).fadeOut(500, function() {
$(this).remove();
});
});
});
</script>
</body>
</html>
.remove()
.stop()
方法中途停止.slideUp()
实现更丰富的动画如果不需要动画效果,可以直接删除:
$("#yourDivId").remove();
如果需要先隐藏再删除:
$("#yourDivId").hide().remove();
没有搜到相关的文章