Prashant Singh
Prashant Singh

Reputation: 3793

Finding the position of scroll bar using javascript

I need to hint my user that there are some useful results on next page as soon as he crosses half of the width of my webpage(home). For this, I wish to know the exact position of the scroll bar , so , that as soon as the position of the scroll bar goes after 50% , I can trigger some javascript function to do the same.

I have encircled the scroll bar in the picture whose position, I have to find out

enter image description here

Also, if we can't find out the position of scroll bar, please tell me some alternate method to do the same !

Thanks !!

Upvotes: 0

Views: 416

Answers (1)

Karl Laurentius Roos
Karl Laurentius Roos

Reputation: 4399

You can't find the position of the scrollbar since it's not a part of the DOM but what you can do is determine the position. Since I don't know how to do it with vanilla JavaScript I'll give you a jQuery example:

var scrollTop = $("body").scrollTop(); // pixels scrolled from top
var documentHeight = $(document).height(); // the height of the document

if(scrollTop >= documentHeight / 2){
    // 50%+ scrolled
}

Upvotes: 1

Related Questions