-
Notifications
You must be signed in to change notification settings - Fork 45
/
jquery.fs.scroller.min.js
9 lines (8 loc) · 6.03 KB
/
jquery.fs.scroller.min.js
1
2
3
4
5
6
7
8
9
/*
* Scroller v3.1.2 - 2015-04-04
* A jQuery plugin for replacing default browser scrollbars. Part of the Formstone Library.
* http://classic.formstone.it/scroller/
*
* Copyright 2015 Ben Plum; MIT Licensed
*/
!function(a,b){"use strict";function c(b){b=a.extend({},q,b||{}),null===n&&(n=a("body"));for(var c=a(this),e=0,f=c.length;f>e;e++)d(c.eq(e),b);return c}function d(c,d){if(!c.hasClass(o.base)){d=a.extend({},d,c.data(m+"-options"));var h="";h+='<div class="'+o.bar+'">',h+='<div class="'+o.track+'">',h+='<div class="'+o.handle+'">',h+="</div></div></div>",d.paddingRight=parseInt(c.css("padding-right"),10),d.paddingBottom=parseInt(c.css("padding-bottom"),10),c.addClass([o.base,d.customClass].join(" ")).wrapInner('<div class="'+o.content+'" />').prepend(h),d.horizontal&&c.addClass(o.isHorizontal);var i=a.extend({$scroller:c,$content:c.find(l(o.content)),$bar:c.find(l(o.bar)),$track:c.find(l(o.track)),$handle:c.find(l(o.handle))},d);i.trackMargin=parseInt(i.trackMargin,10),i.$content.on("scroll."+m,i,e),i.$scroller.on(p.start,l(o.track),i,f).on(p.start,l(o.handle),i,g).data(m,i),r.reset.apply(c),a(b).one("load",function(){r.reset.apply(c)})}}function e(a){a.preventDefault(),a.stopPropagation();var b=a.data,c={};if(b.horizontal){var d=b.$content.scrollLeft();0>d&&(d=0);var e=d/b.scrollRatio;e>b.handleBounds.right&&(e=b.handleBounds.right),c={left:e}}else{var f=b.$content.scrollTop();0>f&&(f=0);var g=f/b.scrollRatio;g>b.handleBounds.bottom&&(g=b.handleBounds.bottom),c={top:g}}b.$handle.css(c)}function f(a){a.preventDefault(),a.stopPropagation();var b=a.data,c=a.originalEvent,d=b.$track.offset(),e="undefined"!=typeof c.targetTouches?c.targetTouches[0]:null,f=e?e.pageX:a.clientX,g=e?e.pageY:a.clientY;b.horizontal?(b.mouseStart=f,b.handleLeft=f-d.left-b.handleWidth/2,k(b,b.handleLeft)):(b.mouseStart=g,b.handleTop=g-d.top-b.handleHeight/2,k(b,b.handleTop)),h(b)}function g(a){a.preventDefault(),a.stopPropagation();var b=a.data,c=a.originalEvent,d="undefined"!=typeof c.targetTouches?c.targetTouches[0]:null,e=d?d.pageX:a.clientX,f=d?d.pageY:a.clientY;b.horizontal?(b.mouseStart=e,b.handleLeft=parseInt(b.$handle.css("left"),10)):(b.mouseStart=f,b.handleTop=parseInt(b.$handle.css("top"),10)),h(b)}function h(a){a.$content.off(l(m)),n.on(p.move,a,i).on(p.end,a,j)}function i(a){a.preventDefault(),a.stopPropagation();var b=a.data,c=a.originalEvent,d=0,e=0,f="undefined"!=typeof c.targetTouches?c.targetTouches[0]:null,g=f?f.pageX:a.clientX,h=f?f.pageY:a.clientY;b.horizontal?(e=b.mouseStart-g,d=b.handleLeft-e):(e=b.mouseStart-h,d=b.handleTop-e),k(b,d)}function j(a){a.preventDefault(),a.stopPropagation();var b=a.data;b.$content.on("scroll.scroller",b,e),n.off(".scroller")}function k(a,b){var c={};if(a.horizontal){b<a.handleBounds.left&&(b=a.handleBounds.left),b>a.handleBounds.right&&(b=a.handleBounds.right);var d=Math.round(b*a.scrollRatio);c={left:b},a.$content.scrollLeft(d)}else{b<a.handleBounds.top&&(b=a.handleBounds.top),b>a.handleBounds.bottom&&(b=a.handleBounds.bottom);var e=Math.round(b*a.scrollRatio);c={top:b},a.$content.scrollTop(e)}a.$handle.css(c)}function l(a){return"."+a}var m="scroller",n=null,o={base:"scroller",content:"scroller-content",bar:"scroller-bar",track:"scroller-track",handle:"scroller-handle",isHorizontal:"scroller-horizontal",isSetup:"scroller-setup",isActive:"scroller-active"},p={start:"touchstart."+m+" mousedown."+m,move:"touchmove."+m+" mousemove."+m,end:"touchend."+m+" mouseup."+m},q={customClass:"",duration:0,handleSize:0,horizontal:!1,trackMargin:0},r={defaults:function(b){return q=a.extend(q,b||{}),"object"==typeof this?a(this):!0},destroy:function(){return a(this).each(function(b,c){var d=a(c).data(m);d&&(d.$scroller.removeClass([d.customClass,o.base,o.isActive].join(" ")),d.$bar.remove(),d.$content.contents().unwrap(),d.$content.off(l(m)),d.$scroller.off(l(m)).removeData(m))})},scroll:function(b,c){return a(this).each(function(){var d=a(this).data(m),e=c||q.duration;if("number"!=typeof b){var f=a(b);if(f.length>0){var g=f.position();b=d.horizontal?g.left+d.$content.scrollLeft():g.top+d.$content.scrollTop()}else b=d.$content.scrollTop()}var h=d.horizontal?{scrollLeft:b}:{scrollTop:b};d.$content.stop().animate(h,e)})},reset:function(){return a(this).each(function(){var b=a(this).data(m);if(b){b.$scroller.addClass(o.isSetup);var c={},d={},e={},f=0,g=!0;if(b.horizontal){b.barHeight=b.$content[0].offsetHeight-b.$content[0].clientHeight,b.frameWidth=b.$content.outerWidth(),b.trackWidth=b.frameWidth-2*b.trackMargin,b.scrollWidth=b.$content[0].scrollWidth,b.ratio=b.trackWidth/b.scrollWidth,b.trackRatio=b.trackWidth/b.scrollWidth,b.handleWidth=b.handleSize>0?b.handleSize:b.trackWidth*b.trackRatio,b.scrollRatio=(b.scrollWidth-b.frameWidth)/(b.trackWidth-b.handleWidth),b.handleBounds={left:0,right:b.trackWidth-b.handleWidth},b.$content.css({paddingBottom:b.barHeight+b.paddingBottom});var h=b.$content.scrollLeft();f=h*b.ratio,g=b.scrollWidth<=b.frameWidth,c={width:b.frameWidth},d={width:b.trackWidth,marginLeft:b.trackMargin,marginRight:b.trackMargin},e={width:b.handleWidth}}else{b.barWidth=b.$content[0].offsetWidth-b.$content[0].clientWidth,b.frameHeight=b.$content.outerHeight(),b.trackHeight=b.frameHeight-2*b.trackMargin,b.scrollHeight=b.$content[0].scrollHeight,b.ratio=b.trackHeight/b.scrollHeight,b.trackRatio=b.trackHeight/b.scrollHeight,b.handleHeight=b.handleSize>0?b.handleSize:b.trackHeight*b.trackRatio,b.scrollRatio=(b.scrollHeight-b.frameHeight)/(b.trackHeight-b.handleHeight),b.handleBounds={top:0,bottom:b.trackHeight-b.handleHeight};var i=b.$content.scrollTop();f=i*b.ratio,g=b.scrollHeight<=b.frameHeight,c={height:b.frameHeight},d={height:b.trackHeight,marginBottom:b.trackMargin,marginTop:b.trackMargin},e={height:b.handleHeight}}g?b.$scroller.removeClass(o.isActive):b.$scroller.addClass(o.isActive),b.$bar.css(c),b.$track.css(d),b.$handle.css(e),k(b,f),b.$scroller.removeClass(o.isSetup)}})}};a.fn[m]=function(a){return r[a]?r[a].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof a&&a?this:c.apply(this,arguments)},a[m]=function(a){"defaults"===a&&r.defaults.apply(this,Array.prototype.slice.call(arguments,1))}}(jQuery);