I'm hoping to find a way to get the current viewable window's position (relative to the total page width/height) so I can use it to force a scroll from one section to another. However, there seems to be a tremendous amount of options when it comes to guessing which object holds the true X/Y for your browser.
Which of these do I need to make sure IE 6+, FF 2+, and Chrome/Safari work?
window.innerWidth
window.innerHeight
window.pageXOffset
window.pageYOffset
document.documentElement.clientWidth
document.documentElement.clientHeight
document.documentElement.scrollLeft
document.documentElement.scrollTop
document.body.clientWidth
document.body.clientHeight
document.body.scrollLeft
document.body.scrollTop
And are there any others? Once I know where the window is I can set an event chain that will slowly call window.scrollBy(x,y);
until it reaches my desired point.
This question is related to
javascript
Using pure javascript you can use Window.scrollX and Window.scrollY
window.addEventListener("scroll", function(event) {
var top = this.scrollY,
left =this.scrollX;
}, false);
Notes
The pageXOffset property is an alias for the scrollX property, and The pageYOffset property is an alias for the scrollY property:
window.pageXOffset == window.scrollX; // always true
window.pageYOffset == window.scrollY; // always true
Here is a quick demo
window.addEventListener("scroll", function(event) {_x000D_
_x000D_
var top = this.scrollY,_x000D_
left = this.scrollX;_x000D_
_x000D_
var horizontalScroll = document.querySelector(".horizontalScroll"),_x000D_
verticalScroll = document.querySelector(".verticalScroll");_x000D_
_x000D_
horizontalScroll.innerHTML = "Scroll X: " + left + "px";_x000D_
verticalScroll.innerHTML = "Scroll Y: " + top + "px";_x000D_
_x000D_
}, false);
_x000D_
*{box-sizing: border-box}_x000D_
:root{height: 200vh;width: 200vw}_x000D_
.wrapper{_x000D_
position: fixed;_x000D_
top:20px;_x000D_
left:0px;_x000D_
width:320px;_x000D_
background: black;_x000D_
color: green;_x000D_
height: 64px;_x000D_
}_x000D_
.wrapper div{_x000D_
display: inline;_x000D_
width: 50%;_x000D_
float: left;_x000D_
text-align: center;_x000D_
line-height: 64px_x000D_
}_x000D_
.horizontalScroll{color: orange}
_x000D_
<div class=wrapper>_x000D_
<div class=horizontalScroll>Scroll (x,y) to </div>_x000D_
<div class=verticalScroll>see me in action</div>_x000D_
</div>
_x000D_
Maybe more simple;
var top = window.pageYOffset || document.documentElement.scrollTop,
left = window.pageXOffset || document.documentElement.scrollLeft;
Credits: so.dom.js#L492
function FastScrollUp()
{
window.scroll(0,0)
};
function FastScrollDown()
{
$i = document.documentElement.scrollHeight ;
window.scroll(0,$i)
};
var step = 20;
var h,t;
var y = 0;
function SmoothScrollUp()
{
h = document.documentElement.scrollHeight;
y += step;
window.scrollBy(0, -step)
if(y >= h )
{clearTimeout(t); y = 0; return;}
t = setTimeout(function(){SmoothScrollUp()},20);
};
function SmoothScrollDown()
{
h = document.documentElement.scrollHeight;
y += step;
window.scrollBy(0, step)
if(y >= h )
{clearTimeout(t); y = 0; return;}
t = setTimeout(function(){SmoothScrollDown()},20);
}
Source: Stackoverflow.com