-
Notifications
You must be signed in to change notification settings - Fork 11
/
jquery.modern-blink.js
170 lines (147 loc) · 4.23 KB
/
jquery.modern-blink.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
/*!
* jQuery Modern Blink plugin
* https://github.com/leonderijke/jQuery-Modern-Blink
*
* Version: 0.1.3
* Author: @leonderijke
* Licensed under the MIT license
*/
;(function ( $, window, document, undefined ) {
"use strict";
var domPrefixes = 'Webkit Moz O ms'.split( ' ' ),
prefix = '',
supportsAnimations = false,
keyframeprefix = '',
keyframes = '',
defaults = {
// Duration specified in milliseconds (integer)
duration: 1000,
// Number of times the element should blink ("infinite" or integer)
iterationCount: "infinite",
// Whether to start automatically or not (boolean)
auto: true
},
animationCss,
i;
if( document.documentElement.style.animationName ) {
supportsAnimations = true;
}
if ( !supportsAnimations ) {
for( i = 0; i < domPrefixes.length; i++ ) {
if( document.documentElement.style[ domPrefixes[ i ] + 'AnimationName' ] !== undefined ) {
prefix = domPrefixes[ i ];
keyframeprefix = '-' + prefix.toLowerCase() + '-';
supportsAnimations = true;
break;
}
}
}
if ( supportsAnimations ) {
keyframes = '@' + keyframeprefix + 'keyframes modernBlink { '+
'50% { opacity: 0; }'+
'}';
var styleSheet = null;
if ( document.styleSheets && document.styleSheets.length ) {
for ( i = 0; i < document.styleSheets.length; i++ ) {
if ( document.styleSheets[ i ].href.indexOf( window.location.hostname ) == -1) {
continue;
}
styleSheet = document.styleSheets[ i ];
break;
}
}
if ( styleSheet !== null ) {
styleSheet.insertRule( keyframes, 0 );
}
else {
var s = document.createElement( 'style' );
s.innerHTML = keyframes;
document.getElementsByTagName( 'head' )[ 0 ].appendChild( s );
}
}
function ModernBlink( element, options ) {
this.el = $(element);
this.options = $.extend( {}, defaults, options );
this._init();
}
/*
* @function _init
* Wraps the element, starts the animation
*/
ModernBlink.prototype._init = function _init() {
if ( this.options.auto ) {
this.start();
}
this._bindEventHandlers();
};
/*
* @function start
* Starts the animation
*/
ModernBlink.prototype.start = function start( event ) {
if ( supportsAnimations ) {
this.el.css({
'animation-name': 'modernBlink',
'animation-duration': '' + this.options.duration + 'ms',
'animation-iteration-count': '' + this.options.iterationCount
});
} else {
this._fallbackAnimation( this.options.iterationCount );
}
};
/*
* @function stop
* Stops the animation
*/
ModernBlink.prototype.stop = function stop( event ) {
if ( supportsAnimations ) {
return this.el.css({
'animation-name' : '',
'animation-duration' : '',
'animation-iteration-count' : ''
});
}
return this.el.stop( true, true );
};
/*
* @function _fallbackAnimation
* Provides a jQuery Animation fallback for browsers not supporting CSS Animations
*/
ModernBlink.prototype._fallbackAnimation = function _fallbackAnimation( iterationCount ) {
var self = this,
duration = this.options.duration / 2;
if ( iterationCount > 0 || iterationCount === 'infinite' ) {
iterationCount = iterationCount === "infinite" ? "infinite" : iterationCount - 1;
this.el.animate( { 'opacity': 0 }, duration ).promise().done( function() {
self.el.animate( { 'opacity': 1 }, duration );
self._fallbackAnimation( iterationCount );
});
}
};
/*
* @function _bindEventHandlers
* Binds some useful event handlers to the element
*/
ModernBlink.prototype._bindEventHandlers = function _bindEventHandlers() {
this.el.on( 'modernBlink.start', $.proxy( this.start, this ) );
this.el.on( 'modernBlink.stop', $.proxy( this.stop, this ) );
};
/*
* @function modernBlink
* jQuery plugin wrapper around ModernBlink
*
* @param options object
*/
$.fn.modernBlink = function ( options ) {
return this.each( function () {
if ( !$.data( this, "plugin_modernBlink" ) ) {
$.data( this, "plugin_modernBlink", new ModernBlink( this, options ) );
} else {
options = ( options || "" ).replace( /^_/ , "" );
if ( $.isFunction( ModernBlink.prototype[ options ] ) ) {
$.data( this, 'plugin_modernBlink' )[ options ]();
}
}
});
};
})( jQuery, window, document );