JavaScript get window X/Y position for scroll
The method jQuery (v1.10) uses to find this is:
var doc = document.documentElement;
var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
That is:
- It tests for
window.pageXOffset
first and uses that if it exists. - Otherwise, it uses
document.documentElement.scrollLeft
. - It then subtracts
document.documentElement.clientLeft
if it exists.
The subtraction of document.documentElement.clientLeft
/ Top
only appears to be required to correct for situations where you have applied a border (not padding or margin, but actual border) to the root element, and at that, possibly only in certain browsers.
How can I get the scrollbar position with JavaScript?
You can use element.scrollTop
and element.scrollLeft
to get the vertical and horizontal offset, respectively, that has been scrolled. element
can be document.body
if you care about the whole page. You can compare it to element.offsetHeight
and element.offsetWidth
(again, element
may be the body) if you need percentages.
Get scroll position with Reactjs
This should work:
this.setState({
post: post,
theposition: window.pageYOffset
});
How to get and set the current web page scroll position?
You're looking for the document.documentElement.scrollTop
property.
Related Topics
Communication Between Sibling Components in Vue.Js 2.0
Convert String in Dot Notation to Get the Object Reference
What's the Recommended Way to Extend Angularjs Controllers
How to Fix Jslint Error 'Don't Make Functions Within a Loop.'
Remove Not Alphanumeric Characters from String
Onclick Function Runs Automatically
Pretty Printing Xml with JavaScript
How to Query Referenced Objects in Mongodb
Keyboard Shortcuts with Jquery
How to Return a List of All the Image File Names from a Folder Using Only JavaScript
Get the Value of Checked Checkbox
JavaScript Get Window X/Y Position for Scroll
What Is Returned from a Constructor
Multiple Left-Hand Assignment with JavaScript
What Is Ajax and How Does It Work
New Es6 Syntax for Importing Commonjs/Amd Modules I.E. 'Import Foo = Require('Foo')'