Reputation: 13206
Currently my div is scrolling using two buttons (Up and Down) but I would like it to jump to top and jump to bottom with those two buttons with a vertical auto scroll (downwards) going on instead. How do I accomplish this in jQuery? Here is my code so far: http://jsfiddle.net/NkY8J/.
HTML
<div id="scroll">
Content here and more content here<br /><br />
Content here and more content here<br /><br />
Content here and more content here<br /><br />
Content here and more content here<br /><br />
Content here and more content here<br /><br />
Content here and more content here<br /><br />
Content here and more content here<br /><br />
Content here and more content here<br /><br />
Content here and more content here<br /><br />
Content here and more content here<br /><br />
Content here and more content here<br /><br />
Content here and more content here<br /><br />
Content here and more content here<br /><br />
Content here and more content here
</div>
<input id="scroll-up" class="btn" type="button" value="Up"/>
<input id="scroll-down" class="btn" type="button" value="Down"/>
JS
$(function() {
var ele = $('#scroll');
var speed = 25, scroll = 5, scrolling;
$('#scroll-up').mouseenter(function() {
// Scroll the element up
scrolling = window.setInterval(function() {
ele.scrollTop( ele.scrollTop() - scroll );
}, speed);
});
$('#scroll-down').mouseenter(function() {
// Scroll the element down
scrolling = window.setInterval(function() {
ele.scrollTop( ele.scrollTop() + scroll );
}, speed);
});
$('#scroll-up, #scroll-down').bind({
click: function(e) {
// Prevent the default click action
e.preventDefault();
},
mouseleave: function() {
if (scrolling) {
window.clearInterval(scrolling);
scrolling = false;
}
}
});
});
CSS
#scroll {
width: 200px;
height: 100px;
overflow: hidden;
padding: 4px;
}
Upvotes: 3
Views: 10046
Reputation: 2850
Let's see Here. http://jsfiddle.net/NkY8J/2/
Just set ele.scrollTop(0);
to jump to top.
And set ele.scrollTop(Math.pow(10,9));
to jump to bottom.
Upvotes: 1