diff --git a/detect-zoom.js b/detect-zoom.js index 6423868..f5a0a2c 100644 --- a/detect-zoom.js +++ b/detect-zoom.js @@ -29,7 +29,7 @@ * @private */ var devicePixelRatio = function () { - return window.devicePixelRatio || 1; + return Math.round(window.devicePixelRatio * 100) / 100 || 1; }; /** @@ -84,52 +84,29 @@ var zoom = deviceWidth / window.innerWidth; return { zoom: zoom, - devicePxPerCssPx: zoom * devicePixelRatio() + devicePxPerCssPx: devicePixelRatio() }; }; /** * Desktop Webkit - * the trick: an element's clientHeight is in CSS pixels, while you can - * set its line-height in system pixels using font-size and - * -webkit-text-size-adjust:none. - * device-pixel-ratio: http://www.webkit.org/blog/55/high-dpi-web-sites/ - * - * Previous trick (used before http://trac.webkit.org/changeset/100847): - * documentElement.scrollWidth is in CSS pixels, while - * document.width was in system pixels. Note that this is the - * layout width of the document, which is slightly different from viewport - * because document width does not include scrollbars and might be wider - * due to big elements. + * Create SVG, detect current scale ratio, remove SVG. + * devicePixelRatio is affected by the zoom level, + * so we can't tell, if we are in zoom mode or in a device + * with a different pixel ratio * @return {Object} * @private */ var webkit = function () { - var important = function (str) { - return str.replace(/;/g, " !important;"); - }; - - var div = document.createElement('div'); - div.innerHTML = "1
2
3
4
5
6
7
8
9
0"; - div.setAttribute('style', important('font: 100px/1em sans-serif; -webkit-text-size-adjust: none; text-size-adjust: none; height: auto; width: 1em; padding: 0; overflow: visible;')); - - // The container exists so that the div will be laid out in its own flow - // while not impacting the layout, viewport size, or display of the - // webpage as a whole. - // Add !important and relevant CSS rule resets - // so that other rules cannot affect the results. - var container = document.createElement('div'); - container.setAttribute('style', important('width:0; height:0; overflow:hidden; visibility:hidden; position: absolute;')); - container.appendChild(div); - - document.body.appendChild(container); - var zoom = 1000 / div.clientHeight; - zoom = Math.round(zoom * 100) / 100; - document.body.removeChild(container); - + var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); + svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg'); + svg.setAttribute('version', '1.1'); + document.body.appendChild(svg); + var zoom = Math.round(svg.currentScale * 100) / 100; + document.body.removeChild(svg); return{ zoom: zoom, - devicePxPerCssPx: zoom * devicePixelRatio() + devicePxPerCssPx: devicePixelRatio() }; }; @@ -194,6 +171,7 @@ * @param maxIter * @param epsilon * @return {Number} + * @private */ var mediaQueryBinarySearch = function (property, unit, a, b, maxIter, epsilon) { var matchMedia; @@ -240,9 +218,10 @@ /** * Generate detection function + * @return {Function} * @private */ - var detectFunction = (function () { + var detectFunction = function () { var func = fallback; //IE8+ if (!isNaN(screen.logicalXDPI) && !isNaN(screen.systemXDPI)) { @@ -253,11 +232,11 @@ func = ie10; } //Mobile Webkit - else if ('orientation' in window && typeof document.body.style.webkitMarquee === 'string') { + else if ('orientation' in window && typeof document.body.style.webkitAnimation === 'string') { func = webkitMobile; } //WebKit - else if (typeof document.body.style.webkitMarquee === 'string') { + else if (typeof document.body.style.webkitAnimation === 'string') { func = webkit; } //Opera @@ -275,6 +254,26 @@ } return func; + }; + + /** + * Cached detectFunction to prevent double calls + */ + var cachedDetectFunction; + + /** + * Script tag for detect-zoom.js can now be included in head + * or before the closing body tag. + * @return {Function} + * @private + */ + var detect = (function () { + return document.body ? detectFunction() : function () { + if (typeof cachedDetectFunction === 'undefined') { + cachedDetectFunction = detectFunction(); + } + return cachedDetectFunction(); + } }()); @@ -285,7 +284,7 @@ * @return {Number} Zoom level */ zoom: function () { - return detectFunction().zoom; + return detect().zoom; }, /** @@ -293,7 +292,7 @@ * @return {Number} devicePxPerCssPx level */ device: function () { - return detectFunction().devicePxPerCssPx; + return detect().devicePxPerCssPx; } }); })); diff --git a/detect-zoom.min.js b/detect-zoom.min.js index 881f7ea..ab1712b 100644 --- a/detect-zoom.min.js +++ b/detect-zoom.min.js @@ -1 +1 @@ -(function(root,ns,factory){"use strict";"undefined"!=typeof module&&module.exports?module.exports=factory(ns,root):"function"==typeof define&&define.amd?define("detect-zoom",function(){return factory(ns,root)}):root[ns]=factory(ns,root)})(window,"detectZoom",function(){var devicePixelRatio=function(){return window.devicePixelRatio||1},fallback=function(){return{zoom:1,devicePxPerCssPx:1}},ie8=function(){var zoom=Math.round(100*(screen.deviceXDPI/screen.logicalXDPI))/100;return{zoom:zoom,devicePxPerCssPx:zoom*devicePixelRatio()}},ie10=function(){var zoom=Math.round(100*(document.documentElement.offsetHeight/window.innerHeight))/100;return{zoom:zoom,devicePxPerCssPx:zoom*devicePixelRatio()}},webkitMobile=function(){var deviceWidth=90==Math.abs(window.orientation)?screen.height:screen.width,zoom=deviceWidth/window.innerWidth;return{zoom:zoom,devicePxPerCssPx:zoom*devicePixelRatio()}},webkit=function(){var important=function(str){return str.replace(/;/g," !important;")},div=document.createElement("div");div.innerHTML="1
2
3
4
5
6
7
8
9
0",div.setAttribute("style",important("font: 100px/1em sans-serif; -webkit-text-size-adjust: none; text-size-adjust: none; height: auto; width: 1em; padding: 0; overflow: visible;"));var container=document.createElement("div");container.setAttribute("style",important("width:0; height:0; overflow:hidden; visibility:hidden; position: absolute;")),container.appendChild(div),document.body.appendChild(container);var zoom=1e3/div.clientHeight;return zoom=Math.round(100*zoom)/100,document.body.removeChild(container),{zoom:zoom,devicePxPerCssPx:zoom*devicePixelRatio()}},firefox4=function(){var zoom=mediaQueryBinarySearch("min--moz-device-pixel-ratio","",0,10,20,1e-4);return zoom=Math.round(100*zoom)/100,{zoom:zoom,devicePxPerCssPx:zoom}},firefox18=function(){return{zoom:firefox4().zoom,devicePxPerCssPx:devicePixelRatio()}},opera11=function(){var zoom=window.top.outerWidth/window.top.innerWidth;return zoom=Math.round(100*zoom)/100,{zoom:zoom,devicePxPerCssPx:zoom*devicePixelRatio()}},mediaQueryBinarySearch=function(property,unit,a,b,maxIter,epsilon){function binarySearch(a,b,maxIter){var mid=(a+b)/2;if(0>=maxIter||epsilon>b-a)return mid;var query="("+property+":"+mid+unit+")";return matchMedia(query).matches?binarySearch(mid,b,maxIter-1):binarySearch(a,mid,maxIter-1)}var matchMedia,head,style,div;window.matchMedia?matchMedia=window.matchMedia:(head=document.getElementsByTagName("head")[0],style=document.createElement("style"),head.appendChild(style),div=document.createElement("div"),div.className="mediaQueryBinarySearch",div.style.display="none",document.body.appendChild(div),matchMedia=function(query){style.sheet.insertRule("@media "+query+"{.mediaQueryBinarySearch "+"{text-decoration: underline} }",0);var matched="underline"==getComputedStyle(div,null).textDecoration;return style.sheet.deleteRule(0),{matches:matched}});var ratio=binarySearch(a,b,maxIter);return div&&(head.removeChild(style),document.body.removeChild(div)),ratio},detectFunction=function(){var func=fallback;return isNaN(screen.logicalXDPI)||isNaN(screen.systemXDPI)?window.navigator.msMaxTouchPoints?func=ie10:"orientation"in window&&"string"==typeof document.body.style.webkitMarquee?func=webkitMobile:"string"==typeof document.body.style.webkitMarquee?func=webkit:navigator.userAgent.indexOf("Opera")>=0?func=opera11:window.devicePixelRatio?func=firefox18:firefox4().zoom>.001&&(func=firefox4):func=ie8,func}();return{zoom:function(){return detectFunction().zoom},device:function(){return detectFunction().devicePxPerCssPx}}}); \ No newline at end of file +!function(root,ns,factory){"use strict";"undefined"!=typeof module&&module.exports?module.exports=factory(ns,root):"function"==typeof define&&define.amd?define("detect-zoom",function(){return factory(ns,root)}):root[ns]=factory(ns,root)}(window,"detectZoom",function(){var cachedDetectFunction,devicePixelRatio=function(){return Math.round(100*window.devicePixelRatio)/100||1},fallback=function(){return{zoom:1,devicePxPerCssPx:1}},ie8=function(){var zoom=Math.round(100*(screen.deviceXDPI/screen.logicalXDPI))/100;return{zoom:zoom,devicePxPerCssPx:zoom*devicePixelRatio()}},ie10=function(){var zoom=Math.round(100*(document.documentElement.offsetHeight/window.innerHeight))/100;return{zoom:zoom,devicePxPerCssPx:zoom*devicePixelRatio()}},webkitMobile=function(){var deviceWidth=90==Math.abs(window.orientation)?screen.height:screen.width,zoom=deviceWidth/window.innerWidth;return{zoom:zoom,devicePxPerCssPx:devicePixelRatio()}},webkit=function(){var svg=document.createElementNS("http://www.w3.org/2000/svg","svg");svg.setAttribute("xmlns","http://www.w3.org/2000/svg"),svg.setAttribute("version","1.1"),document.body.appendChild(svg);var zoom=Math.round(100*svg.currentScale)/100;return document.body.removeChild(svg),{zoom:zoom,devicePxPerCssPx:devicePixelRatio()}},firefox4=function(){var zoom=mediaQueryBinarySearch("min--moz-device-pixel-ratio","",0,10,20,1e-4);return zoom=Math.round(100*zoom)/100,{zoom:zoom,devicePxPerCssPx:zoom}},firefox18=function(){return{zoom:firefox4().zoom,devicePxPerCssPx:devicePixelRatio()}},opera11=function(){var zoom=window.top.outerWidth/window.top.innerWidth;return zoom=Math.round(100*zoom)/100,{zoom:zoom,devicePxPerCssPx:zoom*devicePixelRatio()}},mediaQueryBinarySearch=function(property,unit,a,b,maxIter,epsilon){function binarySearch(a,b,maxIter){var mid=(a+b)/2;if(0>=maxIter||epsilon>b-a)return mid;var query="("+property+":"+mid+unit+")";return matchMedia(query).matches?binarySearch(mid,b,maxIter-1):binarySearch(a,mid,maxIter-1)}var matchMedia,head,style,div;window.matchMedia?matchMedia=window.matchMedia:(head=document.getElementsByTagName("head")[0],style=document.createElement("style"),head.appendChild(style),div=document.createElement("div"),div.className="mediaQueryBinarySearch",div.style.display="none",document.body.appendChild(div),matchMedia=function(query){style.sheet.insertRule("@media "+query+"{.mediaQueryBinarySearch "+"{text-decoration: underline} }",0);var matched="underline"==getComputedStyle(div,null).textDecoration;return style.sheet.deleteRule(0),{matches:matched}});var ratio=binarySearch(a,b,maxIter);return div&&(head.removeChild(style),document.body.removeChild(div)),ratio},detectFunction=function(){var func=fallback;return isNaN(screen.logicalXDPI)||isNaN(screen.systemXDPI)?window.navigator.msMaxTouchPoints?func=ie10:"orientation"in window&&"string"==typeof document.body.style.webkitAnimation?func=webkitMobile:"string"==typeof document.body.style.webkitAnimation?func=webkit:navigator.userAgent.indexOf("Opera")>=0?func=opera11:window.devicePixelRatio?func=firefox18:firefox4().zoom>.001&&(func=firefox4):func=ie8,func},detect=function(){return document.body?detectFunction():function(){return"undefined"==typeof cachedDetectFunction&&(cachedDetectFunction=detectFunction()),cachedDetectFunction()}}();return{zoom:function(){return detect().zoom},device:function(){return detect().devicePxPerCssPx}}}); \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..cab739c --- /dev/null +++ b/index.html @@ -0,0 +1,18 @@ + + + + + + + + + +
+
+ + \ No newline at end of file