Anime: Scroll document to element?

Created on 27 Jun 2017  路  3Comments  路  Source: juliangarnier/anime

Is it possible to have something like the following:

const offset = element.getBoundingClientRect().top;
anime({
    target: document,
    scrollTop: offset,
    duration: 1000
});

The idea being we can scroll to the element using some kind of easing function.

Most helpful comment

Here is an other solution including scrolling position.

const scrollTo = element => {
  const elementSelector = document.querySelector(element)

  document.querySelector('.btn-scroll').addEventListener('click', () => {
    const elementOffset = elementSelector.getBoundingClientRect().top
    const scrollPosition = window.scrollY
    const documentTop = document.documentElement.clientTop
    const scrollOffset = elementOffset + scrollPosition - documentTop

    anime({
      targets: [document.documentElement, document.body],
      scrollTop: scrollOffset,
      duration: 800,
      easing: 'easeInOutQuad'
    })
  })
}

All 3 comments

Yes, but the document itself do not have the scrollTop property. You may want to animate document.documentElement and document.body.

const offset = element.getBoundingClientRect().top;
anime({
    targets: [document.documentElement, document.body],
    scrollTop: offset,
    duration: 1000
});

Here is an other solution including scrolling position.

const scrollTo = element => {
  const elementSelector = document.querySelector(element)

  document.querySelector('.btn-scroll').addEventListener('click', () => {
    const elementOffset = elementSelector.getBoundingClientRect().top
    const scrollPosition = window.scrollY
    const documentTop = document.documentElement.clientTop
    const scrollOffset = elementOffset + scrollPosition - documentTop

    anime({
      targets: [document.documentElement, document.body],
      scrollTop: scrollOffset,
      duration: 800,
      easing: 'easeInOutQuad'
    })
  })
}

@jimblue This was helpful! Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DavudSafarli picture DavudSafarli  路  5Comments

PierBover picture PierBover  路  6Comments

bravebox picture bravebox  路  3Comments

ndimatteo picture ndimatteo  路  5Comments

Crashtor picture Crashtor  路  5Comments