javascript - 在使用 Jquery 加载新内容之前淡入 div
问题描述
我有一个父 div(容器类)和一个子 div(家庭类)。
我想在使用 Jquery 在其中加载新的子 div(约类)之前淡出父 div。
这是代码:
$(document).ready(function() {
$('div.container').on('click', '#about-btn', function() {
$('div.container').fadeOut(1000);
$('div.container').fadeIn(1000).load('ABOUT.html');
});
});
* {
padding: 0;
margin: 0;
}
.layout {
background-color: black;
height: 100vh;
}
.container {
height: 70%;
width: 100%;
}
.home,
.about {
height: 100%;
width: 100%;
background-color: cyan;
}
.bottom-nav {
height: 30%;
width: 100%;
background-color: brown;
}
<div class="layout">
<div class="container">
<div class="home">
<button id="about-btn">About</button>
<h1>HOME</h1>
</div>
</div>
<div class="bottom-nav">
<h1>Bottom Nav</h1>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
关于.html
<div class="about">
<button id="home-btn">Home</button>
<h1>ABOUT</h1>
</div>
我怎样才能做到这一点 ?
解决方案
感谢威尔斯的建议。这是使用 Ajax 的解决方案:
$(document).ready(function() {
$('div.container').on('click', '#about-btn', function() {
$('div.home').fadeOut(500, function() {
$.ajax({
type: "GET",
url: "ABOUT.html",
data: {},
success: function(data) {
$('div.container').hide().fadeIn(500).html(data);
}
});
});
});
});
* {
padding: 0;
margin: 0;
}
.layout {
background-color: black;
height: 100vh;
}
.container {
height: 70%;
width: 100%;
}
.home,
.about {
height: 100%;
width: 100%;
background-color: cyan;
}
.bottom-nav {
height: 30%;
width: 100%;
background-color: brown;
}
<div class="layout">
<div class="container">
<div class="home">
<button id="about-btn">About</button>
<h1>HOME</h1>
</div>
</div>
<div class="bottom-nav">
<h1>Bottom Nav</h1>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
推荐阅读
- javascript - 从 JS 对象中获取名称为“值”的字段
- python - 我是否让我的代码对自己来说太复杂了?
- ruby - 未加载 Ruby/Linux 已安装的 gem
- r - 数据框中的字符串替换,同时在 R 中保持其他列不受影响
- python - Python:将命令输出存储在变量中
- ffmpeg - FFMPEG - 你可以在脚本文件中添加评论吗?
- laravel - 如何在 laravel 的 div 部分中传递输入?
- proxy - 带有 python3 和机器人框架的 OWASP ZAP docker 文件
- pandas - 按更高级别的列合并数据框
- java - 是否可以在 Apache POI XWPF 中设置默认表格样式?