How to get the current scroll position in Javascript?

Dung Do Tien Apr 21 2020 162

I want to check the position of a div compare with the current window scroll position but I don't know how to get the current scroll position in Javascript?

You can suggest to me in Js or Jquery are fine.

Thank you for any suggestions.

Have 1 answer(s) found.
  • You can use the below code :

    document.documentElement.scrollTop 

    But documentElement not working in chrome. Chrome uses the body for keeping track of scrolling.

    So to get the current position, you can :

    var currenPosition = document.documentElement.scrollTop || document.body.scrollTop; 

    If you use Jquery you can do as below:

    var currenPosition = $(window).scrollTop();

    You can see a demo code here.

    Sandeep Kumar Apr 21 2020
Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips