Skip to content

Commit

Permalink
Make sure beforeScroll fires before calculating scroll target
Browse files Browse the repository at this point in the history
  • Loading branch information
kswedberg committed Oct 29, 2012
1 parent 080d3ae commit 1bf802e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
1 change: 1 addition & 0 deletions demo/index.html
Expand Up @@ -32,6 +32,7 @@
$(document).ready(function() {

$('ul.mainnav a').smoothScroll();

$('p.subnav a').click(function(event) {
event.preventDefault();
var link = this;
Expand Down
22 changes: 12 additions & 10 deletions jquery.smooth-scroll.js
@@ -1,9 +1,9 @@
/*! Smooth Scroll - v1.4.6 - 2012-08-23
/*! Smooth Scroll - v1.4.7 - 2012-10-29
* Copyright (c) 2012 Karl Swedberg; Licensed MIT, GPL */

(function($) {

var version = '1.4.6',
var version = '1.4.7',
defaults = {
exclude: [],
excludeWithin:[],
Expand Down Expand Up @@ -132,7 +132,8 @@ $.smoothScroll = function(options, px) {
aniOpts = {},
scrollprops = [];

if ( typeof options === 'number') {

if (typeof options === 'number') {
opts = $.fn.smoothScroll.defaults;
scrollTargetOffset = options;
} else {
Expand All @@ -143,11 +144,6 @@ $.smoothScroll = function(options, px) {
opts.scrollElement.css('position', 'relative');
}
}

scrollTargetOffset = px ||
( $(opts.scrollTarget)[offPos]() &&
$(opts.scrollTarget)[offPos]()[opts.direction] ) ||
0;
}

opts = $.extend({link: null}, opts);
Expand All @@ -160,10 +156,16 @@ $.smoothScroll = function(options, px) {
$scroller = $('html, body').firstScrollable();
}

aniProps[scrollDir] = scrollTargetOffset + scrollerOffset + opts.offset;

// beforeScroll callback function must fire before calculating offset
opts.beforeScroll.call($scroller, opts);

scrollTargetOffset = (typeof options === 'number') ? options :
px ||
( $(opts.scrollTarget)[offPos]() &&
$(opts.scrollTarget)[offPos]()[opts.direction] ) ||
0;

aniProps[scrollDir] = scrollTargetOffset + scrollerOffset + opts.offset;
speed = opts.speed;

// automatically calculate the speed of the scroll based on distance / coefficient
Expand Down
4 changes: 2 additions & 2 deletions jquery.smooth-scroll.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions src/jquery.smooth-scroll.js
Expand Up @@ -129,7 +129,8 @@ $.smoothScroll = function(options, px) {
aniOpts = {},
scrollprops = [];

if ( typeof options === 'number') {

if (typeof options === 'number') {
opts = $.fn.smoothScroll.defaults;
scrollTargetOffset = options;
} else {
Expand All @@ -140,11 +141,6 @@ $.smoothScroll = function(options, px) {
opts.scrollElement.css('position', 'relative');
}
}

scrollTargetOffset = px ||
( $(opts.scrollTarget)[offPos]() &&
$(opts.scrollTarget)[offPos]()[opts.direction] ) ||
0;
}

opts = $.extend({link: null}, opts);
Expand All @@ -157,10 +153,16 @@ $.smoothScroll = function(options, px) {
$scroller = $('html, body').firstScrollable();
}

aniProps[scrollDir] = scrollTargetOffset + scrollerOffset + opts.offset;

// beforeScroll callback function must fire before calculating offset
opts.beforeScroll.call($scroller, opts);

scrollTargetOffset = (typeof options === 'number') ? options :
px ||
( $(opts.scrollTarget)[offPos]() &&
$(opts.scrollTarget)[offPos]()[opts.direction] ) ||
0;

aniProps[scrollDir] = scrollTargetOffset + scrollerOffset + opts.offset;
speed = opts.speed;

// automatically calculate the speed of the scroll based on distance / coefficient
Expand Down

0 comments on commit 1bf802e

Please sign in to comment.