gpt4 book ai didi

javascript - 获取上一个和当前的窗口宽度

转载 作者:搜寻专家 更新时间:2023-11-01 04:58:00 26 4
gpt4 key购买 nike

我正在尝试通过 JS 获取以前和当前的窗口宽度。我使用 jQuery 捕获窗口调整大小事件。这是我的代码:

<script>
function getWindowWidth() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
myWidth = window.innerWidth; myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth ||document.documentElement.clientHeight ) ) {
myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
myWidth = document.body.clientWidth; myHeight = document.body.clientHeight;
}
return myWidth;
}
var lastWindowWidth;
$(document).ready(function() {
$(window).resize(function() {
$('#h1_text').text("Previous: "+ lastWindowWidth + " Current: " + getWindowWidth());
lastWindowWidth = getWindowWidth();
});
});
</script>

它返回我:

Previous: 1685 Current: 1685

为什么 Previous: 和 Current: 值相似?提前致谢!

最佳答案

正在使用 jQuery。

所以使用 jQuery:

$(window).width();

 var lastWindowWidth;
$(document).ready(function() {
$(window).resize(function() {
var $window = $(this),
windowWidth = $window.width();
$('#h1_text').text("Previous: "+ lastWindowWidth + " Current: " + windowWidth );
lastWindowWidth = windowWidth;
});
});

fiddle :http://jsfiddle.net/maniator/pKfSN/5/

关于javascript - 获取上一个和当前的窗口宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12184943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com