Skip to content

Commit 8dd229e

Browse files
committed
* fix lack of afterScroll callback when using window.scrollTo (for touch devices)
* make a no-op function the default for beforeScroll and afterScroll callbacks so no need to check whether it's a function or not. * expose filterPath as $.smoothScroll.filterPath so devs can change it as needed (for matching location.pathanme to the link's pathname).
1 parent 48f11a2 commit 8dd229e

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

jquery.smooth-scroll.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ var version = '1.4.2',
2525
scrollElement: null, // jQuery set of elements you wish to scroll (for $.smoothScroll).
2626
// if null (default), $('html, body').firstScrollable() is used.
2727
scrollTarget: null, // only use if you want to override default behavior
28-
beforeScroll: null, // fn(opts) function to be called before scrolling occurs. "this" is the element(s) being scrolled
29-
afterScroll: null, // fn(opts) function to be called after scrolling occurs. "this" is the triggering element
28+
beforeScroll: function() {}, // fn(opts) function to be called before scrolling occurs. "this" is the element(s) being scrolled
29+
afterScroll: function() {}, // fn(opts) function to be called after scrolling occurs. "this" is the triggering element
3030
easing: 'swing',
3131
speed: 400
3232
},
3333

34-
locationPath = filterPath(location.pathname),
34+
locationPath = $.smoothScroll.filterPath(location.pathname),
3535
getScrollable = function(opts) {
3636
var scrollable = [],
3737
scrolled = false,
@@ -81,7 +81,7 @@ $.fn.extend({
8181

8282
var clickOpts = {}, link = this, $link = $(this),
8383
hostMatch = ((location.hostname === link.hostname) || !link.hostname),
84-
pathMatch = opts.scrollTarget || (filterPath(link.pathname) || locationPath) === locationPath,
84+
pathMatch = opts.scrollTarget || ( $.smoothScroll.filterPath(link.pathname) || locationPath ) === locationPath,
8585
thisHash = escapeSelector(link.hash),
8686
include = true;
8787

@@ -159,41 +159,36 @@ $.smoothScroll = function(options, px) {
159159

160160
aniprops[scrollDir] = scrollTargetOffset + scrollerOffset + opts.offset;
161161

162-
if ( $.isFunction(opts.beforeScroll) ) {
163-
opts.beforeScroll.call($scroller, opts);
164-
}
162+
opts.beforeScroll.call($scroller, opts);
165163

166164
if ( useScrollTo ) {
167165
scrollprops = (opts.direction == 'left') ? [aniprops[scrollDir], 0] : [0, aniprops[scrollDir]];
168166
window.scrollTo.apply(window, scrollprops);
167+
opts.afterScroll.call(opts.link, opts);
168+
169169
} else {
170170
$scroller.animate(aniprops,
171171
{
172172
duration: opts.speed,
173173
easing: opts.easing,
174174
complete: function() {
175-
if ( opts.afterScroll && $.isFunction(opts.afterScroll) ) {
176-
opts.afterScroll.call(opts.link, opts);
177-
}
175+
opts.afterScroll.call(opts.link, opts);
178176
}
179177
});
180178
}
181179

182180
};
183181

184182
$.smoothScroll.version = version;
185-
186-
// default options
187-
$.fn.smoothScroll.defaults = defaults;
188-
189-
190-
// private function
191-
function filterPath(string) {
183+
$.smoothScroll.filterPath = function(string) {
192184
return string
193185
.replace(/^\//,'')
194186
.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
195187
.replace(/\/$/,'');
196-
}
188+
};
189+
190+
// default options
191+
$.fn.smoothScroll.defaults = defaults;
197192

198193
function escapeSelector (str) {
199194
return str.replace(/(:|\.)/g,'\\$1');

0 commit comments

Comments
 (0)