-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjquery.bully.js
160 lines (128 loc) · 5.28 KB
/
jquery.bully.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*!
* jQuery Bully Plugin v0.1.3
* Examples and documentation at http://pixelgrade.github.io/rellax/
* Copyright (c) 2016 PixelGrade http://www.pixelgrade.com
* Licensed under MIT http://www.opensource.org/licenses/mit-license.php/
*/
;(
function( $, window, document, undefined ) {
var $window = $( window ),
windowHeight = $window.height(),
elements = [],
$bully,
lastScrollY = (window.pageYOffset || document.documentElement.scrollTop) - (document.documentElement.clientTop || 0),
current = 0,
inversed = false,
frameRendered = true;
$bully = $( '<div class="c-bully">' ).appendTo( 'body' );
$current = $( '<div class="c-bully__bullet c-bully__bullet--active">' ).appendTo( $bully );
(
function update() {
if ( frameRendered !== true ) {
var count = 0,
inverse = false;
$.each( elements, function( i, element ) {
if ( lastScrollY >= element.offset.top - windowHeight / 2 ) {
count = count + 1;
inverse = lastScrollY < element.offset.top + element.height - windowHeight / 2;
}
} );
if ( inversed !== inverse ) {
inversed = inverse;
$bully.toggleClass( 'c-bully--inversed', inversed );
}
if ( count !== current ) {
var offset = $bully.children( '.c-bully__bullet' ).not( '.c-bully__bullet--active' ).first().outerHeight( true ) * (
count - 1
);
$current.removeClass( 'c-bully__bullet--squash' );
setTimeout( function() {
$current.addClass( 'c-bully__bullet--squash' );
} );
$current.css( 'top', offset );
current = count;
}
}
window.requestAnimationFrame( update );
frameRendered = true;
}
)();
function reloadAll() {
$.each( elements, function( i, element ) {
element._reloadElement();
} );
}
function staggerClass( $elements, classname, timeout ) {
$.each( $elements, function( i, obj ) {
var stagger = i * timeout;
setTimeout( function() {
obj.$bullet.addClass( classname );
}, stagger );
} );
}
$window.on( 'load', function( e ) {
staggerClass( elements, 'c-bully__bullet--pop', 400 );
frameRendered = false;
} );
$window.on( 'scroll', function( e ) {
if ( frameRendered === true ) {
lastScrollY = (window.pageYOffset || document.documentElement.scrollTop) - (document.documentElement.clientTop || 0);
}
frameRendered = false;
} );
$window.on( 'load resize', function() {
reloadAll();
} );
function Bully( element, options ) {
this.element = element;
this.options = $.extend( {}, $.fn.bully.defaults, options );
var self = this,
$bullet = $( '<div class="c-bully__bullet">' );
$bullet.data( 'bully-data', self ).appendTo( $bully );
$bullet.on( 'click', function( event ) {
event.preventDefault();
event.stopPropagation();
self.onClick();
} );
this.$bullet = $bullet;
self._reloadElement();
elements.push( self );
current = 0;
}
Bully.prototype = {
constructor: Bully,
_reloadElement: function() {
this.offset = $( this.element ).offset();
this.height = $( this.element ).outerHeight();
},
onClick: function() {
var self = this,
$target = $( 'html, body' );
if ( self.options.scrollDuration == 0 ) {
$target.scrollTop( self.offset.top );
return;
}
if ( self.options.scrollDuration === 'auto' ) {
var duration = Math.abs( lastScrollY - self.offset.top ) / (
self.options.scrollPerSecond / 1000
);
$target.animate( {scrollTop: self.offset.top}, duration );
return;
}
$target.animate( {scrollTop: self.offset.top}, self.options.scrollDuration );
}
};
$.fn.bully = function( options ) {
return this.each( function() {
if ( ! $.data( this, "plugin_" + Bully ) ) {
$.data( this, "plugin_" + Bully, new Bully( this, options ) );
}
} );
};
$.fn.bully.defaults = {
scrollDuration: 'auto',
scrollPerSecond: 4000
};
$window.on( 'rellax load', reloadAll );
}
)( jQuery, window, document );