当屏幕分辨率改变时用 jQuery 做一些事情
我想在页面加载时将 div 的高度设置为窗口的高度...
所以我使用了以下代码:
$(document).ready(function () {
var x = screen.availWidth;
var y1 = screen.availHeight;
var y2 = screen.Height;
var w = $(window).height();
var d = $(document).height();
alert('availHeight : ' + y1);
alert('Height : ' + y2);
alert('window : ' + w);
alert('document : ' + d);
$('#OuterDiv').css({ 'height': w });
//alert($('#OuterDiv').css('height'));
}); //End Of $(document).reedy
问题#1:
当屏幕分辨率更改时,div 仍然具有以前的高度。
我该如何解决这个问题?
问题 #2:
我希望该 div 的 height=100%,并且由于一些 css 问题,我使用了 jQuery。
但是当页面加载时(页面加载期间),div 的高度就是其内容的高度,并且在 jQuery 工作之后,该 div 获取窗口的高度。
我认为这在页面加载期间不太好。
我该如何解决这个问题?
height:100%
不适用于该 div:
原因如下 -> 链接
谢谢提前
I want to set height of a div as height of window when page loads...
So I used the below code :
$(document).ready(function () {
var x = screen.availWidth;
var y1 = screen.availHeight;
var y2 = screen.Height;
var w = $(window).height();
var d = $(document).height();
alert('availHeight : ' + y1);
alert('Height : ' + y2);
alert('window : ' + w);
alert('document : ' + d);
$('#OuterDiv').css({ 'height': w });
//alert($('#OuterDiv').css('height'));
}); //End Of $(document).reedy
Problem #1:
When screen resolution changes that div still have previous height.
How can I fix that ?
Problem #2:
I want height=100% for that div and because of some css problems I used jQuery.
But when page is loading (during page load) that div's height is the height of it's content and after that jQuery worked, that div gets window's height.
I think this is not nice during page load.
How can I fix this issue?
height:100%
does not work for that div:
Here is the reason -> LINK
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以像这样捕获调整大小事件:
如果您想立即调整大小,请将脚本从就绪事件中取出,并将其内联放置在
div
正下方(并且可能在就绪时再次调用它)以及)例如
You can capture the resize event like so:
If you want to resize straight away, take the script out of the ready event, and put it inline, directly below your
div
(and maybe call it again on ready as well)E.g.
有什么理由不能只使用 CSS 吗?如果将 body 的宽度和高度设置为 100% 并绝对定位 div,应该会达到相同的效果。
这是您要查找的 jQuery 事件,它将 #box 设置为窗口的宽度和高度
这是 的示例http://jsfiddle.net/JxWm9/4/ 其中包含一个用于比较的 CSS 框。请注意,JS 版本滞后,因为它基于调整大小事件,而 CSS 调整大小是浏览器本身原生的。
Is there any reason you can't just use CSS? If you set the width and height of your body to 100% and position your div absolutely, you should achieve the same effect.
Here's the jQuery event you're looking for, which sets #box to the width and height of the window
Here's an example at http://jsfiddle.net/JxWm9/4/ which includes a CSS box for comparison. Note the JS version lags because it's based on the resize event, while the CSS resize is native to the browser itself.