[英]resize div with image on page load and window resize
The header of http://bm-translations.de changes its height depending on window width but sometimes when I load the page it looks like that: http://bm-translations.de的标题会根据窗口宽度更改其高度,但是有时在我加载页面时,它看起来像这样:
Thats the code: 那就是代码:
//navheader responsive height
$(window).on('resize', function(){
$("#banner").css("height",$('#navheader').height()+30);
}).trigger('resize'); //on page load
How to optimize it? 如何优化呢?
As suggested in comments I tried to set CSS calc instead, but without any result. 如评论中所建议,我尝试改为设置CSS calc,但没有任何结果。 What am I doing wrong?
我究竟做错了什么?
I set the parent #navigation
to height:100%
and #banner
to 我将父级
#navigation
设置为height:100%
并将#banner
为
height: -moz-calc(100% + 30px);
height: -webkit-calc(100% + 30px);
height: calc(100% + 30px);
You should use $( window ).on( "load", function() { ... })
instead of just run $(window).trigger('resize')
at the bottom of the page, as follows: 您应该使用
$( window ).on( "load", function() { ... })
而不是仅在页面底部运行$(window).trigger('resize')
,如下所示:
$(window).on("load", function() {
$(window).trigger('resize');
})
As you can see from the following link: https://learn.jquery.com/using-jquery-core/document-ready/ 从以下链接可以看到: https : //learn.jquery.com/using-jquery-core/document-ready/
A page can't be manipulated safely until the document is "ready."
在文档“就绪”之前,无法安全地操纵页面。 Code included inside
$( document ).ready()
will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.$( document ).ready()
内包含的代码仅在页面Document Object Model(DOM)准备好执行JavaScript代码后才能运行。 Code included inside$( window ).on( "load", function() { ... })
will run once the entire page (images or iframes), not just the DOM, is ready.一旦整个页面(图像或iframe)(不仅是DOM)准备就绪,
$( window ).on( "load", function() { ... })
将运行。
As you have images in navigation, you should wait the full page load for correct $('#navheader').height()
detection. 导航中有图像时,应等待整个页面加载,以正确检测
$('#navheader').height()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.