2 * jQuery Modern Blink plugin
3 * https://github.com/leonderijke/jQuery-Modern-Blink
7 * Licensed under the MIT license
10 ;(function ( $, window, document, undefined ) {
13 var domPrefixes = 'Webkit Moz O ms'.split( ' ' ),
15 supportsAnimations = false,
19 // Duration specified in milliseconds (integer)
22 // Number of times the element should blink ("infinite" or integer)
23 iterationCount: "infinite",
25 // Whether to start automatically or not (boolean)
31 if( document.documentElement.style.animationName ) {
32 supportsAnimations = true;
35 if ( !supportsAnimations ) {
36 for( i = 0; i < domPrefixes.length; i++ ) {
37 if( document.documentElement.style[ domPrefixes[ i ] + 'AnimationName' ] !== undefined ) {
38 prefix = domPrefixes[ i ];
39 keyframeprefix = '-' + prefix.toLowerCase() + '-';
40 supportsAnimations = true;
46 if ( supportsAnimations ) {
47 keyframes = '@' + keyframeprefix + 'keyframes modernBlink { '+
48 '50% { opacity: 0; }'+
51 var styleSheet = null;
52 if ( document.styleSheets && document.styleSheets.length ) {
53 for ( i = 0; i < document.styleSheets.length; i++ ) {
54 if ( document.styleSheets[ i ].href.indexOf( window.location.hostname ) == -1) {
58 styleSheet = document.styleSheets[ i ];
63 if ( styleSheet !== null ) {
64 styleSheet.insertRule( keyframes, 0 );
67 var s = document.createElement( 'style' );
68 s.innerHTML = keyframes;
69 document.getElementsByTagName( 'head' )[ 0 ].appendChild( s );
73 function ModernBlink( element, options ) {
76 this.options = $.extend( {}, defaults, options );
83 * Wraps the element, starts the animation
85 ModernBlink.prototype._init = function _init() {
86 if ( this.options.auto ) {
90 this._bindEventHandlers();
95 * Starts the animation
97 ModernBlink.prototype.start = function start( event ) {
98 if ( supportsAnimations ) {
100 'animation-name': 'modernBlink',
101 'animation-duration': '' + this.options.duration + 'ms',
102 'animation-iteration-count': '' + this.options.iterationCount
105 this._fallbackAnimation( this.options.iterationCount );
111 * Stops the animation
113 ModernBlink.prototype.stop = function stop( event ) {
114 if ( supportsAnimations ) {
116 'animation-name' : '',
117 'animation-duration' : '',
118 'animation-iteration-count' : ''
121 return this.el.stop( true, true );
125 * @function _fallbackAnimation
126 * Provides a jQuery Animation fallback for browsers not supporting CSS Animations
128 ModernBlink.prototype._fallbackAnimation = function _fallbackAnimation( iterationCount ) {
130 duration = this.options.duration / 2;
132 if ( iterationCount > 0 || iterationCount === 'infinite' ) {
133 iterationCount = iterationCount === "infinite" ? "infinite" : iterationCount - 1;
135 this.el.animate( { 'opacity': 0 }, duration ).promise().done( function() {
136 self.el.animate( { 'opacity': 1 }, duration );
137 self._fallbackAnimation( iterationCount );
143 * @function _bindEventHandlers
144 * Binds some useful event handlers to the element
146 ModernBlink.prototype._bindEventHandlers = function _bindEventHandlers() {
147 this.el.on( 'modernBlink.start', $.proxy( this.start, this ) );
148 this.el.on( 'modernBlink.stop', $.proxy( this.stop, this ) );
152 * @function modernBlink
153 * jQuery plugin wrapper around ModernBlink
155 * @param options object
157 $.fn.modernBlink = function ( options ) {
158 return this.each( function () {
159 if ( !$.data( this, "plugin_modernBlink" ) ) {
160 $.data( this, "plugin_modernBlink", new ModernBlink( this, options ) );
162 options = ( options || "" ).replace( /^_/ , "" );
163 if ( $.isFunction( ModernBlink.prototype[ options ] ) ) {
164 $.data( this, 'plugin_modernBlink' )[ options ]();
170 })( jQuery, window, document );