2009-03-25 28 views
13

Trong Internet Explorer 7 một số thuộc tính (tọa độ chuột) được coi là vật lý trong khi các thuộc tính khác là logic (offset). Điều này chủ yếu yêu cầu các nhà phát triển web phải nhận thức hoặc tính toán trạng thái zoom. Trong IE8, tất cả các thuộc tính đều hợp lý.Cách lấy mức thu phóng trong Internet Explorer 7? (javascript)

+0

Xem thêm http://stackoverflow.com/questions/1713771/how-to-detect-page-zoom-level-in-all-modern-browsers – ripper234

Trả lời

10

Bạn có thể lấy nó bằng cách sử:

var b = document.body.getBoundingClientRect();  
alert((b.right - b.left)/document.body.clientWidth); 

Thanks a lot @niclasnorgren!

8

Ngoài ra, nếu bạn cần kiểm tra trong IE 8, bạn có thể sử dụng window.screen.deviceXDPI và window.screen.deviceYDPI. Giá trị mặc định là 96 dpi và nếu bạn được phóng to, số sẽ lớn hơn (còn 144 khi được phóng to 150%)

+0

cảm ơn, đó chỉ là những gì tôi cần để tìm "tỷ lệ thu phóng" :-) – naivists

+2

window.screen.deviceXDPI hoạt động đáng tin cậy trong các chế độ mô phỏng của IE9 cho IE8, IE7. Phương thức getBoundingClientRect() trả về 100% trong tất cả các chế độ thu phóng. – ddotsenko

+0

Lưu ý rằng screen.deviceXDPI chỉ dành cho IE chứ không phải trong Chrome. –

4

Có lỗi cú pháp nhỏ (thân thay vì document.body) trên câu trả lời được chấp nhận. Điều này dường như làm các trick cũng có.

var rect = document.body.getBoundingClientRect(); 
var zoomLevel = Math.round((rect.right-rect.left)/document.body.clientWidth * 100); 
0

tôi đã đăng giải pháp cho bài đăng này trên một bài đăng khác mà bạn có thể tải tại đây. cũng sẽ hoạt động trong IE7.

Auto-detect a screen resolution and change browser zoom with Javascript?

This will help to detect browser zoom tested on all browser 
<script> 
window.utility = function(utility){ 
utility.screen = { 
    rtime : new Date(1, 1, 2000, 12,00,00), 
    timeout : false, 
    delta : 200 
}; 
utility.getBrowser = function(){ 
    var $b = $.browser; 
    $.extend(utility.screen,$.browser); 
    utility.screen.isZoomed = false; 
    var screen = utility.screen; 
    screen.zoomf = screen.zoom = 1; 
    screen.width = window.screen.width; 
    screen.height = window.screen.height; 
    if($b.mozilla){ //FOR MOZILLA 
     screen.isZoomed = window.matchMedia('(max--moz-device-pixel-ratio:0.99), (min--moz-device-pixel-ratio:1.01)').matches; 
    } else { 
     if($b.chrome){ //FOR CHROME 
      screen.zoom = (window.outerWidth - 8)/window.innerWidth; 
      screen.isZoomed = (screen.zoom < .98 || screen.zoom > 1.02) 
     } else if($b.msie){//FOR IE7,IE8,IE9 
      var _screen = document.frames.screen; 
      screen.zoom = ((((_screen.deviceXDPI/_screen.systemXDPI) * 100 + 0.9).toFixed())/100); 
      screen.isZoomed = (screen.zoom < .98 || screen.zoom > 1.02); 
      if(screen.isZoomed) screen.zoomf = screen.zoom; 
      screen.width = window.screen.width*screen.zoomf; 
      screen.height = window.screen.height*screen.zoomf; 
     } 
    } 
    return utility.screen; 
}; 
    window.onresize = function(e){ 
     utility.screen.rtime = new Date(); 
     if (utility.screen.timeout === false) { 
       utility.screen.timeout = true; 
       setTimeout(window.resizeend, utility.screen.delta); 
     } 
    }; 
window.resizeend = function() { 
    if (new Date() - utility.screen.rtime < utility.screen.delta) { 
     setTimeout(window.resizeend, utility.screen.delta); 
    } else { 
     utility.screen.timeout = false; 
     utility.screen = utility.getBrowser(); 
     if(window.onresizeend) window.onresizeend (utility.screen); 
     if(utility.onResize) utility.onResize(utility.screen); 
    }    
}; 
window.onresizeend = function(screen){ 
    if(screen.isZoomed) 
     $('body').text('zoom is not 100%'); 
    else{ 
     $('body').text('zoom is 100% & browser resolution is'+[screen.width+'X'+screen.height]); 
    } 
}; 
$(document).ready(function(){ 
    window.onresize(); 
}); 
return utility; 
}({}); 
</script> 

Demo

Các vấn đề liên quan