您可以使用jQuery
hover
功能如下,
http://jsfiddle.net/RZecw/
JS
var stopScrollingDown = true,stopScrollingUp = true;
var api = $('.scroll-pane').jScrollPane().data('jsp');
$('#scroll-up').hover(
function() {
stopScrollingUp = false;
scrollUp();
}, function() {
stopScrollingUp = true;
});
$('#scroll-down').hover(function() {
stopScrollingDown = false;
scrollDown();
}, function() {
stopScrollingDown = true;
});
function scrollUp() {
if (!stopScrollingUp) {
api.scrollByY(-10);
setTimeout(scrollUp, 80);/*the smaller this value (80) is the faster it will move*/
}
}
function scrollDown() {
if (!stopScrollingDown) {
api.scrollByY(10);
setTimeout(scrollDown, 80);
}
}
HTML
<div class="scroll-pane"> <b>An Iframe at the bottom</b>
<p>This demonstration shows basic use of the jScrollPane plugin to add scrollbars to a HTML which can be styled at will.This demonstration shows basic use of the jScrollPane
....
</div>
<div id="scroll-up" class="btn">[up]</div>
<br/>
<div id="scroll-down" class="btn">[down]</div>
CSS
.scroll-pane {
height:100px;
}
.btn{
display:inline;
}
编辑 - 相关评论
隐藏/禁用按钮,如果没有需要对内容进行滚动,你可以检查是否有拖动手柄存在。例如,您可以添加在下面的代码片断结束,
http://jsfiddle.net/RZecw/2/
if(!$('.jspDrag').length){
/*hide controls or disable them*/
/*$('.btn').css({visibility:"hidden"});*/
$('.btn').css({color:"lightgray"});
$('.btn').off('mouseenter mouseleave');
}
你真棒,非常完美。谢谢!对不起,如果没有内容溢出,有没有办法使用这种方法来隐藏按钮,或者我应该以不同的方式做到这一点? – Nick
@Nick谢谢,我很高兴它为你工作。您可以检查是否显示拖曳手柄,并采取合适的行动,例如'if(!$('。jspDrag')。length){/ *隐藏控件或禁用它们* /}' – melc
@Nick用一个例子更新了答案。 – melc