javascript - 顺利追加内容到div
问题描述
我的网站在从菜单中选择后加载“子页面”并将其附加到一个空的隐藏 div。
它正在工作,但我对它的感觉并不满意。交换 div 的内容时,网站会出现口吃/闪烁。
所以,我正在做的是这样的:
HTML
<div class="button" id="page1">Button for link</div>
<div class="subContent">
<div class="containerContentBox textLeft">
<div id="placeContent"><!-- Content goes here --></div>
</div>
</div>
在 Javascript 中,我试图通过淡出主 div 来平滑过渡,更改内容,然后将其淡入,如下所示:
JS
var chosenPage = "";
// Click button
$('.button').click(function(){
chosenPage = $(this).attr('id');
$('#placeContent').fadeOut('fast', function(){
$('#placeContent').empty();
showSelectedPage();
});
});
// Apply the page
function showSelectedPage(){
var newPageFile = "/pages/" + chosenPage + ".html";
$.get(newPageFile)
.done(function(data) {
$('#placeContent').html(data);
$('#placeContent').fadeIn('fast', function(){
scrollToContent();
});
}).fail(function() {
console.log('Page not found');
return;
});
}
// Scroll to top of the appended page
function scrollToContent(){
$('html,body').animate({
scrollTop: $(".subContent").offset().top - 40
}, 'slow');
}
有没有更好的方法来做到这一点?我认为闪烁/口吃来自滚动条改变高度。
解决方案
推荐阅读
- linux - 如何像windows一样在linux中的文件中添加隐藏属性?
- python-3.x - 如何下载存储在 django 文件夹中的文件
- python - 如何从基于类的视图中将模型类属性作为上下文传递
- oracle - 用于插入/更新列的触发器在 Oracle 中不起作用
- css - 在 flexbox 布局中,`order` 属性会改变项目的绘制顺序吗?
- python-3.x - 在某些操作之前或之后将变量声明为实例变量
- javascript - 带有 for 循环的 JavaScript Promise
- python - TypeError: 'numpy.float64' 对象在计算 z 分数时不可迭代
- javascript - 为什么在 Laravel 中导出为 EXCEL 文件时,当月份为两位数时,jQuery 数据表无法正确导出日期?
- react-router - 如果路线与路线列表匹配,则仅渲染第一条路线,但如果没有满足这些路线,则可能呈现多个