目前我的 div 正在使用两个按钮(向上和向下)滚动,但我希望它跳到顶部并跳到底部,而这两个按钮使用垂直自动滚动(向下)来代替。我如何在 jQuery 中实现这一点?到目前为止,这是我的代码: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;
}