diff --git a/README.md b/README.md index 52aaaea..e9042de 100644 --- a/README.md +++ b/README.md @@ -35,11 +35,6 @@ Include script in your document using the following line: Be sure to define and play animations after the page has loaded using jquerys $(window).load -Vendor Prefixing ----------------- -[Prefix Free](http://leaverou.github.io/prefixfree/) is used on the generated css to automatically add vendor prefixes. This means you should avoid adding any vendor prefixes to your defined css. -For convenience, it is bundled into the minified version. - Usage ------------- @@ -49,11 +44,6 @@ Usage var supportedFlag = $.keyframe.isSupported(); ``` -**Enable debugging to the console** -```javascript -$.keyframe.debug = true; -``` - **Adding a new animation sequence (keyframe)** ```javascript @@ -222,8 +212,3 @@ Who is using jQuery.Keyframes? Plugins! -------- See other plugins that allow for spritesheets & more complex movement paths: https://github.com/jQueryKeyframes - -Changelog ---------- -**0.0.9** -* Add debug output diff --git a/jquery.keyframes.js b/jquery.keyframes.js index 8aaaa65..b2f09cc 100644 --- a/jquery.keyframes.js +++ b/jquery.keyframes.js @@ -1,47 +1,23 @@ (function() { - var animationSupport = false, - animationString = 'animation', - vendorPrefix = prefix = '', - domPrefixes = ['Webkit', 'Moz', 'O', 'ms', 'Khtml']; - - $(window).load(function(){ - var style = document.body.style; - if( style.animationName !== undefined ) { animationSupport = true; } - - if( animationSupport === false ) { - for( var i = 0; i < domPrefixes.length; i++ ) { - if( style[ domPrefixes[i] + 'AnimationName' ] !== undefined ) { - prefix = domPrefixes[ i ]; - animationString = prefix + 'Animation'; - vendorPrefix = '-' + prefix.toLowerCase() + '-'; - animationSupport = true; - break; - } - } - } - }); + $.keyframe = { - var $createKeyframeStyleTag = function(id, css) { - if($.keyframe.debug){ console.log(id + " " + css); } - return $("").attr({ - "class": "keyframe-style", - id: id, - type: "text/css" - }).appendTo("head"); - }; + count: 0, - $.keyframe = { - debug: false, - getVendorPrefix: function() { - return vendorPrefix; - }, isSupported: function() { - return animationSupport; + return document.body.style.animationName !== undefined; }, + generate: function(frameData) { - var frameName = frameData.name || ""; - var css = "@" + vendorPrefix + "keyframes " + frameName + " {"; + $.keyframe.count++; + + var frameName, frameId = "keyframe_" + $.keyframe.count; + if(frameData.name){ + frameName = frameData.name; + frameId = "keyframe_" + frameName; + } + + var css = "@keyframes " + frameName + " {"; for (var key in frameData) { if (key !== "name" && key !== "media" && key !== "complete") { @@ -55,19 +31,19 @@ } } - css = PrefixFree.prefixCSS(css + "}"); + css += "}"; if(frameData.media){ css = "@media " + frameData.media + "{" + css + "}"; } - var $frameStyle = $("style#" + frameData.name); + var $frameStyle = $("style#" + frameId); if ($frameStyle.length > 0) { $frameStyle.append(css); - var $elems = $("*").filter(function() { - return this.style[animationString + "Name"] === frameName; + var $elems = $("[style]").filter(function() { + return this.style["animationName"] === frameName; }); $elems.each(function() { @@ -78,9 +54,14 @@ }); }); } else { - $createKeyframeStyleTag(frameName, css); + $("").attr({ + "class": "keyframe-style", + id: frameId, + type: "text/css" + }).appendTo("head"); } }, + define: function(frameData) { if (frameData.length) { for (var i = 0; i < frameData.length; i++) { @@ -90,26 +71,26 @@ } else { this.generate(frameData); } + }, + + setPlayState: function($elem, running){ + return $elem.css("animation-play-state", (running ? 'running' : 'paused')) } }; - var animationPlayState = "animation-play-state"; - var playStateRunning = "running"; - $.fn.resetKeyframe = function(callback) { - var $el = $(this).css(vendorPrefix + animationPlayState, playStateRunning).css(vendorPrefix + "animation", "none"); - if (callback) { - setTimeout(callback, 1); + setTimeout(callback, 0); } + return $.keyframe.setPlayState($(this), true).css("animation", "none"); }; $.fn.pauseKeyframe = function() { - $(this).css(vendorPrefix + animationPlayState, "paused"); + $.keyframe.setPlayState($(this), false); }; $.fn.resumeKeyframe = function() { - $(this).css(vendorPrefix + animationPlayState, playStateRunning); + $.keyframe.setPlayState($(this), true); }; $.fn.playKeyframe = function(frameOptions, callback) { @@ -128,57 +109,33 @@ var animationcss = ""; - if($.isArray(frameOptions)){ + if($.type(frameOptions) == "array"){ var frameOptionsStrings = []; for(var i = 0; i < frameOptions.length; i++){ - if (typeof frameOptions[i] === 'string') { + if ($.type(frameOptions[i]) == 'string') { frameOptionsStrings.push(frameOptions[i]); }else{ frameOptionsStrings.push(animObjToStr(frameOptions[i])); } } animationcss = frameOptionsStrings.join(", "); - }else if (typeof frameOptions === 'string') { + }else if ($.type(frameOptions) == 'string') { animationcss = frameOptions; }else{ animationcss = animObjToStr(frameOptions); } - var animationkey = vendorPrefix + "animation"; - var pfx = ["webkit", "moz", "MS", "o", ""]; - if(!callback && frameOptions.complete){ callback = frameOptions.complete; } - var _prefixEvent = function(element, type, callback) { - for(var i = 0; i < pfx.length; i++){ - if (!pfx[i]) { - type = type.toLowerCase(); - } - var evt = pfx[i] + type; - element.off(evt).on(evt, callback); - } - }; - this.each(function() { - var $el = $(this).addClass("boostKeyframe").css(vendorPrefix + animationPlayState, playStateRunning).css(animationkey, animationcss).data("keyframeOptions", frameOptions); - if($.keyframe.debug){ - console.group(); - if(vendorPrefix){ console.log("Vendor Prefix: " + vendorPrefix); } - console.log("Style Applied: " + animationcss); - var testCss = $el.css(animationkey); - console.log("Rendered Style: " + (testCss ? testCss : $el[0].style.animation)); - console.groupEnd(); - } + var $el = $(this).resetKeyframe().css('animation', animationcss).data("keyframeOptions", frameOptions); if (callback) { - _prefixEvent($el, 'AnimationIteration', callback); - _prefixEvent($el, 'AnimationEnd', callback); + $el.off('AnimationIteration AnimationEnd').on('AnimationIteration AnimationEnd', callback); } }); return this; }; - $createKeyframeStyleTag("boost-keyframe", " .boostKeyframe{" + vendorPrefix + "transform:scale3d(1,1,1);}"); - }).call(this); diff --git a/jquery.keyframes.min.js b/jquery.keyframes.min.js deleted file mode 100644 index 7ccb755..0000000 --- a/jquery.keyframes.min.js +++ /dev/null @@ -1,8 +0,0 @@ -/** - * StyleFix 1.0.3 & PrefixFree 1.0.7 - * @author Lea Verou - * MIT license - */ -(function(){function t(e,t){return[].slice.call((t||document).querySelectorAll(e))}if(!window.addEventListener)return;var e=window.StyleFix={link:function(t){try{if(t.rel!=="stylesheet"||t.hasAttribute("data-noprefix"))return}catch(n){return}var r=t.href||t.getAttribute("data-href"),i=r.replace(/[^\/]+$/,""),s=(/^[a-z]{3,10}:/.exec(i)||[""])[0],o=(/^[a-z]{3,10}:\/\/[^\/]+/.exec(i)||[""])[0],u=/^([^?]*)\??/.exec(r)[1],a=t.parentNode,f=new XMLHttpRequest,l;f.onreadystatechange=function(){f.readyState===4&&l()};l=function(){var n=f.responseText;if(n&&t.parentNode&&(!f.status||f.status<400||f.status>600)){n=e.fix(n,!0,t);if(i){n=n.replace(/url\(\s*?((?:"|')?)(.+?)\1\s*?\)/gi,function(e,t,n){return/^([a-z]{3,10}:|#)/i.test(n)?e:/^\/\//.test(n)?'url("'+s+n+'")':/^\//.test(n)?'url("'+o+n+'")':/^\?/.test(n)?'url("'+u+n+'")':'url("'+i+n+'")'});var r=i.replace(/([\\\^\$*+[\]?{}.=!:(|)])/g,"\\$1");n=n.replace(RegExp("\\b(behavior:\\s*?url\\('?\"?)"+r,"gi"),"$1")}var l=document.createElement("style");l.textContent=n;l.media=t.media;l.disabled=t.disabled;l.setAttribute("data-href",t.getAttribute("href"));a.insertBefore(l,t);a.removeChild(t);l.media=t.media}};try{f.open("GET",r);f.send(null)}catch(n){if(typeof XDomainRequest!="undefined"){f=new XDomainRequest;f.onerror=f.onprogress=function(){};f.onload=l;f.open("GET",r);f.send(null)}}t.setAttribute("data-inprogress","")},styleElement:function(t){if(t.hasAttribute("data-noprefix"))return;var n=t.disabled;t.textContent=e.fix(t.textContent,!0,t);t.disabled=n},styleAttribute:function(t){var n=t.getAttribute("style");n=e.fix(n,!1,t);t.setAttribute("style",n)},process:function(){t('link[rel="stylesheet"]:not([data-inprogress])').forEach(StyleFix.link);t("style").forEach(StyleFix.styleElement);t("[style]").forEach(StyleFix.styleAttribute)},register:function(t,n){(e.fixers=e.fixers||[]).splice(n===undefined?e.fixers.length:n,0,t)},fix:function(t,n,r){for(var i=0;i-1&&(e=e.replace(/(\s|:|,)(repeating-)?linear-gradient\(\s*(-?\d*\.?\d*)deg/ig,function(e,t,n,r){return t+(n||"")+"linear-gradient("+(90-r)+"deg"}));e=t("functions","(\\s|:|,)","\\s*\\(","$1"+s+"$2(",e);e=t("keywords","(\\s|:)","(\\s|;|\\}|$)","$1"+s+"$2$3",e);e=t("properties","(^|\\{|\\s|;)","\\s*:","$1"+s+"$2:",e);if(n.properties.length){var o=RegExp("\\b("+n.properties.join("|")+")(?!:)","gi");e=t("valueProperties","\\b",":(.+?);",function(e){return e.replace(o,s+"$1")},e)}if(r){e=t("selectors","","\\b",n.prefixSelector,e);e=t("atrules","@","\\b","@"+s+"$1",e)}e=e.replace(RegExp("-"+s,"g"),"-");e=e.replace(/-\*-(?=[a-z]+)/gi,n.prefix);return e},property:function(e){return(n.properties.indexOf(e)>=0?n.prefix:"")+e},value:function(e,r){e=t("functions","(^|\\s|,)","\\s*\\(","$1"+n.prefix+"$2(",e);e=t("keywords","(^|\\s)","(\\s|$)","$1"+n.prefix+"$2$3",e);n.valueProperties.indexOf(r)>=0&&(e=t("properties","(^|\\s|,)","($|\\s|,)","$1"+n.prefix+"$2$3",e));return e},prefixSelector:function(e){return e.replace(/^:{1,2}/,function(e){return e+n.prefix})},prefixProperty:function(e,t){var r=n.prefix+e;return t?StyleFix.camelCase(r):r}};(function(){var e={},t=[],r={},i=getComputedStyle(document.documentElement,null),s=document.createElement("div").style,o=function(n){if(n.charAt(0)==="-"){t.push(n);var r=n.split("-"),i=r[1];e[i]=++e[i]||1;while(r.length>3){r.pop();var s=r.join("-");u(s)&&t.indexOf(s)===-1&&t.push(s)}}},u=function(e){return StyleFix.camelCase(e)in s};if(i.length>0)for(var a=0;a"+n+"").attr({"class":"keyframe-style",id:e,type:"text/css"}).appendTo("head")};$.keyframe={debug:!1,getVendorPrefix:function(){return t},isSupported:function(){return e},generate:function(e){var i=e.name||"",o="@"+t+"keyframes "+i+" {";for(var r in e)if("name"!==r&&"media"!==r&&"complete"!==r){o+=r+" {";for(var s in e[r])o+=s+":"+e[r][s]+";";o+="}"}o=PrefixFree.prefixCSS(o+"}"),e.media&&(o="@media "+e.media+"{"+o+"}");var f=$("style#"+e.name);if(f.length>0){f.append(o);var l=$("*").filter(function(){return this.style[n+"Name"]===i});l.each(function(){var e=$(this),n=e.data("keyframeOptions");e.resetKeyframe(function(){e.playKeyframe(n)})})}else a(i,o)},define:function(e){if(e.length)for(var n=0;n