-
Notifications
You must be signed in to change notification settings - Fork 2
/
hi-roy.js
308 lines (233 loc) · 7.08 KB
/
hi-roy.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Hi Roy
* Version 1.0.0
* A simple and easy jQuery plugin for CSS animated page transitions.
* https://github.com/bamadesigner/hi-roy-jquery-plugin
* License: MIT
* Author: Rachel Cherry (https://bamadesigner.com/)
*/
(function($) {
// Set a few properties
var $hi_roy_sides = ['top','right','bottom','left'];
// Keep up with the Roys-es
var $hi_roys = [];
// Get random position
function get_random_position() {
return $hi_roy_sides[ Math.floor( Math.random()*$hi_roy_sides.length ) ];
}
// Let's get ready to say hi
function hiRoy($element,$options,$callback) {
// Hi Roy
var $hi_roy = this;
// What are our default options?
var $defaults = {
id: 'hi-roy',
startPosition: get_random_position(), // Which side position do we start with
image: 'roy-head.png', // Allows you to replace the image
link: 'https://twitter.com/intent/tweet?text=Hi+@royboy789.+http%3A%2F%2Fhiroy.club%2F&hashtags=hiroy',
link_target: '_blank',
moveOnStart: true,
moveOnMove: true
};
// Mesh with the passed options
$hi_roy.options = $.extend({},$defaults,$options);
// Setup our other options
$hi_roy.disabled = false;
$hi_roy.move_disabled = false;
// Set the start position
$hi_roy.position = $hi_roy.options.startPosition;
delete $hi_roy.options.startPosition;
// Create our new Hi Roy element
var $hi_roy_id = ( $hi_roys.length > 1 ) ? ( $hi_roy.options.id + $hi_roys.length ) : $hi_roy.options.id;
$hi_roy.element = $('<div id="' + $hi_roy_id + '" class="hi-roy-element"></div>').addClass($hi_roy.position).hide();
// Store Roy
$hi_roys.push($hi_roy.element);
// Create the link
var $hi_roy_element_link = $('<a href="' + $hi_roy.options.link + '"></a>');
if ( $hi_roy.options.link_target !== undefined && $hi_roy.options.link_target != '' ) {
$hi_roy_element_link.attr( 'target', $hi_roy.options.link_target );
}
// Create the image
var $hi_roy_element_img = $('<img src="' + $hi_roy.options.image + '" />');
// Add all the elements together
$hi_roy_element_link.append($hi_roy_element_img);
$hi_roy.element.append($hi_roy_element_link);
// Once the image loads...
$hi_roy_element_img.on('load',function(){
// Place the copy on the site so we can get the height
var $hi_roy_element_img_copy = $hi_roy_element_img.clone().css({'position':'absolute','left':'200%','top':'200%'}).appendTo($('body'));
// Get the height
$hi_roy.height = $hi_roy_element_img_copy.height();
// Remove the copy
$hi_roy_element_img_copy.remove();
// Add our element to the body
$element.append($hi_roy.element);
// If we want Roy to move from the get to...
if ( $hi_roy.options.moveOnStart ) {
// Move Roy in
$hi_roy.moveIn($hi_roy.position, function () {
// Move Roy cutout when the screen is touched or when the mouse moves
if ($hi_roy.options.moveOnMove) {
$(window).on('touchstart mousemove', function ($event) {
$hi_roy.move();
});
}
// Call callback
if ( $callback ) {
$callback();
}
});
} else {
// Call callback
if ( $callback ) {
$callback();
}
}
});
}
$.extend(hiRoy.prototype,{
// Get the current position
get_position: function() {
return this.position;
},
// Disable Roy
disable: function() {
this.disabled = true;
},
// Enable Roy
enable: function() {
this.disabled = false;
},
// Disable Roy from moving
disableMove: function() {
this.move_disabled = true;
},
// Enable Roy from moving
enableMove: function() {
this.move_disabled = false;
},
// Destroy Roy
destroy: function() {
this.element.remove();
this.disable();
},
// Move Roy in
moveIn: function($position,callback) {
// Hi Roy
var $hi_roy = this;
// Don't move if disabled
if ( $hi_roy.move_disabled ) {
return false;
}
// Disable while we work
$hi_roy.move_disabled = true;
// Set the position class
$hi_roy.element.removeClass($hi_roy.position).addClass($position);
// Clean up styles
$hi_roy.element.removeAttr('style');
$hi_roy.element.find('img').removeAttr('style');
// Place Roy
$hi_roy.element.css( $position, ( - $hi_roy.height ) );
// Change current position
$hi_roy.position = $position;
// Set new background position
// A whole number between 10 and 90
var $new_cutout_pos = Math.floor( Math.random() * ((90 - 10) + 1 ) + 10 );
switch($hi_roy.position) {
case 'left':
case 'right':
$hi_roy.element.css( 'width', $hi_roy.height );
$hi_roy.element.find('img').css({ 'top': $new_cutout_pos + '%' });
break;
case 'top':
case 'bottom':
$hi_roy.element.css( 'height', $hi_roy.height );
$hi_roy.element.find('img').css({ 'left': $new_cutout_pos + '%' });
break;
}
// Set animate properties for moving back in
var $animate = {};
$animate[$hi_roy.position] = 0;
// Move Roy in
$hi_roy.element.show().animate($animate,800,function() {
// Enable move
$hi_roy.move_disabled = false;
// Trigger our move in event, pass position information
var $hiRoyAfterMoveIn = new CustomEvent( 'hiRoyAfterMoveIn', {
detail: {
position: $hi_roy.position
}
});
document.body.dispatchEvent($hiRoyAfterMoveIn);
// Call the callback
if (callback) {
callback();
}
});
},
// Move Roy out
moveOut: function(callback) {
// Hi Roy
var $hi_roy = this;
// Don't move if disabled
if ( $hi_roy.move_disabled ) {
return false;
}
// Disable while we work
$hi_roy.move_disabled = true;
// Set animate properties for moving out
var $animate = {};
$animate[$hi_roy.position] = 0 - $hi_roy.height;
// Move Roy out
$hi_roy.element.animate($animate, 800, function() {
// Hide Roy
$hi_roy.element.hide();
// Enable move
$hi_roy.move_disabled = false;
// Trigger our move out event, pass position information
var $hiRoyAfterMoveOut = new CustomEvent( 'hiRoyAfterMoveOut', {
detail: {
position: $hi_roy.position
}
});
document.body.dispatchEvent($hiRoyAfterMoveOut);
// Call the callback
if (callback) {
callback();
}
});
},
// Move Roy
move: function($position) {
// Hi Roy
var $hi_roy = this;
// Don't move if disabled
if ( $hi_roy.disabled || $hi_roy.move_disabled ) {
return false;
}
// Set next position
var $next_position = $position;
// Make sure we have a valid next position
// If not passed, will select one at random
if ( $next_position === undefined || $.inArray($next_position, $hi_roy_sides ) == -1 ) {
$next_position = get_random_position();
}
// Move Roy out
$hi_roy.moveOut(function() {
// Move Roy back in
$hi_roy.moveIn($next_position);
});
}
});
// Let's get Roy all setup
// @TODO Can we "chain" and still return the object?
$.fn.hiRoy = function($options,$callback) {
//return this.each(function() {
if ( ! $.data( this, 'hi-roy' ) ) {
var $new_hi_roy = new hiRoy(this,$options,$callback);
$.data( this, 'hi-roy', $new_hi_roy );
return $new_hi_roy;
}
//});
};
}(jQuery));