css平滑滚动

jQuery实现方法:

$('#div').animate({
    scrollTop: 100px,
    scrollLeft: 100px
});

原生实现方法:

var scrollSmoothTo = function (position) {
    if (!window.requestAnimationFrame) {
        window.requestAnimationFrame = function(callback, element) {
            return setTimeout(callback, 17);
        };
    }
    // 当前滚动高度
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    // 滚动step方法
    var step = function () {
        // 距离目标滚动距离
        var distance = position - scrollTop;
        // 目标滚动位置
        scrollTop = scrollTop + distance / 5;
        if (Math.abs(distance) < 1) {
            window.scrollTo(0, position);
        } else {
            window.scrollTo(0, scrollTop);
            requestAnimationFrame(step);
        }
    };
    step();
};
// 使用方法
scrollSmoothTo(0);

css平滑滚动样式:

body {
    scroll-behavior:smooth;
}

scrollIntoView方法:

dom.scrollIntoView({
    behavior: "smooth"
});