Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

javascript - 顺畅滚动(Smooth scroll on scroll)

I have a problem with this code ( https://codepen.io/MAJO-SEARCH/pen/gOOyWWb ).

(我对此代码有问题( https://codepen.io/MAJO-SEARCH/pen/gOOyWWb )。)

The problem is that the first time a page is loaded it is cut off a footer and after changing the width of the page everything returns to normal.

(问题在于,第一次加载页面时,页面的页脚会被剪切掉,更改页面的宽度后,一切都会恢复正常。)

I don't know how it is caused.

(我不知道是怎么引起的。)

Script which is responsible for it is placed at the bottom:

(负责该脚本的脚本位于底部:)

var lastPosition = 0;
var html = document.documentElement;
var body = document.body;
html.style.height = body.offsetHeight + 'px';
resizeHandler = function(){
  html.style.height = body.offsetHeight + 'px';
}
window.addEventListener('resize',resizeHandler,false);
clickHandler = function(){
wrapper.style.height = smooth.offsetHeight + 'px';
window.scrollTo(0,offsetTop);
smooth.style.transform = 'translate3d(0px, -' + toggle.offsetTop + 'px, 0px)';
lastPosition = window.pageYOffset;
return false;
}

var scroll = window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    function(callback) {
        window.setTimeout(callback, 1000 / 60)
    };
function loop() {
    if (lastPosition == window.pageYOffset) {
        scroll(loop);
        return false;
    } else lastPosition = window.pageYOffset;
    body.style.transform = 'translate3d(0px, -' + lastPosition + 'px, 0px)';
    scroll(loop);
  return true;
}
body.addEventListener('scroll',loop,true);
loop();
  ask by MAJO translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...