Javascript smoothscrolling仍然落后
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Javascript smoothscrolling仍然落后,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2723字,纯文字阅读大概需要4分钟。
内容图文
![Javascript smoothscrolling仍然落后](/upload/InfoBanner/zyjiaocheng/772/70d77cd95d6b4f48ac83b431e7955449.jpg)
我正在写这个页面:http://i333180.iris.fhict.nl/p2_vc/
经过你们中的一些人的帮助,我成功添加了一个流畅的滚动插件.
但是,当首次打开页面时,我注意到了很多滚动延迟.
我试过的
>我将背景图像从10 mb压缩到2.2mb(1280 x 1024).
>我将背景图像移动到div元素,因此它仅在内容上而不在视频后面.
<div id="div_section_img">
<!-- all content -->
</div>
#div_section_img{
background: url("nature.png") no-repeat center center fixed;
}
>这样做,视频和内容部分之间出现了一条奇怪的“空白”行.我认为这是因为视频高度设置为100vh.
我设法通过将#logo和页脚元素的margin-top设置更改为padding-top来解决此问题.
#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);
}
尽管所有这些都有所帮助,但仍然存在非常明显的滚动滞后量.
如何摆脱滚动滞后?
光滑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;
}
}
});
});
>甚至卷顶5000也是滞后的
更新
尝试使用此脚本而不是其他脚本
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');
});
代码工作,滞后仍然明显
解决方法:
您需要在加载页面时隐藏页面,以便用户在加载完所有内容后不会开始滚动.
>将类添加到隐藏body标签的html标记中,并将加载图像添加到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">
内容总结
以上是互联网集市为您收集整理的Javascript smoothscrolling仍然落后全部内容,希望文章能够帮你解决Javascript smoothscrolling仍然落后所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。