Scroll to element only if not in view - jQuery

前端 未结 10 1238
醉酒成梦
醉酒成梦 2020-12-13 18:26

I know a variation on this has been asked several times; I\'ve been browsing SO for a while now but either I\'m doing something wrong or I haven\'t found what I need.

<
10条回答
  •  盖世英雄少女心
    2020-12-13 18:36

    to make sure elem is in view inside a container:

    let rectElem = elem.getBoundingClientRect(), rectContainer=container.getBoundingClientRect();
    if (rectElem.bottom > rectContainer.bottom) elem.scrollIntoView(false);
    if (rectElem.top < rectContainer.top) elem.scrollIntoView();
    

提交回复
热议问题