我正在此页面上工作:http://i333180.iris.fhict.nl/p2_vc/
在您的帮助下,我已经成功添加了平滑滚动插件。
但是,第一次打开页面时,我注意到很多滚动滞后。
我尝试过的内容
<div id="div_section_img">
<!-- all content -->
</div>
#div_section_img{
background: url("nature.png") no-repeat center center fixed;
}
100vh
。 我设法通过将
margin-top
和padding-top
元素上的#logo
设置更改为footer
来解决此问题。#logo {
width: 410px;
**padding-top: 120px;**
margin-left: auto;
margin-right: auto;
margin-bottom: 0px;
}
footer {
**padding-top: 100px;**
width: 100%;
height: 300px;
background-color: rgba(25, 25, 25, 0.8);
}
尽管所有这些都有所帮助,但滚动延迟仍然非常明显。
如何摆脱滚动延迟?
smooth-scroll.js
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
From this css-tricks tutorial
更新
尝试过此脚本而不是其他脚本
function start(){
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
function videoEnded() {
$('html,body').animate({
scrollTop: $("#section").offset().top
}, 1000);
}
}
window.onload = start;
但仍然落后
更新2
添加
var loading = $('html');
loading.addClass('loading');
$(window).load(function(){
loading.removeClass('loading');
});
代码有效,延迟仍然很明显
最佳答案
您需要在页面加载时隐藏页面,以便用户在所有内容加载完毕后才开始滚动。
html
标签添加一个类,该标签隐藏body标签,并将加载图像添加到html
标签的背景var loading = $('html');
loading.addClass('loading');
$(window).load(function(){
loading.removeClass('loading');
});
html.loading {
height: 100%;
background-image: url('http://i.imgur.com/HEAhF9v.gif'); /* Animated loading spinner image */
background-position: center center;
background-repeat: no-repeat
}
html.loading body {
visibility: hidden;
height: 100%;
overflow: hidden;
}
<!-- For demo purposes only -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img src="//lorempixel.com/1024/768">