-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrollfoo.min.js
More file actions
8 lines (8 loc) · 3.41 KB
/
scrollfoo.min.js
File metadata and controls
8 lines (8 loc) · 3.41 KB
1
2
3
4
5
6
7
8
/**
* ScrollFoo, a jQuery + BEM-friendly scrollbar plugin v0.1
*
* @link https://github.com/vslio/scrollFoo
* @author Kostas Vasileiou <hello@vsl.io>
* @license http://opensource.org/licenses/MIT MIT
*/
var ScrollFoo=function(a){this.options=a||{},this.defaults={parentEl:"",scrollerEl:".scrollfoo__scroller",visibleParentHeight:0,realParentHeight:0,ratio:0,state:!1,visibleParentHeightOffset:0,draggingClass:"scrollfoo--dragging",disabledClass:"scrollfoo__scroller--disabled"},this.$body=$("body"),this.config=$.extend(!0,{},this.defaults,this.options),this.initialize()};ScrollFoo.prototype.initialize=function(){this.registerEvents(),this.doCalculate()},ScrollFoo.prototype.registerEvents=function(){var a=this;this.unload(),$(this.config.parentEl).on("scroll mousewheel DOMMouseScroll MozMousePixelScroll",function(b){a.onScrollEvent($(this),b)}),$(this.config.scrollerEl).on("mousedown",function(b){a.onMouseDownEvent($(this),b)})},ScrollFoo.prototype.unload=function(){$(this.config.parentEl).off("scroll mousewheel DOMMouseScroll MozMousePixelScroll"),$(this.config.scrollerEl).off("mousedown")},ScrollFoo.prototype.onScrollEvent=function(a,b){var c;if("scroll"===b.type)c=a.scrollTop(),"undefined"==typeof b.originalEvent.wheelDeltaY&&this.config.state===!0&&this.handleReelbarScroller(c,!1);else if("DOMMouseScroll"!==b.type&&"MozMousePixelScroll"!==b.type){var d;d=b.originalEvent.wheelDeltaY?b.originalEvent.wheelDeltaY:b.originalEvent.wheelDelta,d>0?(c=a.scrollTop()-Math.abs(d),a.scrollTop(a.scrollTop()-Math.abs(d/10))):(c=a.scrollTop()+Math.abs(d),a.scrollTop(a.scrollTop()+Math.abs(d/10)))}else{var e;e="MozMousePixelScroll"===b.type?b.originalEvent.detail/3:3*b.originalEvent.detail,c=e>0?a.scrollTop(a.scrollTop()+Math.abs(e)):a.scrollTop(a.scrollTop()-Math.abs(e))}b.preventDefault()},ScrollFoo.prototype.onMouseDownEvent=function(a,b){var c=b.pageY,d=this.getVisibleParentHeight(),e=parseFloat(a.css("top").replace("px",""),10)||0,f=a.height()-this.config.visibleParentHeightOffset/2;this.$body.addClass(this.config.draggingClass),this.$body.on("mousemove",function(b){var g=b.pageY-c+e;d-f>=g&&(0>g&&(g=0),a.css("top",g),$(this.config.parentEl).scrollTop(g/this.config.ratio))}.bind(this)),this.$body.one("mouseup",function(){this.$body.off("mousemove"),a.off("mouseup"),this.$body.removeClass(this.config.draggingClass)}.bind(this))},ScrollFoo.prototype.getVisibleParentHeight=function(){return"function"==typeof this.config.visibleParentHeight?this.config.visibleParentHeight()===!1?"auto":this.config.visibleParentHeight()+this.config.visibleParentHeightOffset:this.config.visibleParentHeight+this.config.visibleParentHeightOffset},ScrollFoo.prototype.getRealParentHeight=function(){return"function"==typeof this.config.realParentHeight?this.config.realParentHeight():this.config.realParentHeight},ScrollFoo.prototype.doCalculate=function(){var c,d,a=$(this.config.parentEl),b=$(this.config.scrollerEl),e=a.scrollTop();if(a.css({height:"auto"}),c=this.getVisibleParentHeight(),d=this.getRealParentHeight(),this.config.ratio=c/d,this.config.ratio<1){var f=this.config.ratio*c;this.config.state=!0,b.css({height:f}).removeClass(this.config.disabledClass)}else this.config.state=!1,b.addClass(this.config.disabledClass);return a.css({height:c}).scrollTop(e),this},ScrollFoo.prototype.handleReelbarScroller=function(a,b){var b=b||!1,c={top:a*this.config.ratio};return b&&$(this.config.parentEl).css("top",-a*this.config.ratio),$(this.config.scrollerEl).css(c),this};