From 8a05acb913cf4f56582acd40c35ffee21b7290aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20=C4=90akonovi=C4=87?= Date: Sun, 31 Jan 2016 12:59:22 +0100 Subject: [PATCH] Partial resolution of #53 - IE 10 & 11 now recognizes jQuery.Keyframes responsive keyframes API, but doesn't fully mimic 'normal' browsers behavior on resize. Various code optimizations. --- jquery.keyframes.js | 95 ++++++++++++++++++++++++++++++++--------- jquery.keyframes.min.js | 4 +- 2 files changed, 79 insertions(+), 20 deletions(-) diff --git a/jquery.keyframes.js b/jquery.keyframes.js index 00beb83..58076cf 100644 --- a/jquery.keyframes.js +++ b/jquery.keyframes.js @@ -1,11 +1,17 @@ -(function() { +(function(win, doc, $) { var animationSupport = false, - animationString = 'animation', - vendorPrefix = prefix = '', - domPrefixes = ['Webkit', 'Moz', 'O', 'ms', 'Khtml']; - - $(window).load(function(){ - var style = document.body.style; + animationString = 'animation', + vendorPrefix = prefix = '', + domPrefixes = ['Webkit', 'Moz', 'O', 'ms', 'Khtml'], + //match ie 10 & 11 exclusively + ie1011 = (win.matchMedia && win.matchMedia('all and (-ms-high-contrast: none), (-ms-high-contrast: active)').matches), + $win = $(win), + head = doc.getElementsByTagName('head')[0] + + + // $.fn.load is deprecated, https://api.jquery.com/load-event/, use '.on' instead + $win.on('load', function(){ + var style = doc.body.style; if( style.animationName !== undefined ) { animationSupport = true; } if( animationSupport === false ) { @@ -20,15 +26,57 @@ } } }); - - - var $createKeyframeStyleTag = function(id, css) { - if($.keyframe.debug){ console.log(id + " " + css); } + var + //update style tags with data-media attr + ie1011iterateStyles = function(){ + $.each( $('[data-media]'), function(index,elem){ + $el = $(elem); + if( win.matchMedia($el.attr('data-media')).matches ){ + $el.html($el.data('keyframedata'));//set keyframe that should apply according to media query + } else { + $el.data('keyframedata', $el.html() ); + $el.html('');//take back keyframe that should not apply according to media query + } + }); + }, + + // debounce timeout + dbnTout, + // debounce ie1011iterateStyles function + ie1011iterateStylesProxy = function(){ + win.clearTimeout(dbnTout); + dbnTout = win.setTimeout( ie1011iterateStyles, 34 );// wait approx two frames + }, + + $createKeyframeStyleTag = function(id, css, ie1011Media) { + if($.keyframe.debug){ win.console.log(id + " " + css); } + + // issue #53 + if(ie1011 && ie1011Media){ + var $style = $('').attr({ + "class": "keyframe-style", + id: id, + type: "text/css", + 'data-media' : ie1011Media + }); + + if(win.matchMedia(ie1011Media).matches){ + $style.html(css); + } else { + $style.data('keyframedata', css); + } + + $win.on('resize', ie1011iterateStylesProxy) + .on('orientationchange', ie1011iterateStylesProxy); + + return $style.appendTo(head); + } + return $("").attr({ "class": "keyframe-style", id: id, type: "text/css" - }).appendTo("head"); + }).appendTo(head); }; $.keyframe = { @@ -54,11 +102,13 @@ css += "}"; } } - if(window.PrefixFree) - css = PrefixFree.prefixCSS(css + "}"); + if(win.PrefixFree) + css = win.PrefixFree.prefixCSS(css + "}"); else css += "}"; - if(frameData.media){ + + // issue #53: if it's is responsive @keyframe, but NOT ie 10/11 + if(frameData.media && !ie1011 ){ css = "@media " + frameData.media + "{" + css + "}"; } @@ -66,7 +116,8 @@ if ($frameStyle.length > 0) { $frameStyle.html(css); - + // this needs HUGE optimization, maybe to add specific class to animation targets, so + // we can retrieve them easely with .classname selector var $elems = $("*").filter(function() { return this.style[animationString + "Name"] === frameName; }); @@ -79,7 +130,12 @@ }); }); } else { - $createKeyframeStyleTag(frameName, css); + // issue #53. pass frameData.media as third argument to @keyframe style tag creation mechanism... + if(ie1011 && frameData.media ){ + $createKeyframeStyleTag(frameName, css, frameData.media); + } else { + $createKeyframeStyleTag(frameName, css); + } } }, define: function(frameData) { @@ -101,7 +157,7 @@ var $el = $(this).css(vendorPrefix + animationPlayState, playStateRunning).css(vendorPrefix + "animation", "none"); if (callback) { - setTimeout(callback, 1); + win.setTimeout(callback, 1); } }; @@ -180,6 +236,7 @@ return this; }; + $createKeyframeStyleTag("boost-keyframe", " .boostKeyframe{" + vendorPrefix + "transform:scale3d(1,1,1);}"); -}).call(this); +})(window,document,jQuery); diff --git a/jquery.keyframes.min.js b/jquery.keyframes.min.js index 96a6ea0..eb8e3a2 100644 --- a/jquery.keyframes.min.js +++ b/jquery.keyframes.min.js @@ -5,4 +5,6 @@ */ (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||"",a="@"+t+"keyframes "+i+" {";for(var r in e)if("name"!==r&&"media"!==r&&"complete"!==r){a+=r+" {";for(var s in e[r])a+=s+":"+e[r][s]+";";a+="}"}window.PrefixFree?a=PrefixFree.prefixCSS(a+"}"):a+="}",e.media&&(a="@media "+e.media+"{"+a+"}");var f=$("style#"+e.name);if(f.length>0){f.html(a);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 o(i,a)},define:function(e){if(e.length)for(var n=0;n").attr({"class":"keyframe-style",id:t,type:"text/css","data-media":i});return e.matchMedia(i).matches?o.html(n):o.data("keyframedata",n),f.on("resize",c).on("orientationchange",c),o.appendTo(m)}return a("").attr({"class":"keyframe-style",id:t,type:"text/css"}).appendTo(m)};a.keyframe={debug:!1,getVendorPrefix:function(){return o},isSupported:function(){return n},generate:function(t){var n=t.name||"",r="@"+o+"keyframes "+n+" {";for(var f in t)if("name"!==f&&"media"!==f&&"complete"!==f){r+=f+" {";for(var m in t[f])r+=m+":"+t[f][m]+";";r+="}"}e.PrefixFree?r=e.PrefixFree.prefixCSS(r+"}"):r+="}",t.media&&!s&&(r="@media "+t.media+"{"+r+"}");var l=a("style#"+t.name);if(l.length>0){l.html(r);var d=a("*").filter(function(){return this.style[i+"Name"]===n});d.each(function(){var e=a(this),t=e.data("keyframeOptions");e.resetKeyframe(function(){e.playKeyframe(t)})})}else s&&t.media?u(n,r,t.media):u(n,r)},define:function(e){if(e.length)for(var t=0;t