Skip to content

Commit 66a2505

Browse files
committed
Use bind instead of live. Fixes kswedberg#26
1 parent e12b6d3 commit 66a2505

File tree

3 files changed

+229
-58
lines changed

3 files changed

+229
-58
lines changed

jquery.smooth-scroll.js

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1-
/*!
2-
* jQuery Smooth Scroll Plugin v1.4.5
3-
*
4-
* Date: Sun Mar 11 18:17:42 2012 EDT
5-
* Requires: jQuery v1.3+
6-
*
7-
* Copyright 2012, Karl Swedberg
8-
* Dual licensed under the MIT and GPL licenses (just like jQuery):
9-
* http://www.opensource.org/licenses/mit-license.php
10-
* http://www.gnu.org/licenses/gpl.html
11-
*
12-
*
13-
*
14-
*
15-
*/
1+
/*! Smooth Scroll - v1.4.5 - 2012-07-21
2+
* Copyright (c) 2012 Karl Swedberg; Licensed MIT, GPL */
3+
164

175
(function($) {
186

19-
var version = '1.4.4',
7+
var version = '1.4.5',
208
defaults = {
219
exclude: [],
2210
excludeWithin:[],
@@ -43,21 +31,20 @@ var version = '1.4.4',
4331
var el = $(this);
4432
if ( el[dir]() > 0 ) {
4533
scrollable.push(this);
46-
return;
47-
}
48-
49-
el[dir](1);
50-
scrolled = el[dir]() > 0;
51-
el[dir](0);
52-
if ( scrolled ) {
53-
scrollable.push(this);
54-
return;
34+
} else {
35+
// if scroll(Top|Left) === 0, nudge the element 1px and see if it moves
36+
el[dir](1);
37+
scrolled = el[dir]() > 0;
38+
// then put it back, of course
39+
el[dir](0);
40+
if ( scrolled ) {
41+
scrollable.push(this);
42+
}
5543
}
56-
5744
});
5845

5946
if ( opts.el === 'first' && scrollable.length ) {
60-
scrollable = [ scrollable.shift() ];
47+
scrollable = [ scrollable[0] ];
6148
}
6249

6350
return scrollable;
@@ -79,26 +66,29 @@ $.fn.extend({
7966
var opts = $.extend({}, $.fn.smoothScroll.defaults, options),
8067
locationPath = $.smoothScroll.filterPath(location.pathname);
8168

82-
this.die('click.smoothscroll').live('click.smoothscroll', function(event) {
83-
84-
var clickOpts = {}, link = this, $link = $(this),
69+
this
70+
.unbind('click.smoothscroll')
71+
.bind('click.smoothscroll', function(event) {
72+
var link = this,
73+
$link = $(this),
74+
exclude = opts.exclude,
75+
excludeWithin = opts.excludeWithin,
76+
elCounter = 0, ewlCounter = 0,
77+
include = true,
78+
clickOpts = {},
8579
hostMatch = ((location.hostname === link.hostname) || !link.hostname),
8680
pathMatch = opts.scrollTarget || ( $.smoothScroll.filterPath(link.pathname) || locationPath ) === locationPath,
87-
thisHash = escapeSelector(link.hash),
88-
include = true;
81+
thisHash = escapeSelector(link.hash);
8982

9083
if ( !opts.scrollTarget && (!hostMatch || !pathMatch || !thisHash) ) {
9184
include = false;
9285
} else {
93-
var exclude = opts.exclude, elCounter = 0, el = exclude.length;
94-
while (include && elCounter < el) {
86+
while (include && elCounter < exclude.length) {
9587
if ($link.is(escapeSelector(exclude[elCounter++]))) {
9688
include = false;
9789
}
9890
}
99-
100-
var excludeWithin = opts.excludeWithin, ewlCounter = 0, ewl = excludeWithin.length;
101-
while ( include && ewlCounter < ewl ) {
91+
while ( include && ewlCounter < excludeWithin.length ) {
10292
if ($link.closest(excludeWithin[ewlCounter++]).length) {
10393
include = false;
10494
}
@@ -173,8 +163,10 @@ $.smoothScroll = function(options, px) {
173163

174164
// automatically calculate the speed of the scroll based on distance / coefficient
175165
if (speed === 'auto') {
166+
176167
// if aniprops[scrollDir] == 0 then we'll use scrollTop() value instead
177168
speed = aniprops[scrollDir] || $scroller.scrollTop();
169+
178170
// divide the speed by the coefficient
179171
speed = speed / opts.autoCoefficent;
180172
}
@@ -188,7 +180,6 @@ $.smoothScroll = function(options, px) {
188180
}
189181
});
190182
}
191-
192183
};
193184

194185
$.smoothScroll.version = version;

jquery.smooth-scroll.min.js

Lines changed: 3 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.smooth-scroll.js

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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

Comments
 (0)