I’m trying to do a scrollable container using external buttons, without using a plugin. I have a container called #feed with a fixed width and height, and overflow hidden. Inside that I have #feed-inner, which is 560px tall. There are up and down arrows which move #feed-inner up and down, but I need to restrict the animation so #feed-inner can’t be moved passed 0 or the max height (560px).
$('#arrow-next').click(function() {
$('#feed-inner').animate({
top: '-=70'
}, 500, function() {
});
});
$('#arrow-prev').click(function() {
$('#feed-inner').animate({
top: '+=70'
}, 500, function() {
});
});
You can get the value of the current position of
#feed-innerrelative to#feedby using $.position. With that you can check to see if the position is at 0 or the max height of 560px. The code would look something like this:Note that
#arrow-nextwould be bringing#feed-innerupward (making it appear like the content is scrolling downwards) which seems contrary to the label “next” and vice versa for#arrow-prev.One additional note is that when the position of
#feed-innerreaches560px, there will be a blank canvas since the element has moved completely to the bottom and what you will see is an empty#feedcontainer. To avoid this, you would want to calculate the height of the#feedcontainer and ensure that#feed-innerdoes not move passed 560 –$(#feed).outerHeight();