Skip to content

Commit 06b9bb6

Browse files
Move continuous setting out of global .waypoints.settings and into individual waypoint options
1 parent c84f20b commit 06b9bb6

File tree

3 files changed

+92
-76
lines changed

3 files changed

+92
-76
lines changed

test/spec.js

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,75 @@ describe('jQuery Waypoints', function() {
244244
expect($.waypoints().length).toBeFalsy();
245245
});
246246
});
247+
248+
it('should trigger if continuous is true and waypoint is not last', function() {
249+
var $f = $('#near1'),
250+
$g = $('#near2'),
251+
hitcount = 0;
252+
253+
spyOnEvent($f, 'waypoint.reached');
254+
255+
runs(function() {
256+
$e.add($f).add($g).waypoint(function() {
257+
hitcount++;
258+
});
259+
$se.scrollTop($g.offset().top);
260+
});
261+
262+
waits(standardWait);
263+
264+
runs(function() {
265+
expect(hitcount).toEqual(3);
266+
});
267+
});
268+
269+
it('should not trigger if continuous is false and waypoint is not last', function() {
270+
var $f = $('#near1'),
271+
$g = $('#near2'),
272+
hitcount = 0;
273+
274+
spyOnEvent($f, 'waypoint.reached');
275+
276+
runs(function() {
277+
$e.add($f).add($g).waypoint(function() {
278+
hitcount++;
279+
});
280+
$f.waypoint({
281+
continuous: false
282+
});
283+
$se.scrollTop($g.offset().top);
284+
});
285+
286+
waits(standardWait);
287+
288+
runs(function() {
289+
expect(hitcount).toEqual(2);
290+
});
291+
});
292+
293+
it('should trigger if continuous is false but the waypoint is last', function() {
294+
var $f = $('#near1'),
295+
$g = $('#near2'),
296+
hitcount = 0;
297+
298+
spyOnEvent($f, 'waypoint.reached');
299+
300+
runs(function() {
301+
$e.add($f).add($g).waypoint(function() {
302+
hitcount++;
303+
});
304+
$g.waypoint({
305+
continuous: false
306+
});
307+
$se.scrollTop($g.offset().top);
308+
});
309+
310+
waits(standardWait);
311+
312+
runs(function() {
313+
expect(hitcount).toEqual(3);
314+
});
315+
});
247316
});
248317

249318
describe('.waypoint(callback, options)', function() {
@@ -460,59 +529,6 @@ describe('jQuery Waypoints', function() {
460529
});
461530
});
462531

463-
it('should fire all when continuous is true', function() {
464-
runs(function() {
465-
$se.scrollTop($('#near2').offset().top);
466-
});
467-
468-
waits(standardWait);
469-
470-
runs(function() {
471-
expect(count).toEqual(4);
472-
});
473-
});
474-
475-
it('should fire only the last if continuous is false', function() {
476-
runs(function() {
477-
$.waypoints.settings.continuous = false;
478-
$se.scrollTop($('#near2').offset().top);
479-
});
480-
481-
waits(standardWait);
482-
483-
runs(function() {
484-
expect(count).toEqual(1);
485-
expect(curID).toEqual("near2");
486-
$.waypoints.settings.continuous = true;
487-
});
488-
});
489-
490-
it('should fire the top element in the up direction', function() {
491-
var points;
492-
493-
runs(function() {
494-
$.waypoints.settings.continuous = false;
495-
points = $.waypoints();
496-
points.waypoint('remove');
497-
$se.scrollTop($('#near2').offset().top + 1);
498-
});
499-
500-
waits(standardWait);
501-
502-
runs(function() {
503-
points.waypoint();
504-
$se.scrollTop($('#same1').offset().top - 1);
505-
});
506-
507-
waits(standardWait);
508-
509-
runs(function() {
510-
expect(count).toEqual(1);
511-
expect(curID).toEqual("same1");
512-
$.waypoints.settings.continuous = true;
513-
});
514-
});
515-
516532
it('should throttle the scroll check', function() {
517533
runs(function() {
518534
$se.scrollTop($('#same1').offset().top);

waypoints.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ Support:
231231
return isDown ?
232232
(el.offset > oldScroll && el.offset <= newScroll) :
233233
(el.offset <= oldScroll && el.offset > newScroll);
234-
});
234+
}),
235+
len = pointsHit.length;
235236

236237
// iOS adjustment
237238
if (!oldScroll || !newScroll) {
@@ -242,21 +243,21 @@ Support:
242243
oldScroll = newScroll;
243244

244245
// No waypoints crossed? Eject.
245-
if (!pointsHit.length) return;
246+
if (!len) return;
247+
248+
// If several waypoints triggered, need to do so in reverse order going up
249+
if (!isDown) pointsHit.reverse();
246250

247251
/*
248-
One scroll move may cross several waypoints. If the continuous setting is
249-
true, every waypoint event should fire. If false, only the last one.
252+
One scroll move may cross several waypoints. If the waypoint's continuous
253+
option is true it should fire even if it isn't the last waypoint. If false,
254+
it will only fire if it's the last one.
250255
*/
251-
if ($[wps].settings.continuous) {
252-
$.each(isDown ? pointsHit : pointsHit.reverse(), function(i, point) {
256+
$.each(pointsHit, function(i, point) {
257+
if (point.options.continuous || i === len - 1) {
253258
triggerWaypoint(point, [isDown ? 'down' : 'up']);
254-
});
255-
}
256-
else {
257-
triggerWaypoint(pointsHit[isDown ? pointsHit.length - 1 : 0],
258-
[isDown ? 'down' : 'up']);
259-
}
259+
}
260+
});
260261
}
261262

262263

@@ -275,7 +276,7 @@ Support:
275276
return methods.init.apply(this, [null, method]);
276277
}
277278
else {
278-
$.error( 'Method ' + method + ' does not exist on jQuery' + wp );
279+
$.error( 'Method ' + method + ' does not exist on jQuery ' + wp );
279280
}
280281
};
281282

@@ -296,6 +297,13 @@ Support:
296297
boolean
297298
default: false
298299
If true, the waypoint will be destroyed when triggered.
300+
301+
continuous
302+
boolean
303+
default: true
304+
If true, and multiple waypoints are triggered in one scroll, this waypoint will
305+
trigger even if it is not the last waypoint reached. If false, it will only
306+
trigger if it is the last waypoint.
299307
300308
An offset of 250 would trigger the waypoint when the top of the element is 250px
301309
from the top of the viewport. Negative values for any offset work as you might
@@ -331,6 +339,7 @@ Support:
331339
want to record an event once for each page visit.
332340
*/
333341
$.fn[wp].defaults = {
342+
continuous: true,
334343
offset: 0,
335344
triggerOnce: false
336345
};
@@ -450,14 +459,6 @@ Support:
450459
$.waypoints.settings
451460
452461
Settings object that determines some of the plugin’s behavior.
453-
454-
continuous
455-
boolean
456-
default: true
457-
Determines which waypoints to trigger events for if a single scroll change
458-
passes more than one waypoint. If false, only the last waypoint is triggered
459-
and the rest are ignored. If true, all waypoints between the previous scroll
460-
position and the new one are triggered in order.
461462
462463
resizeThrottle
463464
number
@@ -478,7 +479,6 @@ Support:
478479
http://benalman.com/projects/jquery-throttle-debounce-plugin/
479480
*/
480481
$[wps].settings = {
481-
continuous: true,
482482
resizeThrottle: 200,
483483
scrollThrottle: 100
484484
};

waypoints.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)