Reputation: 22653
i have this code and i want to make the scroll on a div instead of the body
var scrollPosition = document.body.scrollTop || document.documentElement.scrollTop;
i tried this :
var divToScroll = $(".divToScroll"),
scrollPosition = divToScroll.scrollTop || document.divToScroll.scrollTop;
but it is not working
<section class=divToScroll>
<article>
</article>
</section>
the style:
.divToScroll{
position:relative;
width:640px;
height:320px;
overflow-y:auto;
overflow-x:hidden;
}
.divToScroll article{
width:100%;
height:2000px;
}
Upvotes: 2
Views: 8973
Reputation: 318302
The reason it's not working is because divToScroll
is a jQuery object, not a native DOM node like document.body
, and as such has no scrollTop property, but jQuery has a scrollTop() method
var divToScroll = $(".divToScroll"),
scrollPosition = divToScroll.scrollTop();
of course, this only works if the scrollbar is attached to that element.
Without jQuery it would be:
var divToScroll = document.getElementsByClassName('divToScroll')[0],
scrollPosition = divToScroll.scrollTop;
Upvotes: 2