Есть ли плагин jQuery или способ использования прямого JavaScript для определения размера браузера.
Я бы предпочел, чтобы результаты были "живыми", поэтому, если изменяется ширина или высота, то и результаты.
Есть ли плагин jQuery или способ использования прямого JavaScript для определения размера браузера.
Я бы предпочел, чтобы результаты были "живыми", поэтому, если изменяется ширина или высота, то и результаты.
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
Изменить: Обновлен код JavaScript для поддержки IE8 и более ранних версий.
вы можете использовать
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>
Это должно вернуть видимую область:
document.body.offsetWidth
document.body.offsetHeight
Я думаю, что это всегда равно размеру браузера?
используйте ширину и высоту в любом месте, где угодно... при изменении размера браузера он также изменит значение переменной.
$(window).resize(function() {
width = $(this).width());
height = $(this).height());
});
Вы имеете в виду что-то вроде этого window.innerHeight; window.innerWidth
$(window).height(); $(window).width()
Вы можете попробовать добавить даже прослушиватель при повторном размере, например
window.addEventListener('resize',CheckBrowserSize,false);
function CheckBrowserSize()
{
var ResX= document.body.offsetHeight;
var ResY= document.body.offsetWidth;
}