-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrollpoint.jquery.js
More file actions
105 lines (93 loc) · 4.07 KB
/
scrollpoint.jquery.js
File metadata and controls
105 lines (93 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
(function($) {
$.scrollpoint = function(element, options) {
var opts = $.extend({
offset: 0,
triggeredClass: '',
animation: '',
delay:0,
loadAfter: false,
beforeTrigger: function(){},
onTrigger: function(){}
}, options);
var element = element[0];
// add space before classname if one exists already
if(element.className != ''){
element.className += ' ';
}
element.className += 'scrollpoint';
// add animation class
if(opts.animation){
element.className += ' '+opts.animation;
}
// start scroll/resize functionality to trigger animations
$(window).on('scroll resize', function(){
// define variables used.
var scrollPos = window.pageYOffset;
itemTop = element.getBoundingClientRect().top,
itemBottom = element.getBoundingClientRect().bottom,
windowHeight = window.innerHeight,
bottomOfScreen = scrollPos+windowHeight,
topOfScreen = scrollPos,
scrollUpVisible = (itemBottom >= ((windowHeight*.1)+opts.offset)),
scrollDownVisible = (itemTop <= (windowHeight - (windowHeight*.1)-opts.offset));
// if the top of the item is higher than the bottom of the screen
// and the bottom of the item is lower than the top of the screen,
// that means it is viewable in the window. There's a defined offset
// of 10% of the window size to make sure the animation is seen when
// it happens, a slight "delay".
if(
(
scrollDownVisible
&& scrollUpVisible
&& !$(element).hasClass('sp-triggered')
) ||
$(element).hasClass('sp-force-trigger')
) {
if(opts.loadAfter){
// if the element it is dependant on has not been triggered, go back and trigger it, the user is scrolling up
if(!opts.loadAfter.hasClass('sp-triggered') && !opts.loadAfter.addClass('sp-force-trigger') && !opts.loadAfter.hasClass('sp-trigger-completed')){
opts.loadAfter.addClass('sp-force-trigger');
}
if(opts.loadAfter.hasClass('sp-trigger-completed')){
animateit(element, opts);
}
} else {
animateit(element, opts);
}
}
});
}
function animateit(element, opts){
opts.beforeTrigger();
$(element).removeClass('sp-force-trigger');
// add the class name sp-triggered
element.className = element.className.replace('sp-pre-triggered', '');
element.className += ' sp-triggered';
// if any custom classes are provided to add on trigger, they're added here
if(opts.triggeredClass){element.className += ' '+opts.triggeredClass;}
// trigger a window scroll to possibly load the next element if there is one
$(window).trigger('scroll');
$(element).on('transitionend webkitTransitionEnd', function(e){
$(this).addClass('sp-trigger-completed');
// trigger a window scroll to possibly load the next element if there is one
$(window).trigger('scroll');
});
// fire the onTrigger function provided
opts.onTrigger(element);
}
$.fn.scrollpoint = function(options) {
return this.each(function() {
if (undefined == $(this).data('scrollpoint')) {
var plugin = new $.scrollpoint($(this), options);
$(this).data('scrollpoint', plugin);
}
});
}
// this make sure any animations on screen already happen on load without having to wait for a scroll action
$(document).ready(function(){
$(window).trigger('scroll');
});
$(window).load(function(){
$(window).trigger('scroll');
});
})(jQuery);