f1n*_*1nn 4 javascript jquery resize
我试图通过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>
Run Code Online (Sandbox Code Playgroud)
它回报我:
Previous: 1685 Current: 1685
Run Code Online (Sandbox Code Playgroud)
为什么上一个:和当前:值相似?提前致谢!
你正在使用jQuery.
所以使用jQuery:
$(window).width();
Run Code Online (Sandbox Code Playgroud)
var lastWindowWidth;
$(document).ready(function() {
$(window).resize(function() {
var $window = $(this),
windowWidth = $window.width();
$('#h1_text').text("Previous: "+ lastWindowWidth + " Current: " + windowWidth );
lastWindowWidth = windowWidth;
});
});
Run Code Online (Sandbox Code Playgroud)
小提琴:http://jsfiddle.net/maniator/pKfSN/5/
| 归档时间: |
|
| 查看次数: |
6305 次 |
| 最近记录: |