美文网首页
返回顶部

返回顶部

作者: Devin_tao | 来源:发表于2019-11-26 17:10 被阅读0次

    jQuery

    $('.totop').on('click', function () {
                $('body,html').animate({
                    scrollTop: 0,
                }, 80);
            })
    

    原生

    var topBtn = document.querySelector('.totop');
            
            function totop(speed, delay) {
                var timer = null;
    
                timer = setInterval(function () {
                    var currentScroll = document.documentElement.scrollTop || document.body.scrollTop;
                    document.documentElement.scrollTop = document.body.scrollTop = currentScroll - speed;
                    if (currentScroll == 0) {
                        clearInterval(timer);
                    }
                }, delay);
            }
    
            topBtn.addEventListener('click', function () {
                totop(50, 10);
            })
    

    相关文章

      网友评论

          本文标题:返回顶部

          本文链接:https://www.haomeiwen.com/subject/rlrywctx.html