In Javascript, I wanted to understand a few things:
- Can we both get/set scroll pos for a window?
- What is the diff between window scroll pos and for normal element on page?
- Can we set scroll pos for any HTML elements or is it only for those with overflow properties?
- Is there any difference in these property calc for iPad?
- For
scrollWidth, does it get affected by vertical scrollbars..If yes, is it the same in all browsers?
Your answers.
$(window).scrollTop(),$(window).scrollTop(100);windowscroll position is the main page’s scroll postion(left/top) where as for normal element the scroll position applies when we set overflow:auto to it and then we can get/set the scroll position of it.We can get the scroll position of any element using
$("selector").scrollTop()/scrollLeft()but yes if the overflow is not auto then I think it will always return 0.For iPad the
scrollTop()/scrollLeft()do not work, you have to usewindow.pageYOffset/window.pageXOffsetJavaScript equivalents, not tested though across browsers
$(window).scrollTop()–document.documentElement.scrollTop/scrollLeft$("selector").scrollLeft()–document.getElementById("elementId").offsetTop/offsetLeft