【问题标题】:scrollTop animation without jquery [duplicate]没有jquery的scrollTop动画[重复]
【发布时间】:2014-02-23 19:54:26
【问题描述】:

我正在尝试在不使用 jQuery 的情况下制作动画“滚动到顶部”效果。

在jQuery中,我通常使用这样的代码:

$('#go-to-top').click(function(){ 
      $('html,body').animate({ scrollTop: 0 }, 400);
      return false; 
});

如何在不使用 jQuery 的情况下为 scrollTop 设置动画?

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

HTML:

<button onclick="scrollToTop(1000);"></button>

1# JavaScript(线性):

function scrollToTop (duration) {
    // cancel if already on top
    if (document.scrollingElement.scrollTop === 0) return;

    const totalScrollDistance = document.scrollingElement.scrollTop;
    let scrollY = totalScrollDistance, oldTimestamp = null;

    function step (newTimestamp) {
        if (oldTimestamp !== null) {
            // if duration is 0 scrollY will be -Infinity
            scrollY -= totalScrollDistance * (newTimestamp - oldTimestamp) / duration;
            if (scrollY <= 0) return document.scrollingElement.scrollTop = 0;
            document.scrollingElement.scrollTop = scrollY;
        }
        oldTimestamp = newTimestamp;
        window.requestAnimationFrame(step);
    }
    window.requestAnimationFrame(step);
}

2# JavaScript(缓入缓出):

function scrollToTop (duration) {
    // cancel if already on top
    if (document.scrollingElement.scrollTop === 0) return;

    const cosParameter = document.scrollingElement.scrollTop / 2;
    let scrollCount = 0, oldTimestamp = null;

    function step (newTimestamp) {
        if (oldTimestamp !== null) {
            // if duration is 0 scrollCount will be Infinity
            scrollCount += Math.PI * (newTimestamp - oldTimestamp) / duration;
            if (scrollCount >= Math.PI) return document.scrollingElement.scrollTop = 0;
            document.scrollingElement.scrollTop = cosParameter + cosParameter * Math.cos(scrollCount);
        }
        oldTimestamp = newTimestamp;
        window.requestAnimationFrame(step);
    }
    window.requestAnimationFrame(step);
}
/* 
  Explanation:
  - pi is the length/end point of the cosinus intervall (see below)
  - newTimestamp indicates the current time when callbacks queued by requestAnimationFrame begin to fire.
    (for more information see https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame)
  - newTimestamp - oldTimestamp equals the delta time

    a * cos (bx + c) + d                        | c translates along the x axis = 0
  = a * cos (bx) + d                            | d translates along the y axis = 1 -> only positive y values
  = a * cos (bx) + 1                            | a stretches along the y axis = cosParameter = window.scrollY / 2
  = cosParameter + cosParameter * (cos bx)  | b stretches along the x axis = scrollCount = Math.PI / (scrollDuration / (newTimestamp - oldTimestamp))
  = cosParameter + cosParameter * (cos scrollCount * x)
*/

注意:

  • 以毫秒为单位的持续时间 (1000ms = 1s)
  • 第二个脚本使用 cos 函数。 Example curve:

Github 上的 3# Simple scrolling library

【讨论】:

  • 请记住,const 不是 EcmaScript 5 的一部分,而是具有不同语义的 6。目前只有 Firefox 和 Chrome 以某种方式支持它。在此处阅读更多信息:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 是的,不错的对象。因此,在所有浏览器都支持 const 之前,使用 var 可能是更好的选择。
  • 我能看到这个的 JSFIDDLE 吗?我似乎无法让它工作。 . .
  • 谢谢,效果很好!
  • 谢谢!我对其进行了一些扩展,以允许在没有 performance.now() 的情况下滚动到特定元素和浏览器的 polyfill ...gist.github.com/joshcanhelp/a3a669df80898d4097a1e2c01dea52c1
猜你喜欢
  • 2012-04-25
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多