scrollIntoView Scrolls just too far

前端 未结 21 2038
盖世英雄少女心
盖世英雄少女心 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条回答
  •  春和景丽
    2020-12-07 10:57

    Based on the answer of Arseniy-II: I had the Use-Case where the scrolling entity was not window itself but a inner Template (in this case a div). In this scenario we need to set an ID for the scrolling container and get it via getElementById to use its scrolling function:

    ...
    const yOffsetForScroll = -100
    const y = document.getElementById(this.idToScroll).getBoundingClientRect().top;
    const main = document.getElementById('app-content');
    main.scrollTo({
        top: y + main.scrollTop + yOffsetForScroll,
        behavior: 'smooth'
      });
    

    Leaving it here in case someone faces a similar situation!

提交回复
热议问题