/* * tiny scrollbar * http://www.baijs.nl/tinyscrollbar/ * * dual licensed under the mit or gpl version 2 licenses. * http://www.opensource.org/licenses/mit-license.php * http://www.opensource.org/licenses/gpl-2.0.php * * date: 13 / 08 / 2012 * @version 1.81 * @author maarten baijs * */ ;( function( $ ) { $.tiny = $.tiny || { }; $.tiny.scrollbar = { options: { axis : 'y' // vertical or horizontal scrollbar? ( x || y ). , wheel : 40 // how many pixels must the mouswheel scroll at a time. , scroll : false // enable or disable the mousewheel. , lockscroll : true // return scrollwheel to browser if there is no more content. , size : '500' // set the size of the scrollbar to auto or a fixed number. , sizethumb : '60' // set the size of the thumb to auto or a fixed number. , invertscroll : false // enable mobile invert style scrolling } }; $.fn.tinyscrollbar = function( params ) { // alert("111"); var options = $.extend( {}, $.tiny.scrollbar.options, params ); this.each( function() { $( this ).data('tsb', new scrollbar( $( this ), options ) ); }); return this; }; $.fn.tinyscrollbar_update = function(sscroll) { return $( this ).data( 'tsb' ).update( sscroll ); }; function scrollbar( root, options ) { var oself = this , owrapper = root , oviewport = { obj: $( '.viewport', root ) } , ocontent = { obj: $( '.overview', root ) } , oscrollbar = { obj: $( '.scrollbar', root ) } , otrack = { obj: $( '.track', oscrollbar.obj ) } , othumb = { obj: $( '.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; } this.update = function( sscroll ) { oviewport[ options.axis ] = oviewport.obj[0][ 'offset'+ ssize ]; ocontent[ options.axis ] = ocontent.obj[0][ 'scroll'+ ssize ]; ocontent.ratio = oviewport[ options.axis ] / ocontent[ options.axis ]; oscrollbar.obj.toggleclass( 'disable', ocontent.ratio >= 1 ); otrack[ options.axis ] = options.size === 'auto' ? oviewport[ options.axis ] : options.size; othumb[ options.axis ] = math.min( otrack[ options.axis ], math.max( 0, ( options.sizethumb === 'auto' ? ( otrack[ options.axis ] * ocontent.ratio ) : options.sizethumb ) ) ); oscrollbar.ratio = options.sizethumb === 'auto' ? ( ocontent[ options.axis ] / otrack[ options.axis ] ) : ( ocontent[ options.axis ] - oviewport[ options.axis ] ) / ( otrack[ options.axis ] - othumb[ options.axis ] ); iscroll = ( sscroll === 'relative' && ocontent.ratio <= 1 ) ? math.min( ( ocontent[ options.axis ] - oviewport[ options.axis ] ), math.max( 0, iscroll )) : 0; iscroll = ( sscroll === 'bottom' && ocontent.ratio <= 1 ) ? ( ocontent[ options.axis ] - oviewport[ options.axis ] ) : isnan( parseint( sscroll, 10 ) ) ? iscroll : parseint( sscroll, 10 ); setsize(); }; function setsize() { var scsssize = ssize.tolowercase(); othumb.obj.css( sdirection, iscroll / oscrollbar.ratio ); ocontent.obj.css( sdirection, -iscroll ); imouse.start = othumb.obj.offset()[ sdirection ]; oscrollbar.obj.css( scsssize, otrack[ options.axis ] ); otrack.obj.css( scsssize, otrack[ options.axis ] ); othumb.obj.css( scsssize, othumb[ options.axis ] ); } function setevents() { if( ! touchevents ) { othumb.obj.bind( 'mousedown', start ); otrack.obj.bind( 'mouseup', drag ); } else { oviewport.obj[0].ontouchstart = function( event ) { if( 1 === event.touches.length ) { start( event.touches[ 0 ] ); event.stoppropagation(); } }; } if( options.scroll && window.addeventlistener ) { owrapper[0].addeventlistener( 'dommousescroll', wheel, false ); owrapper[0].addeventlistener( 'mousewheel', wheel, false ); owrapper[0].addeventlistener( 'mozmousepixelscroll', function( event ){ event.preventdefault(); }, false); } else if( options.scroll ) { owrapper[0].onmousewheel = wheel; } } function start( event ) { $( "body" ).addclass( "noselect" ); var othumbdir = parseint( othumb.obj.css( sdirection ), 10 ); imouse.start = saxis ? event.pagex : event.pagey; iposition.start = othumbdir == 'auto' ? 0 : othumbdir; if( ! touchevents ) { $( document ).bind( 'mousemove', drag ); $( document ).bind( 'mouseup', end ); othumb.obj.bind( 'mouseup', end ); } else { document.ontouchmove = function( event ) { event.preventdefault(); drag( event.touches[ 0 ] ); }; document.ontouchend = end; } } function wheel( event ) { if( ocontent.ratio < 1 ) { var oevent = event || window.event , idelta = oevent.wheeldelta ? oevent.wheeldelta / 120 : -oevent.detail / 3 ; iscroll -= idelta * options.wheel; 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 ); if( options.lockscroll || ( iscroll !== ( ocontent[ options.axis ] - oviewport[ options.axis ] ) && iscroll !== 0 ) ) { oevent = $.event.fix( oevent ); oevent.preventdefault(); } } } function drag( event ) { if( ocontent.ratio < 1 ) { if( options.invertscroll && touchevents ) { iposition.now = math.min( ( otrack[ options.axis ] - othumb[ options.axis ] ), math.max( 0, ( iposition.start + ( imouse.start - ( saxis ? event.pagex : event.pagey ) )))); } else { iposition.now = math.min( ( otrack[ options.axis ] - othumb[ options.axis ] ), math.max( 0, ( iposition.start + ( ( saxis ? event.pagex : event.pagey ) - imouse.start)))); } iscroll = iposition.now * (oscrollbar.ratio-0.9); ocontent.obj.css( sdirection, -iscroll ); othumb.obj.css( sdirection, iposition.now ); } } function end() { $( "body" ).removeclass( "noselect" ); $( document ).unbind( 'mousemove', drag ); $( document ).unbind( 'mouseup', end ); othumb.obj.unbind( 'mouseup', end ); document.ontouchmove = document.ontouchend = null; } return initialize(); } }(jquery));