有没有jQuery插件或者直接使用JavaScript来检测浏览器大小的方法?
我更喜欢结果是“实时”的,所以如果宽度或高度改变了,结果也会改变。
发布于 2012-10-08 20:37:27
JavaScript
function jsUpdateSize(){
// Get the dimensions of the viewport
var width = window.innerWidth ||
document.documentElement.clientWidth ||
document.body.clientWidth;
var height = window.innerHeight ||
document.documentElement.clientHeight ||
document.body.clientHeight;
document.getElementById('jsWidth').innerHTML = width; // Display the width
document.getElementById('jsHeight').innerHTML = height;// Display the height
};
window.onload = jsUpdateSize; // When the page first loads
window.onresize = jsUpdateSize; // When the browser changes size
jQuery
function jqUpdateSize(){
// Get the dimensions of the viewport
var width = $(window).width();
var height = $(window).height();
$('#jqWidth').html(width); // Display the width
$('#jqHeight').html(height); // Display the height
};
$(document).ready(jqUpdateSize); // When the page first loads
$(window).resize(jqUpdateSize); // When the browser changes size
jsfiddle demo
编辑:更新了JavaScript代码以支持IE8和更早版本。
发布于 2012-10-08 19:56:11
您可以使用
function onresize (){
var h = $(window).height(), w= $(window).width();
$('#resultboxid').html('height= ' + h + ' width: ' w);
}
$(window).resize(onresize );
onresize ();// first time;
html:
<span id=resultboxid></span>
发布于 2012-10-08 19:55:21
这应该返回可见区域:
document.body.offsetWidth
document.body.offsetHeight
我猜这总是等于浏览器的大小?
https://stackoverflow.com/questions/12781205
复制相似问题