Skip to content

Instantly share code, notes, and snippets.

@timrijkse
Created October 8, 2019 09:24
Show Gist options
  • Save timrijkse/ca0f0f7c9ca4c7fdc5129004f85cf37c to your computer and use it in GitHub Desktop.
Save timrijkse/ca0f0f7c9ca4c7fdc5129004f85cf37c to your computer and use it in GitHub Desktop.
Get scroll percentage of element. When the top of the element hits the top of the screen percentage will be 0. When the bottom of the element hits the bottom of the screen percentage will be 100.
const { top, height } = this.$el.getBoundingClientRect()
const { pageYOffset, innerHeight } = window
const min = top + pageYOffset
const max = min + height - innerHeight
const percentage = ((pageYOffset - min) * 100) / (max - min)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment