【问题标题】:JS for smooth scroll to the bottom of the pageJS平滑滚动到页面底部
【发布时间】:2014-07-13 15:54:25
【问题描述】:

我有一个 JS 可以从页面底部平滑滚动到顶部,它可以工作:

<script>

     $("a[href='#top']").click(function() {
     $("html, body").animate({ scrollTop: 0 }, "slow");
     return true;
  });
</script>

但是现在我想做一个从上到下的平滑滚动,我用这个试了一下:

 <script>

     $("a[href='#footer']").click(function() {
     $("html, body").animate({ scrollToBottom: 0 }, "slow");
     return true;
  });
</script>`

它不起作用,它不是一个平滑的滚动。有谁知道这是怎么回事?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    纯JS:

     window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' })
    

    到顶部为:

     window.scrollTo({ top: 0, behavior: 'smooth' })
    

    【讨论】:

    • 这应该被标记为有效答案。谢谢!
    【解决方案2】:

    更全面的平滑滚动方法列表见我的回答here


    要滚动到页面底部,可以将y位置设置为document.scrollingElement.scrollHeight

    为了在准确的时间内滚动到某个位置,可以使用window.requestAnimationFrame,每次计算适当的当前位置。在不支持requestAnimationFrame 时,可以使用setTimeout 达到类似的效果。

    /*
       @param pos: the y-position to scroll to (in pixels)
       @param time: the exact amount of time the scrolling will take (in milliseconds)
    */
    function scrollToSmoothly(pos, time) {
        var currentPos = window.pageYOffset;
        var start = null;
        if(time == null) time = 500;
        pos = +pos, time = +time;
        window.requestAnimationFrame(function step(currentTime) {
            start = !start ? currentTime : start;
            var progress = currentTime - start;
            if (currentPos < pos) {
                window.scrollTo(0, ((pos - currentPos) * progress / time) + currentPos);
            } else {
                window.scrollTo(0, currentPos - ((currentPos - pos) * progress / time));
            }
            if (progress < time) {
                window.requestAnimationFrame(step);
            } else {
                window.scrollTo(0, pos);
            }
        });
    }
    

    function scrollToSmoothly(pos, time) {
        var currentPos = window.pageYOffset;
        var start = null;
        if(time == null) time = 500;
        pos = +pos, time = +time;
        window.requestAnimationFrame(function step(currentTime) {
            start = !start ? currentTime : start;
            var progress = currentTime - start;
            if (currentPos < pos) {
                window.scrollTo(0, ((pos - currentPos) * progress / time) + currentPos);
            } else {
                window.scrollTo(0, currentPos - ((currentPos - pos) * progress / time));
            }
            if (progress < time) {
                window.requestAnimationFrame(step);
            } else {
                window.scrollTo(0, pos);
            }
        });
    }
    
    document.querySelector('button').addEventListener('click', function(e){
      scrollToSmoothly(document.scrollingElement.scrollHeight, 1000);
    });
    html, body {
      height: 5000px;
    }
    &lt;button&gt;Scroll to bottom&lt;/button&gt;

    SmoothScroll.js library 也可以使用,它可以处理更复杂的情况,例如垂直和水平平滑滚动、在其他容器元素内滚动、不同的缓动行为、从当前位置相对滚动等等。

    smoothScroll({yPos: 'end', duration: 1000});
    

    document.querySelector('button').addEventListener('click', function(e){
      smoothScroll({yPos: 'end', duration: 1000});
    });
    html, body {
      height: 5000px;
    }
    <script src="https://cdn.jsdelivr.net/gh/LieutenantPeacock/SmoothScroll@1.2.0/src/smoothscroll.min.js" integrity="sha384-UdJHYJK9eDBy7vML0TvJGlCpvrJhCuOPGTc7tHbA+jHEgCgjWpPbmMvmd/2bzdXU" crossorigin="anonymous"></script>
    <button>Scroll to bottom</button>

    【讨论】:

      【解决方案3】:
      // Scroll smoothly to the bottom
      domElement.scrollTo({
        top: document.body.scrollHeight,
        behavior: 'smooth',
      })
      

      scrollTo 的所有选项是:

      • top: number
      • left: number
      • behavior: 'smooth'behavior: 'auto'

      【讨论】:

        【解决方案4】:

        没有scrollToBottom 这样的东西。试试这个:

        $("html, body").animate({ scrollTop: document.body.scrollHeight }, "slow");
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-14
        • 2021-01-11
        • 2012-07-27
        相关资源
        最近更新 更多