-
Notifications
You must be signed in to change notification settings - Fork 40
/
jquery.scroll.min.js
7 lines (7 loc) · 9.98 KB
/
jquery.scroll.min.js
1
2
3
4
5
6
7
/*!
jquery scroll - a custom stylable scrollbar
Version 0.4
https://github.com/thomd/jquery-scroll
Copyright (c) 2011 Thomas Duerr (me-at-thomd-dot-net)
Licensed under the MIT license (https://raw.github.com/thomd/jquery-scroll/master/MIT-LICENSE)
*/(function($,document){var methods={init:function(fn,opts){var options=$.extend({},$.fn.scrollbar.defaults,opts);return this.each(function(){var container=$(this),props={arrows:options.arrows};options.containerHeight!="auto"&&container.height(options.containerHeight),props.containerHeight=container.height(),props.contentHeight=$.fn.scrollbar.contentHeight(container);if(props.contentHeight<=props.containerHeight)return!0;this.scrollbar=new $.fn.scrollbar.Scrollbar(container,props,options),this.scrollbar.buildHtml().setHandle().appendEvents(),typeof fn=="function"&&fn(container.find(".scrollbar-pane"),this.scrollbar)})},repaint:function(){return this.each(function(){this.scrollbar.repaint()})},scrollto:function(to){return this.each(function(){this.scrollbar.scrollto(to)})}};$.fn.scrollbar=function(method){if(methods[method])return methods[method].apply(this,Array.prototype.slice.call(arguments,1));if(typeof method=="function"||method===undefined)return methods.init.apply(this,arguments);if(typeof method=="object")return methods.init.apply(this,[null,method]);$.error("method '"+method+"' does not exist for $.fn.scrollbar")},$.fn.scrollbar.defaults={containerHeight:"auto",arrows:!0,handleHeight:"auto",handleMinHeight:30,scrollTimeout:50,scrollStep:20,scrollTimeoutArrows:40,scrollStepArrows:3},$.fn.scrollbar.Scrollbar=function(container,props,options){this.container=container,this.props=props,this.opts=options,this.mouse={},this.props.arrows=this.container.hasClass("no-arrows")?!1:this.props.arrows},$.fn.scrollbar.Scrollbar.prototype={buildHtml:function(){this.container.wrapInner('<div class="scrollbar-pane"/>'),this.container.append('<div class="scrollbar-handle-container"><div class="scrollbar-handle"/></div>'),this.props.arrows&&this.container.append('<div class="scrollbar-handle-up"/>').append('<div class="scrollbar-handle-down"/>');var height=this.container.height();return this.pane=this.container.find(".scrollbar-pane"),this.handle=this.container.find(".scrollbar-handle"),this.handleContainer=this.container.find(".scrollbar-handle-container"),this.handleArrows=this.container.find(".scrollbar-handle-up, .scrollbar-handle-down"),this.handleArrowUp=this.container.find(".scrollbar-handle-up"),this.handleArrowDown=this.container.find(".scrollbar-handle-down"),this.pane.defaultCss({top:0,left:0}),this.handleContainer.defaultCss({right:0}),this.handle.defaultCss({top:0,right:0}),this.handleArrows.defaultCss({right:0}),this.handleArrowUp.defaultCss({top:0}),this.handleArrowDown.defaultCss({bottom:0}),this.container.css({position:this.container.css("position")==="absolute"?"absolute":"relative",overflow:"hidden",height:height}),this.pane.css({position:"absolute",overflow:"visible",height:"auto"}),this.handleContainer.css({position:"absolute",top:this.handleArrowUp.outerHeight(!0),height:this.props.containerHeight-this.handleArrowUp.outerHeight(!0)-this.handleArrowDown.outerHeight(!0)+"px"}),this.handle.css({position:"absolute",cursor:"pointer"}),this.handleArrows.css({position:"absolute",cursor:"pointer"}),this.pane.top=0,this},setHandle:function(){return this.props.handleContainerHeight=this.handleContainer.height(),this.props.contentHeight=this.pane.height(),this.props.handleHeight=this.opts.handleHeight=="auto"?Math.max(Math.ceil(this.props.containerHeight*this.props.handleContainerHeight/this.props.contentHeight),this.opts.handleMinHeight):this.opts.handleHeight,this.handle.height(this.props.handleHeight),this.handle.height(2*this.handle.height()-this.handle.outerHeight(!0)),this.props.handlePosition={min:0,max:this.props.handleContainerHeight-this.props.handleHeight},this.props.handleContentRatio=(this.props.contentHeight-this.props.containerHeight)/(this.props.handleContainerHeight-this.props.handleHeight),this.handle.top==undefined?this.handle.top=0:this.handle.top=-1*this.pane.top/this.props.handleContentRatio,this},appendEvents:function(){return this.handle.bind("mousedown.handle",$.proxy(this,"startOfHandleMove")),this.handleContainer.bind("mousedown.handle",$.proxy(this,"onHandleContainerMousedown")),this.handleContainer.bind("mouseenter.container mouseleave.container",$.proxy(this,"onHandleContainerHover")),this.handleArrows.bind("mousedown.arrows",$.proxy(this,"onArrowsMousedown")),this.container.bind("mousewheel.container",$.proxy(this,"onMouseWheel")),this.container.bind("mouseenter.container mouseleave.container",$.proxy(this,"onContentHover")),this.handle.bind("click.scrollbar",this.preventClickBubbling),this.handleContainer.bind("click.scrollbar",this.preventClickBubbling),this.handleArrows.bind("click.scrollbar",this.preventClickBubbling),this},mousePosition:function(ev){return ev.pageY||ev.clientY+(document.documentElement.scrollTop||document.body.scrollTop)||0},repaint:function(){this.setHandle(),this.setHandlePosition()},scrollto:function(to){var distance=0;typeof to=="number"?distance=(to<0?0:to)/this.props.handleContentRatio:typeof to=="string"?(to=="bottom"&&(distance=this.props.handlePosition.max),to=="middle"&&(distance=Math.ceil(this.props.handlePosition.max/2))):typeof to=="object"&&!$.isPlainObject(to)&&(distance=Math.ceil(to.position().top/this.props.handleContentRatio)),this.handle.top=distance,this.setHandlePosition(),this.setContentPosition()},startOfHandleMove:function(ev){ev.preventDefault(),ev.stopPropagation(),this.mouse.start=this.mousePosition(ev),this.handle.start=this.handle.top,$(document).bind("mousemove.handle",$.proxy(this,"onHandleMove")).bind("mouseup.handle",$.proxy(this,"endOfHandleMove")),this.handle.addClass("move"),this.handleContainer.addClass("move")},onHandleMove:function(ev){ev.preventDefault();var distance=this.mousePosition(ev)-this.mouse.start;this.handle.top=this.handle.start+distance,this.setHandlePosition(),this.setContentPosition()},endOfHandleMove:function(ev){$(document).unbind(".handle"),this.handle.removeClass("move"),this.handleContainer.removeClass("move")},setHandlePosition:function(){this.handle.top=this.handle.top>this.props.handlePosition.max?this.props.handlePosition.max:this.handle.top,this.handle.top=this.handle.top<this.props.handlePosition.min?this.props.handlePosition.min:this.handle.top,this.handle[0].style.top=this.handle.top+"px"},setContentPosition:function(){this.pane.top=-1*this.props.handleContentRatio*this.handle.top,this.pane[0].style.top=this.pane.top+"px"},onMouseWheel:function(ev,delta){this.handle.top-=delta,this.setHandlePosition(),this.setContentPosition(),this.handle.top>this.props.handlePosition.min&&this.handle.top<this.props.handlePosition.max&&ev.preventDefault()},onHandleContainerMousedown:function(ev){ev.preventDefault();if(!$(ev.target).hasClass("scrollbar-handle-container"))return!1;this.handle.direction=this.handle.offset().top<this.mousePosition(ev)?1:-1,this.handle.step=this.opts.scrollStep;var that=this;$(document).bind("mouseup.handlecontainer",function(){clearInterval(timer),that.handle.unbind("mouseenter.handlecontainer"),$(document).unbind("mouseup.handlecontainer")}),this.handle.bind("mouseenter.handlecontainer",function(){clearInterval(timer)});var timer=setInterval($.proxy(this.moveHandle,this),this.opts.scrollTimeout)},onArrowsMousedown:function(ev){ev.preventDefault(),this.handle.direction=$(ev.target).hasClass("scrollbar-handle-up")?-1:1,this.handle.step=this.opts.scrollStepArrows,$(ev.target).addClass("move");var timer=setInterval($.proxy(this.moveHandle,this),this.opts.scrollTimeoutArrows);$(document).one("mouseup.arrows",function(){clearInterval(timer),$(ev.target).removeClass("move")})},moveHandle:function(){this.handle.top=this.handle.direction===1?Math.min(this.handle.top+this.handle.step,this.props.handlePosition.max):Math.max(this.handle.top-this.handle.step,this.props.handlePosition.min),this.handle[0].style.top=this.handle.top+"px",this.setContentPosition()},onContentHover:function(ev){ev.type==="mouseenter"?(this.container.addClass("hover"),this.handleContainer.addClass("hover")):(this.container.removeClass("hover"),this.handleContainer.removeClass("hover"))},onHandleContainerHover:function(ev){ev.type==="mouseenter"?this.handleArrows.addClass("hover"):this.handleArrows.removeClass("hover")},preventClickBubbling:function(ev){ev.stopPropagation()}},$.fn.scrollbar.contentHeight=function(container){var wrapper=container.wrapInner("<div/>").find(":first"),height=wrapper.css({overflow:"hidden"}).height();return wrapper.replaceWith(wrapper.contents()),height},$.fn.defaultCss=function(styles){var notdef={right:"auto",left:"auto",top:"auto",bottom:"auto",position:"static"};return this.each(function(){var elem=$(this);for(var style in styles)elem.css(style)===notdef[style]&&elem.css(style,styles[style])})},$.event.special.mousewheel={setup:function(){this.addEventListener?(this.addEventListener("mousewheel",$.fn.scrollbar.mouseWheelHandler,!1),this.addEventListener("DOMMouseScroll",$.fn.scrollbar.mouseWheelHandler,!1)):this.onmousewheel=$.fn.scrollbar.mouseWheelHandler},teardown:function(){this.removeEventListener?(this.removeEventListener("mousewheel",$.fn.scrollbar.mouseWheelHandler,!1),this.removeEventListener("DOMMouseScroll",$.fn.scrollbar.mouseWheelHandler,!1)):this.onmousewheel=null}},$.fn.extend({mousewheel:function(fn){return fn?this.bind("mousewheel",fn):this.trigger("mousewheel")},unmousewheel:function(fn){return this.unbind("mousewheel",fn)}}),$.fn.scrollbar.mouseWheelHandler=function(event){var orgEvent=event||window.event,args=[].slice.call(arguments,1),delta=0,returnValue=!0,deltaX=0,deltaY=0;return event=$.event.fix(orgEvent),event.type="mousewheel",event.wheelDelta&&(delta=event.wheelDelta/120),event.detail&&(delta=-event.detail/3),orgEvent.axis!==undefined&&orgEvent.axis===orgEvent.HORIZONTAL_AXIS&&(deltaY=0,deltaX=-1*delta),orgEvent.wheelDeltaY!==undefined&&(deltaY=orgEvent.wheelDeltaY/120),orgEvent.wheelDeltaX!==undefined&&(deltaX=-1*orgEvent.wheelDeltaX/120),args.unshift(event,delta,deltaX,deltaY),$.event.handle.apply(this,args)}})(jQuery,document)