ScrollTo动画

时间:2012-08-30 14:36:15

标签: javascript scrollto

如何添加缓动/动画/慢慢移动到此功能? 此刻它只是跳跃。 现在它应该用动画移动到“锚点”。

<script type='text/javascript'>
        setTimeout("window.scrollBy(0,270);",3000);
</script>

9 个答案:

答案 0 :(得分:39)

也可以使用请求动画帧的普通javascript ..

// first add raf shim
// http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       ||
          window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame    ||
          function( callback ){
            window.setTimeout(callback, 1000 / 60);
          };
})();

// main function
function scrollToY(scrollTargetY, speed, easing) {
    // scrollTargetY: the target scrollY property of the window
    // speed: time in pixels per second
    // easing: easing equation to use

    var scrollY = window.scrollY,
        scrollTargetY = scrollTargetY || 0,
        speed = speed || 2000,
        easing = easing || 'easeOutSine',
        currentTime = 0;

    // min time .1, max time .8 seconds
    var time = Math.max(.1, Math.min(Math.abs(scrollY - scrollTargetY) / speed, .8));

    // easing equations from https://github.com/danro/easing-js/blob/master/easing.js
    var PI_D2 = Math.PI / 2,
        easingEquations = {
            easeOutSine: function (pos) {
                return Math.sin(pos * (Math.PI / 2));
            },
            easeInOutSine: function (pos) {
                return (-0.5 * (Math.cos(Math.PI * pos) - 1));
            },
            easeInOutQuint: function (pos) {
                if ((pos /= 0.5) < 1) {
                    return 0.5 * Math.pow(pos, 5);
                }
                return 0.5 * (Math.pow((pos - 2), 5) + 2);
            }
        };

    // add animation loop
    function tick() {
        currentTime += 1 / 60;

        var p = currentTime / time;
        var t = easingEquations[easing](p);

        if (p < 1) {
            requestAnimFrame(tick);

            window.scrollTo(0, scrollY + ((scrollTargetY - scrollY) * t));
        } else {
            console.log('scroll done');
            window.scrollTo(0, scrollTargetY);
        }
    }

    // call it once to get started
    tick();
}

// scroll it!
scrollToY(0, 1500, 'easeInOutQuint');

答案 1 :(得分:5)

改编自this answer

function scrollBy(distance, duration) {

    var initialY = document.body.scrollTop;
    var y = initialY + distance;
    var baseY = (initialY + y) * 0.5;
    var difference = initialY - baseY;
    var startTime = performance.now();

    function step() {
        var normalizedTime = (performance.now() - startTime) / duration;
        if (normalizedTime > 1) normalizedTime = 1;

        window.scrollTo(0, baseY + difference * Math.cos(normalizedTime * Math.PI));
        if (normalizedTime < 1) window.requestAnimationFrame(step);
    }
    window.requestAnimationFrame(step);
}

这样可以让您顺畅地滚动指定的距离。

答案 2 :(得分:3)

对于在2019年查看此问题的任何人:现在可以通过使用本机完成

window.scrollBy({
    top: 0,
    left: 270,
    behavior: 'smooth'
});

此功能在Edge和Safari之外的所有主要浏览器中均有效。参见https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollBy#Examples

答案 3 :(得分:2)

jQuery的另一个例子,使用缓动插件来获得一些不错的效果:

http://tympanus.net/codrops/2010/06/02/smooth-vertical-or-horizontal-page-scrolling-with-jquery/

答案 4 :(得分:2)

自己弄明白了。因为wordpress和jquery.noConflict模式我要修改代码:

<script type="text/javascript">
        (function($){
        $(document).ready(function(){
            setTimeout(function() {
            $('body').scrollTo( '300px', 2500 );
        }, 3000);
        });
        }(jQuery));
</script>

感谢大家!!!

答案 5 :(得分:1)

使用jQuery时,您可以轻松使用.animate功能。

Here's an example on how it should work.

答案 6 :(得分:0)

使用jQuery可以更容易,也许使用scrollto插件。 http://flesler.blogspot.se/2007/10/jqueryscrollto.html

考虑一个解决方案:

<script type='text/javascript' src='js/jquery.1.7.2.min.js'></script>
<script type='text/javascript' src='js/jquery.scrollTo-min.js'></script>
<script type='text/javascript' src='js/jquery.easing.1.3.js'></script><!-- only for other easings than swing or linear -->
<script type='text/javascript'>
$(document).ready(function(){
    setTimeout(function() {
    $('html,body').scrollTo( {top:'30%', left:'0px'}, 800, {easing:'easeInBounce'} );
}, 3000);
});
</script>

当然你需要编写脚本。

有关工作示例,请参阅http://jsfiddle.net/7bFAF/2/

答案 7 :(得分:0)

这将起作用,假设您需要平滑滚动到页面顶部。

const scrollToTop = () => {
  const c = document.documentElement.scrollTop || document.body.scrollTop;
  if (c > 0) {
    window.requestAnimationFrame(scrollToTop);
    window.scrollTo(0, c - c / 8);
  }
};

答案 8 :(得分:-3)

使用此功能,您的问题将得到解决

animate(document.documentElement, 'scrollTop', 0, 200);

由于