I have a div that is scrollable that I want to conditionally stop scrolli开发者_如何学编程ng.
The code is something like:
if(theDiv.scrollTop + theDiv.clientHeight + thisScrollAmount > theDiv.scrollHeight)
StopScrolling();
How can I get the amount the scroll scrolled?
(I know that event.wheelDelta
is typically +-120, but it seems the amount that is actually scrolled when one scrolls can be quite different than that.)
EDIT Apologies. It seems this question is unclear. I was looking for how much the div WOULD scroll if the event was not canceled by it's handler. I was assuming that it took on different values, but it appears to only take on values +- 120.
Perhaps it should be deleted.
//Works pretty good
<style type="text/css">
#divTest{width:150px;height:200px;overflow:auto}
</style>
<script>
var amountCanScroll = 400
function maxScroll( ){
if( document.getElementById('divTest').scrollTop > amountCanScroll){
document.getElementById('divTest').scrollTop = amountCanScroll;
}
}
</script>
<body><div id="divTest" onscroll="maxScroll()" >
1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>
</div>
</body>
Listen to the onscroll
event and check the scrollTop
property of that div against your maximum allowed scroll amount (this will be an arbitrary number you'll have to come up with) and if it is more than that, re-set the scrollTop
of that div to that maximum amount.
精彩评论