|
| 1 | + |
| 2 | +(function($) { |
| 3 | + |
| 4 | +var version = '@VERSION', |
| 5 | + defaults = { |
| 6 | + exclude: [], |
| 7 | + excludeWithin:[], |
| 8 | + offset: 0, |
| 9 | + direction: 'top', // one of 'top' or 'left' |
| 10 | + scrollElement: null, // jQuery set of elements you wish to scroll (for $.smoothScroll). |
| 11 | + // if null (default), $('html, body').firstScrollable() is used. |
| 12 | + scrollTarget: null, // only use if you want to override default behavior |
| 13 | + beforeScroll: function() {}, // fn(opts) function to be called before scrolling occurs. "this" is the element(s) being scrolled |
| 14 | + afterScroll: function() {}, // fn(opts) function to be called after scrolling occurs. "this" is the triggering element |
| 15 | + easing: 'swing', |
| 16 | + speed: 400, |
| 17 | + autoCoefficent: 2 // coefficient for "auto" speed |
| 18 | + }, |
| 19 | + |
| 20 | + getScrollable = function(opts) { |
| 21 | + var scrollable = [], |
| 22 | + scrolled = false, |
| 23 | + dir = opts.dir && opts.dir == 'left' ? 'scrollLeft' : 'scrollTop'; |
| 24 | + |
| 25 | + this.each(function() { |
| 26 | + |
| 27 | + if (this == document || this == window) { return; } |
| 28 | + var el = $(this); |
| 29 | + if ( el[dir]() > 0 ) { |
| 30 | + scrollable.push(this); |
| 31 | + } else { |
| 32 | + // if scroll(Top|Left) === 0, nudge the element 1px and see if it moves |
| 33 | + el[dir](1); |
| 34 | + scrolled = el[dir]() > 0; |
| 35 | + // then put it back, of course |
| 36 | + el[dir](0); |
| 37 | + if ( scrolled ) { |
| 38 | + scrollable.push(this); |
| 39 | + } |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + if ( opts.el === 'first' && scrollable.length ) { |
| 44 | + scrollable = [ scrollable[0] ]; |
| 45 | + } |
| 46 | + |
| 47 | + return scrollable; |
| 48 | + }, |
| 49 | + isTouch = 'ontouchend' in document; |
| 50 | + |
| 51 | +$.fn.extend({ |
| 52 | + scrollable: function(dir) { |
| 53 | + var scrl = getScrollable.call(this, {dir: dir}); |
| 54 | + return this.pushStack(scrl); |
| 55 | + }, |
| 56 | + firstScrollable: function(dir) { |
| 57 | + var scrl = getScrollable.call(this, {el: 'first', dir: dir}); |
| 58 | + return this.pushStack(scrl); |
| 59 | + }, |
| 60 | + |
| 61 | + smoothScroll: function(options) { |
| 62 | + options = options || {}; |
| 63 | + var opts = $.extend({}, $.fn.smoothScroll.defaults, options), |
| 64 | + locationPath = $.smoothScroll.filterPath(location.pathname); |
| 65 | + |
| 66 | + this |
| 67 | + .unbind('click.smoothscroll') |
| 68 | + .bind('click.smoothscroll', function(event) { |
| 69 | + var link = this, |
| 70 | + $link = $(this), |
| 71 | + exclude = opts.exclude, |
| 72 | + excludeWithin = opts.excludeWithin, |
| 73 | + elCounter = 0, ewlCounter = 0, |
| 74 | + include = true, |
| 75 | + clickOpts = {}, |
| 76 | + hostMatch = ((location.hostname === link.hostname) || !link.hostname), |
| 77 | + pathMatch = opts.scrollTarget || ( $.smoothScroll.filterPath(link.pathname) || locationPath ) === locationPath, |
| 78 | + thisHash = escapeSelector(link.hash); |
| 79 | + |
| 80 | + if ( !opts.scrollTarget && (!hostMatch || !pathMatch || !thisHash) ) { |
| 81 | + include = false; |
| 82 | + } else { |
| 83 | + while (include && elCounter < exclude.length) { |
| 84 | + if ($link.is(escapeSelector(exclude[elCounter++]))) { |
| 85 | + include = false; |
| 86 | + } |
| 87 | + } |
| 88 | + while ( include && ewlCounter < excludeWithin.length ) { |
| 89 | + if ($link.closest(excludeWithin[ewlCounter++]).length) { |
| 90 | + include = false; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if ( include ) { |
| 96 | + event.preventDefault(); |
| 97 | + |
| 98 | + $.extend( clickOpts, opts, { |
| 99 | + scrollTarget: opts.scrollTarget || thisHash, |
| 100 | + link: link |
| 101 | + }); |
| 102 | + |
| 103 | + $.smoothScroll( clickOpts ); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + return this; |
| 108 | + |
| 109 | + } |
| 110 | +}); |
| 111 | + |
| 112 | +$.smoothScroll = function(options, px) { |
| 113 | + var opts, $scroller, scrollTargetOffset, speed, |
| 114 | + scrollerOffset = 0, |
| 115 | + offPos = 'offset', |
| 116 | + scrollDir = 'scrollTop', |
| 117 | + aniprops = {}, |
| 118 | + useScrollTo = false, |
| 119 | + scrollprops = []; |
| 120 | + |
| 121 | + if ( typeof options === 'number') { |
| 122 | + opts = $.fn.smoothScroll.defaults; |
| 123 | + scrollTargetOffset = options; |
| 124 | + } else { |
| 125 | + opts = $.extend({}, $.fn.smoothScroll.defaults, options || {}); |
| 126 | + if (opts.scrollElement) { |
| 127 | + offPos = 'position'; |
| 128 | + if (opts.scrollElement.css('position') == 'static') { |
| 129 | + opts.scrollElement.css('position', 'relative'); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + scrollTargetOffset = px || |
| 134 | + ( $(opts.scrollTarget)[offPos]() && |
| 135 | + $(opts.scrollTarget)[offPos]()[opts.direction] ) || |
| 136 | + 0; |
| 137 | + } |
| 138 | + opts = $.extend({link: null}, opts); |
| 139 | + scrollDir = opts.direction == 'left' ? 'scrollLeft' : scrollDir; |
| 140 | + |
| 141 | + if ( opts.scrollElement ) { |
| 142 | + $scroller = opts.scrollElement; |
| 143 | + scrollerOffset = $scroller[scrollDir](); |
| 144 | + } else { |
| 145 | + $scroller = $('html, body').firstScrollable(); |
| 146 | + useScrollTo = isTouch && 'scrollTo' in window; |
| 147 | + } |
| 148 | + |
| 149 | + aniprops[scrollDir] = scrollTargetOffset + scrollerOffset + opts.offset; |
| 150 | + |
| 151 | + opts.beforeScroll.call($scroller, opts); |
| 152 | + |
| 153 | + if ( useScrollTo ) { |
| 154 | + scrollprops = (opts.direction == 'left') ? [aniprops[scrollDir], 0] : [0, aniprops[scrollDir]]; |
| 155 | + window.scrollTo.apply(window, scrollprops); |
| 156 | + opts.afterScroll.call(opts.link, opts); |
| 157 | + |
| 158 | + } else { |
| 159 | + speed = opts.speed; |
| 160 | + |
| 161 | + // automatically calculate the speed of the scroll based on distance / coefficient |
| 162 | + if (speed === 'auto') { |
| 163 | + |
| 164 | + // if aniprops[scrollDir] == 0 then we'll use scrollTop() value instead |
| 165 | + speed = aniprops[scrollDir] || $scroller.scrollTop(); |
| 166 | + |
| 167 | + // divide the speed by the coefficient |
| 168 | + speed = speed / opts.autoCoefficent; |
| 169 | + } |
| 170 | + |
| 171 | + $scroller.stop().animate(aniprops, |
| 172 | + { |
| 173 | + duration: speed, |
| 174 | + easing: opts.easing, |
| 175 | + complete: function() { |
| 176 | + opts.afterScroll.call(opts.link, opts); |
| 177 | + } |
| 178 | + }); |
| 179 | + } |
| 180 | +}; |
| 181 | + |
| 182 | +$.smoothScroll.version = version; |
| 183 | +$.smoothScroll.filterPath = function(string) { |
| 184 | + return string |
| 185 | + .replace(/^\//,'') |
| 186 | + .replace(/(index|default).[a-zA-Z]{3,4}$/,'') |
| 187 | + .replace(/\/$/,''); |
| 188 | +}; |
| 189 | + |
| 190 | +// default options |
| 191 | +$.fn.smoothScroll.defaults = defaults; |
| 192 | + |
| 193 | +function escapeSelector (str) { |
| 194 | + return str.replace(/(:|\.)/g,'\\$1'); |
| 195 | +} |
| 196 | + |
| 197 | +})(jQuery); |
0 commit comments