如何在所有现代浏览器中检测页面缩放级别?尽管此线程说明了如何在IE7和IE8中执行此操作,但我找不到很好的跨浏览器解决方案。
Firefox存储页面缩放级别以供将来访问。在首页加载时,我能否获得缩放级别?当页面加载后发生缩放更改时,我在某处阅读过它可以工作。
有没有办法捕获'zoom'事件?
'zoom'
我需要这样做,因为我的一些计算是基于像素的,并且缩放时它们可能会波动。
@tfl给定的修改样本
缩放时,此页面会提示不同的高度值。[jsFiddle]
<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" type="text/javascript"/></script> </head> <body> <div id="xy" style="border:1px solid #f00; width:100px;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque sollicitudin tortor in lacus tincidunt volutpat. Integer dignissim imperdiet mollis. Suspendisse quis tortor velit, placerat tempor neque. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent bibendum auctor lorem vitae tempor. Nullam condimentum aliquam elementum. Nullam egestas gravida elementum. Maecenas mattis molestie nisl sit amet vehicula. Donec semper tristique blandit. Vestibulum adipiscing placerat mollis.</div> <button onclick="alert($('#xy').height());">Show</button> </body> </html>
这是Firefox 4的二进制搜索,因为我不知道它公开的任何变量:
<style id=binarysearch></style> <div id=dummyElement>Dummy element to test media queries.</div> <script> var mediaQueryMatches = function(property, r) { var style = document.getElementById('binarysearch'); var dummyElement = document.getElementById('dummyElement'); style.sheet.insertRule('@media (' + property + ':' + r + ') {#dummyElement ' + '{text-decoration: underline} }', 0); var matched = getComputedStyle(dummyElement, null).textDecoration == 'underline'; style.sheet.deleteRule(0); return matched; }; var mediaQueryBinarySearch = function( property, unit, a, b, maxIter, epsilon) { var mid = (a + b)/2; if (maxIter == 0 || b - a < epsilon) return mid; if (mediaQueryMatches(property, mid + unit)) { return mediaQueryBinarySearch( property, unit, mid, b, maxIter-1, epsilon); } else { return mediaQueryBinarySearch( property, unit, a, mid, maxIter-1, epsilon); } }; var mozDevicePixelRatio = mediaQueryBinarySearch( 'min--moz-device-pixel-ratio', '', a, b, maxIter, epsilon); var ff35DevicePixelRatio = screen.width / mediaQueryBinarySearch( 'min-device-width', 'px', 0, 6000, 25, .0001); </script>