scrollIntoView Scrolls just too far

前端 未结 21 2030
盖世英雄少女心
盖世英雄少女心 2020-12-07 10:08

I have a page where a scroll bar containing table rows with divs in them is dynamically generated from the database. Each table row acts like a link, sort of like you\'d see

21条回答
  •  -上瘾入骨i
    2020-12-07 10:54

    This works for me in Chrome (With smooth scrolling and no timing hacks)

    It just moves the element, initiates the scroll, then moves it back.

    There is no visible "popping" if the element is already on the screen.

    pos = targetEle.style.position;
    top = targetEle.style.top;
    targetEle.style.position = 'relative';
    targetEle.style.top = '-20px';
    targetEle.scrollIntoView({behavior: 'smooth', block: 'start'});
    targetEle.style.top = top;
    targetEle.style.position = pos;
    

提交回复
热议问题