forked from thomd/jquery-scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.scroll.min.js
More file actions
8 lines (8 loc) · 9.99 KB
/
jquery.scroll.min.js
File metadata and controls
8 lines (8 loc) · 9.99 KB
1
2
3
4
5
6
7
8
/*!
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(a,b){var c={init:function(b,c){var d=a.extend({},a.fn.scrollbar.defaults,c);return this.each(function(){var c=a(this),e={arrows:d.arrows};d.containerHeight!="auto"&&c.height(d.containerHeight),e.containerHeight=c.height(),e.contentHeight=a.fn.scrollbar.contentHeight(c);if(e.contentHeight<=e.containerHeight)return!0;this.scrollbar=new a.fn.scrollbar.Scrollbar(c,e,d),this.scrollbar.buildHtml().setHandle().appendEvents(),typeof b=="function"&&b(c.find(".scrollbar-pane"),this.scrollbar)})},repaint:function(){return this.each(function(){this.scrollbar&&this.scrollbar.repaint()})},scrollto:function(a,b,c){var b=b||!1,c=c||!1;return this.each(function(){this.scrollbar.scrollto(a,b,c)})},unscrollbar:function(){return this.each(function(){this.scrollbar&&(this.scrollbar.unscrollbar(),this.scrollbar=null)})}};a.fn.scrollbar=function(b){if(c[b])return c[b].apply(this,Array.prototype.slice.call(arguments,1));if(typeof b=="function"||b===undefined)return c.init.apply(this,arguments);if(typeof b=="object")return c.init.apply(this,[null,b]);a.error("method '"+b+"' does not exist for $.fn.scrollbar")},a.fn.scrollbar.defaults={containerHeight:"auto",arrows:!0,handleHeight:"auto",handleMinHeight:30,scrollTimeout:50,scrollStep:20,scrollTimeoutArrows:40,scrollStepArrows:3},a.fn.scrollbar.Scrollbar=function(a,b,c){this.container=a,this.props=b,this.opts=c,this.mouse={},this.props.arrows=this.container.hasClass("no-arrows")?!1:this.props.arrows},a.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 a=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:a}),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(){var c=this;return this.handle.bind("mousedown.handle",a.proxy(this,"startOfHandleMove")),this.handleContainer.bind("mousedown.handle",a.proxy(this,"onHandleContainerMousedown")),this.handleContainer.bind("mouseenter.container mouseleave.container",a.proxy(this,"onHandleContainerHover")),this.handleArrows.bind("mousedown.arrows",a.proxy(this,"onArrowsMousedown")),this.container.bind("mousewheel.container",a.proxy(this,"onMouseWheel")),this.container.bind("mouseenter.container mouseleave.container",a.proxy(this,"onContentHover")),a(b).bind("keydown",function(b){if(c.container.hasClass("focused")||c.container.hasClass("hover")){var d=!1;switch(b.keyCode){case 38:b.preventDefault(),c.handle.direction=-1,c.handle.step=c.opts.scrollStepArrows,d=setTimeout(a.proxy(c.moveHandle,c),c.opts.scrollTimeoutArrows);break;case 40:b.preventDefault(),c.handle.direction=1,c.handle.step=c.opts.scrollStepArrows,d=setTimeout(a.proxy(c.moveHandle,c),c.opts.scrollTimeoutArrows);break;case 33:b.preventDefault(),c.handle.direction=-1,c.handle.step=c.opts.scrollStepArrows*15,d=setTimeout(a.proxy(c.moveHandle,c),c.opts.scrollTimeoutArrows);break;case 34:b.preventDefault(),c.handle.direction=1,c.handle.step=c.opts.scrollStepArrows*15,d=setTimeout(a.proxy(c.moveHandle,c),c.opts.scrollTimeoutArrows)}}}),this.handle.bind("click.scrollbar",this.preventClickBubbling),this.handleContainer.bind("click.scrollbar",this.preventClickBubbling),this.handleArrows.bind("click.scrollbar",this.preventClickBubbling),this},mousePosition:function(a){return a.pageY||a.clientY+(b.documentElement.scrollTop||b.body.scrollTop)||0},repaint:function(){this.pane.height()<=this.props.containerHeight?this.unscrollbar():(this.setHandle(),this.setHandlePosition())},scrollto:function(b,c,d){var e=0;typeof b=="number"?e=(b<0?0:b)/this.props.handleContentRatio:typeof b=="string"?(b=="bottom"&&(e=this.props.handlePosition.max),b=="middle"&&(e=Math.ceil(this.props.handlePosition.max/2))):typeof b=="object"&&!a.isPlainObject(b)&&(e=Math.ceil(b.position().top/this.props.handleContentRatio)),this.handle.top=e,this.setHandlePosition(c,d),this.setContentPosition(c,d)},unscrollbar:function(){var a=this.container.find(".scrollbar-pane").html();a!==null&&(this.container.empty(),this.container.append(a),this.container.attr("style",""))},startOfHandleMove:function(c){c.preventDefault(),c.stopPropagation(),this.mouse.start=this.mousePosition(c),this.handle.start=this.handle.top,a(b).bind("mousemove.handle",a.proxy(this,"onHandleMove")).bind("mouseup.handle",a.proxy(this,"endOfHandleMove")),this.handle.addClass("move"),this.handleContainer.addClass("move")},onHandleMove:function(a){a.preventDefault();var b=this.mousePosition(a)-this.mouse.start;this.handle.top=this.handle.start+b,this.setHandlePosition(),this.setContentPosition()},endOfHandleMove:function(c){a(b).unbind(".handle"),this.handle.removeClass("move"),this.handleContainer.removeClass("move")},setHandlePosition:function(b,c){var b=b||!1,c=c?a.fn.easing?c:"swing":!1;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,b&&!isNaN(b)?this.handle.stop().animate({top:this.handle.top+"px"},b,c):this.handle[0].style.top=this.handle.top+"px"},setContentPosition:function(b,c){var b=b||!1,c=c?a.fn.easing?c:"swing":!1;this.pane.top=-1*this.props.handleContentRatio*this.handle.top,b&&!isNaN(b)?this.pane.stop().animate({top:this.pane.top+"px"},b,c):this.pane[0].style.top=this.pane.top+"px",this.container.trigger("scrolled")},onMouseWheel:function(a,b){if(!this.container.hasClass("focused")&&!this.container.hasClass("hover"))return;b*=2.5,this.handle.top-=b,this.setHandlePosition(),this.setContentPosition(),this.handle.top>this.props.handlePosition.min&&this.handle.top<this.props.handlePosition.max&&a.preventDefault()},onHandleContainerMousedown:function(c){c.preventDefault();if(!a(c.target).hasClass("scrollbar-handle-container"))return!1;this.handle.direction=this.handle.offset().top<this.mousePosition(c)?1:-1,this.handle.step=this.opts.scrollStep;var d=this;a(b).bind("mouseup.handlecontainer",function(){clearInterval(e),d.handle.unbind("mouseenter.handlecontainer"),a(b).unbind("mouseup.handlecontainer")}),this.handle.bind("mouseenter.handlecontainer",function(){clearInterval(e)});var e=setInterval(a.proxy(this.moveHandle,this),this.opts.scrollTimeout)},onArrowsMousedown:function(c){c.preventDefault(),this.handle.direction=a(c.target).hasClass("scrollbar-handle-up")?-1:1,this.handle.step=this.opts.scrollStepArrows,a(c.target).addClass("move");var d=setInterval(a.proxy(this.moveHandle,this),this.opts.scrollTimeoutArrows);a(b).one("mouseup.arrows",function(){clearInterval(d),a(c.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(),this.container.trigger("scrolled")},onContentHover:function(b){b.type==="mouseenter"?(this.container.addClass("hover"),this.handleContainer.addClass("hover"),this.container.closest("div.scrollbar-pane").each(function(){a(this).parent().removeClass("hover focused")})):(this.container.removeClass("hover"),this.handleContainer.removeClass("hover"),this.container.closest("div.scrollbar-pane").each(function(){a(this).parent().addClass("hover focused")}))},onHandleContainerHover:function(a){a.type==="mouseenter"?this.handleArrows.addClass("hover"):this.handleArrows.removeClass("hover")},preventClickBubbling:function(a){a.stopPropagation()}},a.fn.scrollbar.contentHeight=function(a){var b=a.wrapInner('<div style="zoom:1"/>').find(":first"),c=b.css({overflow:"hidden"}).height();return b.replaceWith(b.contents()),c},a.fn.defaultCss=function(b){var c={right:"auto",left:"auto",top:"auto",bottom:"auto",position:"static"};return this.each(function(){var d=a(this);for(var e in b)d.css(e)===c[e]&&d.css(e,b[e])})}})(jQuery,document);