ScrollIntoView() causing the whole page to move

后端 未结 12 2050
庸人自扰
庸人自扰 2020-11-30 21:40

I am using ScrollIntoView() to scroll the highlighted item in a list into view. When I scroll downwards ScrollIntoView(false) works perfectly. But when I scroll upwards, Scr

12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 22:23

    Just to add an answer as per my latest experience and working on VueJs. I found below piece of code ad best, which does not impact your application in anyways.

    const el = this.$el.getElementsByClassName('your_element_class')[0];
    if (el) {
       scrollIntoView(el,
                      {
                           block: 'nearest',
                           inline: 'start',
                           behavior: 'smooth',
                           boundary: document.getElementsByClassName('main_app_class')[0]
                        });
         }
    

    main_app_class is the root class

    your_element_class is the element/view where you can to scroll into

    And for browser which does not support ScrollIntoView() just use below library its awesome https://www.npmjs.com/package/scroll-into-view-if-needed

提交回复
热议问题