Using the markup below how would I get the "#content" div to scroll up or down when I click or hover over the "#scrollUp" or "#scrollDown" anchor tag. Scrolling should be smooth preferably. If clicked it should scroll a specific amount (for touch devices) if mouseover it can scroll until mouseout.
<style>
#content {
overflow:auto;
height: 50px; /*could be whatever*/
}
</style>
<a id="scrollUp" href="#">up</a>
<a id="scrollDown" href="#">down</a>
<div id="wrapper">
<div id="content">
<ul>
<li>some content here</li>
<li>some content here</li>
<li>some content here</li>
<li>some content here</li>
<li>some content here</li>
<li>some content here</li>
</ul>
</div>
</div>
Try using JavaScript instead of jQuery for this task. Google the function
scrollBy()
as inwindow.scrollBy()
You can use jQuery's
animate
function to accomplish a smooth-scrolling effect onclick
ormouseover
:Working example: http://jsfiddle.net/andrewwhitaker/s5mgX/
(You'll have to disable the
mouseover
andmouseout
events to see the effects of theclick
event handler properly)How it works:
animate
function mentioned above to scroll smoothly by a specified amount on click.mouseover
event handler is called, and disable scrolling when the link'smouseout
event handler.scrollContent
is called, if thescrolling
flag is stilltrue
after the animation is completed, animate again in the same direction. The callback function parameter ofanimate
allows us to take an action after animation has completed.