使用动画滚动到

ScrollTo with animation

本文关键字:滚动 动画      更新时间:2023-09-26

如何向该函数添加放松/动画/缓慢移动?此刻它只是跳跃。现在,它应该移动到带有动画的"锚点"。

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

也可以使用请求动画帧的纯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');

对于2019年看到这个问题的人来说:现在可以通过使用本地完成

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

这适用于除edge和safari之外的所有主要浏览器。看见https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollBy#Examples

改编自以下答案:

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);
}

这将允许您平滑地滚动指定的距离。

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

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

jQuery的另一个例子使用了轻松插件来获得一些不错的效果:

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

自己得到的。因为wordpress和jquery.noConflictMode i hade修改了代码:

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

谢谢大家!!!

使用jQuery时,可以很容易地使用.animate函数。

下面是一个关于它应该如何工作的例子。

使用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>

当然,你需要dl脚本。

请参阅http://jsfiddle.net/7bFAF/2/对于一个工作示例

我们可以通过使用css属性滚动行为来简化它

html {
  scroll-behavior: smooth;
}