我在插件中添加了一個新函數,並使用它來更新keydown事件上的滾動。
代碼添加到插件:
// define the added function
jQuery.fn.tinyscrollbar_updatescroll = function(sScroll)
{
return jQuery(this).data('tsb').updatescroll(sScroll);
};
// end of added function definition
function Scrollbar(root, options)
{
var oSelf = this
, oWrapper = root
, oViewport = { obj: jQuery('.viewport', root) }
, oContent = { obj: jQuery('.overview', root) }
, oScrollbar = { obj: jQuery('.scrollbar', root) }
, oTrack = { obj: jQuery('.track', oScrollbar.obj) }
, oThumb = { obj: jQuery('.thumb', oScrollbar.obj) }
, sAxis = options.axis === 'x'
, sDirection = sAxis ? 'left' : 'top'
, sSize = sAxis ? 'Width' : 'Height'
, iScroll = 0
, iPosition = { start: 0, now: 0 }
, iMouse = {}
, touchEvents = 'ontouchstart' in document.documentElement
;
function initialize()
{
oSelf.update();
setEvents();
return oSelf;
}
// the new added function using the wheel function
this.updatescroll = function(sScroll)
{
if(oContent.ratio < 1)
{
iScroll -= sScroll;
iScroll = Math.min((oContent[ options.axis ] - oViewport[ options.axis ]), Math.max(0, iScroll));
oThumb.obj.css(sDirection, iScroll/oScrollbar.ratio);
oContent.obj.css(sDirection, -iScroll);
}
};
// end of added function
插件以外的代碼:
jQuery('body').keydown(function (event) {
if (event.keyCode == 38) {
// up arrow
$('#scrollbar1').tinyscrollbar_updatescroll(40);
} else if (event.keyCode == 40) {
// down arrow
$('#scrollbar1').tinyscrollbar_updatescroll(-40);
}
});
的tinyscrollbar_updatescroll滾動內容的當前位置加上發送給它的量。原始的tinyscrollbar_update函數將內容滾動到以像素爲單位的特定位置。