jQuery/javaScript:何时获得正确的容器Div尺寸?
jQuery/javaScript:何时获得正确的容器Div尺寸?
<div>
<div class="container">
<div class="slider">
<img src="image1.jpg">
<img src="image2.jpg">
</div>
<div>
</div>
|container----|
--------------Slider--------
|image1 | image2 |
-----------------------------
获得容器宽度并将图像宽度设置为容器宽度的合适时机是什么?
尝试的
$(document).ready(function() {
var width = $(".container").width();
$("img").css("width", width + "px");
});
宽度每次重新加载时的宽度都不同,例如524,1449,1920,
正确的值为524。
当调试代码时,请始终获得正确的值524。原因可能是容器div在调试模式下完全渲染(停在断点)。
jquery/javascript: when to get the correct container div size?
<div>
<div class="container">
<div class="slider">
<img src="image1.jpg">
<img src="image2.jpg">
</div>
<div>
</div>
|container----|
--------------Slider--------
|image1 | image2 |
-----------------------------
What is the right time to get container width and set the image width to the container width?
Tried
$(document).ready(function() {
var width = $(".container").width();
$("img").css("width", width + "px");
});
The width is different every time the page is reloaded, e.g., 524, 1449, 1920
the correct value is 524.
When debugging the code, always get correct value 524. The reason might be that the container div is fully rendered during debugging mode (stopped at breakpoint).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难说出为什么宽度会根据您的代码发生变化...因此我发布了3个解决方案。 编辑:阅读您的评论后,我相信解决方案2是您想要的。
解决方案1-使用大众
简单解决方案应将图像宽度设置为CSS中的100VW:这样:
#img {width = 100VW}
解决方案2-更新window.onload中
通常
window.onload =()=&gt; {}
会做。这就是为什么:图像负载与DOM负载
看来,图像加载时间可能会影响您的容器大小。使用
Ready
时,代码在等待图像加载之前会启动。然后,您的代码在图像具有“真实”宽度之前运行。您应该使用$(window).on(“加载”,处理程序)
完整paxdiablo的解释
解决方案3'hacky' - 调整事件侦听器
这是一种“ hack”解决方案,尽管以上所有内容都无法正常工作,它可能对您有效(就像我一样,这是一般的'不确定是什么原因导致代码的宽度变化)。假设您不希望容器尺寸始终不断变化,而只是在加载后才更改。您可以在有限的时间内收听更改,然后将其关闭:
然后,您可以在一段时间后转动此事件侦听器,或者在用户以某种方式与您的页面进行交互时:
请参见详细的示例在这里。请注意,它是hacky的,并且AS cesare-polonara 提到:
It is hard to tell why the width is changing according to your code... So I posted 3 solutions. Edit: After reading your comments, I believe solution 2 is what you are looking for.
Solution 1 - using vw
The simple solution should be setting the image width to 100vw in css like so:
#img {width=100vw}
Solution 2 - update in window.onload
Usually
window.onload = () => {}
will do. Here is why:image load vs. DOM load
It appears that the image load time might effect your container size. When using
ready
the code initiates before waiting for images to load. Then, your code runs before the images have their 'true' width. you should use$(window).on("load", handler)
Full explanation by paxdiablo
Solution 3 'hacky' - resize event listener
This is a kind of a 'hack' solution, though if all of the above won't work, it might work for you you (it is general as I'm not sure what causes the width changes in your code). Assuming you don't want the container size to keep changing always, but only after load. You could listen to changes for limited time, then turn them off:
You could then turn of this event listener after some time, or as user interacted with your page somehow:
See detailed example here. Note that it is hacky, and as cesare-polonara mentioned: