码迷,mamicode.com
首页 > 其他好文 > 详细

回到顶部bug

时间:2018-07-15 16:09:29      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:gets   isp   lse   click   问题   The   top   cli   osc   

参考自一博客(https://www.cnblogs.com/abao0/p/6642288.html)内有慕课网教程(后发现有bug, 弃置不用了)

以下有问题, 当滚动条处于顶部时, 刷新页面, 回到顶部icon依然会出现.

//Page loading trigger
window.onload = function () {
    var oscrollBtn = document.getElementById(‘scrollBtn‘);
    var timer = null;
    var isTop = true;
    //Gets the height of the visible window
    var clientHeight = document.documentElement.clientHeight;
    //Eliminate the problem of returning the top icon when it is at the top
    var osTop = document.documentElement.scrollTop || document.body.scrollTop;
        if (osTop >= clientHeight) {
            oscrollBtn.style.display = ‘block‘;
        } else {
            oscrollBtn.style.display = ‘none‘;
    }
    //Triggered when the scroll bar rolls
    window.onscroll = function () {
        var osTop = document.documentElement.scrollTop || document.body.scrollTop;
        if (osTop >= clientHeight) {
            oscrollBtn.style.display = ‘block‘;
        } else {
            oscrollBtn.style.display = ‘none‘;
        }
        if (!isTop) {
            clearInterval(timer);
        }
        isTop = false;
    }
    oscrollBtn.onclick = function () {
        //Set timer
        timer = setInterval(function () {
            //Gets the height of the scroll bar
            var osTop = document.documentElement.scrollTop || document.body.scrollTop;
            //The scroll bar slows down
            var ispeed = Math.ceil(osTop / 6);
            document.documentElement.scrollTop = document.body.scrollTop = osTop - ispeed;
            isTop = true;
            if (osTop == 0) {
                clearInterval(timer);
            }
        }, 30);

    }
}

 

回到顶部bug

标签:gets   isp   lse   click   问题   The   top   cli   osc   

原文地址:https://www.cnblogs.com/yadongliang/p/9313583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!