Scroll Box bottom Position check
August 24th, 2009
by Zeeshan
I wanted to see if I could check if a user has scrolled to the bottom of the box. For example, to see if they have read all the terms and conditions.
We can find out the scroll position using $(selector).scrollTop() and to find the highest posible scroll position using $(selector)[0].scrollHeight.
So we could use
if($(selector)[0].scrollHeight - $(selector).scrollTop() == $(selector).outerHeight()){
//do something...
}
Which would detect if the user is at the bottom.
So like I mentioned before we could impliment it on a “have you read the terms and conditions” context. I used this code:
JS
$(document).ready(function(){
$('#box').scroll(function(){
if($('#box')[0].scrollHeight - $('#box').scrollTop() == $('#box').outerHeight()) {
$('#button').fadeIn();
}
else{
$('#button').fadeOut();
}
});
});
HTML
TermsPellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus
So the button will fade in when the user scrolls to the bottom of #box.









