scrollintoview动画

Ted*_*rer 33 scroll smooth js-scrollintoview

我的代码在http://jsfiddle.net/mannagod/QT3v5/7/.

JS是:

function delay() {
    var INTENDED_MONTH = 7 //August
    // INTENDED_MONTH is zero-relative
    now = new Date().getDate(),
rows = document.getElementById('scripture').rows;
    if (new Date().getMonth() != INTENDED_MONTH) {
        // need a value here less than 1, 
        // or the box for the first of the month will be in Red
        now = 0.5
    };
    for (var i = 0, rl = rows.length; i < rl; i++) {
        var cells = rows[i].childNodes;
        for (j = 0, cl = cells.length; j < cl; j++) {
            if (cells[j].nodeName == 'TD'
  && cells[j].firstChild.nodeValue != ''
  && cells[j].firstChild.nodeValue == now) {
                // 'ffff99' // '#ffd700' // TODAY - red
                rows[i].style.backgroundColor = 'red' 
                rows[i].scrollIntoView();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要找到一种方法来平滑.scrollintoview().现在它"跳"到突出显示的行.我需要它顺利过渡到那一行.它需要动态完成以替换scrollintoview.有任何想法吗?谢谢.

小智 68

在大多数现代浏览器(Chrome和Firefox,但不包括Safari,UC或IE)中,您可以将对象中的选项传递给.scrollIntoView().

试试这个:

elm.scrollIntoView({ behavior: 'smooth', block: 'center' })
Run Code Online (Sandbox Code Playgroud)

其他值是behavior: 'instant'block: 'start'block: 'end'.请参阅https://developer.mozilla.org/en/docs/Web/API/Element/scrollIntoView

  • 嗯,Firefox比Chrome做得更好 (5认同)
  • 如今它适用于所有主要浏览器。唯一的兼容性问题是不完全兼容的流畅行为:https://caniuse.com/#feat=scrollintoview (2认同)
  • 使用 polyfill 使 scrollintoviewoptions 浏览器独立,您就完成了 :) /sf/ask/2975251961/#55357885 (2认同)

Sar*_*han 32

我也在搜索这个问题并找到了这个解决方案:

$('html, body').animate({
    scrollTop: $("#elementId").offset().top
}, 1000);
Run Code Online (Sandbox Code Playgroud)

资源:http: //www.abeautifulsite.net/smoothly-scroll-to-an-element-without-a-jquery-plugin-2/

  • 这是最好的答案.它可以平滑地滚动到所需的位置.谢谢! (4认同)

小智 13

也许您不想仅为实现此功能而添加jQuery.elem是要滚动的元素.目标pos可以从要移动到视图中的元素的offsetTop属性中获取.

function Scroll_To(elem, pos)
{
    var y = elem.scrollTop;
    y += (pos - y) * 0.3;
    if (Math.abs(y-pos) < 2)
    {
        elem.scrollTop = pos;
        return;
    }
    elem.scrollTop = y;
    setTimeout(Scroll_To, 40, elem, pos);   
}
Run Code Online (Sandbox Code Playgroud)

  • 我遇到了无限递归的问题.我用`Math.round((pos - y)*0.3);`和`Math.abs(y-pos)<= 2来修复它 (2认同)

Zre*_*ren 7

requestAnimationFrame在没有jQuery的情况下使用特定持续时间进行平滑滚动.

演示:http://codepen.io/Shadeness/pen/XXyvKG?edit = 0010

window.bringIntoView_started = 0;
window.bringIntoView_ends = 0;
window.bringIntoView_y = 0;
window.bringIntoView_tick = function() {
  var distanceLeft, dt, duration, t, travel;
  t = Date.now();
  if (t < window.bringIntoView_ends) {
    dt = t - window.bringIntoView_started;
    duration = window.bringIntoView_ends - window.bringIntoView_started;
    distanceLeft = window.bringIntoView_y - document.body.scrollTop;
      travel = distanceLeft * (dt / duration);
      document.body.scrollTop += travel;
      window.requestAnimationFrame(window.bringIntoView_tick);
  } else {
    document.body.scrollTop = window.bringIntoView_y;
  }
};
window.bringIntoView = function(e, duration) {
  window.bringIntoView_started = Date.now();
  window.bringIntoView_ends = window.bringIntoView_started + duration;
  window.bringIntoView_y = Math.min(document.body.scrollTop + e.getBoundingClientRect().top, document.body.scrollHeight - window.innerHeight);
  window.requestAnimationFrame(window.bringIntoView_tick);
};
Run Code Online (Sandbox Code Playgroud)

例如:

bringIntoView(document.querySelector('#bottom'), 400)
Run Code Online (Sandbox Code Playgroud)

随着dt(deltaTime)变大,它应该加速,并且随着distanceLeftget变小而变慢.如果用户滚动但我认为我打破了循环.全局变量阻止先前的调用完全接管,但不会取消先前的递归循环,因此它的动画速度会快两倍.