-
Notifications
You must be signed in to change notification settings - Fork 0
/
GifLinks.js
167 lines (126 loc) · 3.79 KB
/
GifLinks.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
var GifLinks = (function() {
'use strict';
var body;
var container;
/* -------------------------
/* UTILS
/* -------------------------*/
// Soft object augmentation
function extend( target, source ) {
for ( var key in source ) {
if ( !( key in target ) ) {
target[ key ] = source[ key ];
}
}
return target;
}
// Applys a dict of css properties to an element
function applyProperties( target, properties ) {
for( var key in properties ) {
target.style[ key ] = properties[ key ];
}
}
/* -------------------------
/* App
/* -------------------------*/
// Initialize
function init( elements, preload ) {
if ( elements.length ) {
// Loop and assign
for( var i = 0; i < elements.length; i++ ) {
if ( preload === true ) {
preloadAndTrack( elements[ i ] );
} else {
track( elements[ i ] );
}
}
} else {
if ( preload === true ) {
preloadAndTrack( elements );
} else {
track( elements );
}
}
}
// Start tracking after preload
function preloadAndTrack( element ) {
var awesomeGif = element.getAttribute( 'data-src' );
if ( awesomeGif ) {
// Load the image
var img = new Image();
img.onload = function() {
element.className += ' preloaded'
track( element )
}
img.src = awesomeGif;
}
}
// Start tracking mouse hovers
function track( element ) {
// "Party on Wayne" ~ "Party on Garth"
element.addEventListener( 'mouseover', function() { startPartying( this ); }, false );
element.addEventListener( 'touchstart', function() { startPartying( this ); }, false);
// Someone called the cops.
element.addEventListener( 'mouseout', function() { stopPartying(); }, false);
element.addEventListener( 'touchmove', function( event ) { event.preventDefault(); stopPartying(); }, false);
element.addEventListener( 'click', function() { stopPartying(); }, false);
element.addEventListener( 'dblclick', function() { stopPartying(); }, false);
addClasses( element );
}
// Adds classes to do with giflink status (has link etc)
function addClasses( element ) {
element.className += ' giflink ready';
if ( element.href ) {
element.className += ' has-link';
} else {
element.className += ' no-link';
}
}
// Create and cache the gif container.
function createContainer() {
var containerProperties = {
'backgroundPosition': '50% 50%',
'backgroundSize': 'cover',
'pointerEvents': 'none',
'position': 'fixed',
'zIndex': '999999',
'display': 'none',
'height': '100%',
'width': '100%',
'margin': '0px',
'left': '0px',
'top': '0px'
}
container = document.createElement( 'div' );
applyProperties( container, containerProperties );
body.appendChild( container );
}
// Add the background to the container, and the container to the page!
function startPartying( element ) {
var awesomeGif = element.getAttribute( 'data-src' );
if( awesomeGif ) {
container.style[ 'backgroundImage' ] = 'url(' + awesomeGif + ')';
container.style[ 'display' ] = 'block';
} else {
console.log( "Sorry, an element doesn't have a data-src!" );
}
}
// Hide the container
function stopPartying() {
container.style[ 'display' ] = 'none';
container.style[ 'backgroundImage' ] = '';
}
function main( elements, options ) {
// Caching
body = document.body;
createContainer();
var preload = false;
if ( options && options.preload ) {
preload = !!options.preload;
}
// Initialize giflinks
init( elements, preload );
}
return extend( main, {
});
})();