一个JavaScript问题.
有人知道为什么在window.onresize页面加载时会在某些浏览器中调用它吗?
这可以避免吗?
我发现了IE中的问题,Android 27的Firefox 27(三星Galaxy S3上测试),Google Nexus 7(浏览Browserstack)和Windows Phone 8(Internet Explorer).
我的testpage看起来像这样:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<script type="text/javascript">
window.onresize = resize;
function resize(){
alert("resize event detected!");
}
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
解:
var windowWidth = $(window).width();
window.onresize = resize;
function resize(){
if($(window).width()!=windowWidth){
alert("Bingo");
windowWidth = $(window).width();
}
}
Run Code Online (Sandbox Code Playgroud)