jQuery/javaScript:何时获得正确的容器Div尺寸?

发布于 2025-01-25 08:10:00 字数 671 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的旅程 2025-02-01 08:10:00

很难说出为什么宽度会根据您的代码发生变化...因此我发布了3个解决方案。 编辑:阅读您的评论后,我相信解决方案2是您想要的。

解决方案1-使用大众

简单解决方案应将图像宽度设置为CSS中的100VW:这样:
#img {width = 100VW}

解决方案2-更新window.onload中

通常window.onload =()=&gt; {}会做。这就是为什么:

图像负载与DOM负载

看来,图像加载时间可能会影响您的容器大小。使用Ready时,代码在等待图像加载之前会启动。然后,您的代码在图像具有“真实”宽度之前运行。您应该使用$(window).on(“加载”,处理程序)

使用jquery,您使用$(document).ready()在加载 dom 和和$(window).on时执行某些内容(”加载”,处理程序)也加载所有其他内容时,执行某件事,例如图像

完整paxdiablo的解释

解决方案3'hacky' - 调整事件侦听器

这是一种“ hack”解决方案,尽管以上所有内容都无法正常工作,它可能对您有效(就像我一样,这是一般的'不确定是什么原因导致代码的宽度变化)。假设您不希望容器尺寸始终不断变化,而只是在加载后才更改。您可以在有限的时间内收听更改,然后将其关闭:

const resize_ob = new ResizeObserver((entries) => {
    let rect= entries[0].contentRect;
    let width = rect.width;

    // here you can select all of its image children and adjust their size

});

let myDiv=window.getElementById("container")
resize_ob.observe(myDiv)

然后,您可以在一段时间后转动此事件侦听器,或者在用户以某种方式与您的页面进行交互时:

//end resize observation
resize_ob.unobserve(myDiv);

请参见详细的示例在这里。请注意,它是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)

With jQuery, you use $(document).ready() to execute something when the DOM is loaded and $(window).on("load", handler) to execute something when all other things are loaded as well, such as the images

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:

const resize_ob = new ResizeObserver((entries) => {
    let rect= entries[0].contentRect;
    let width = rect.width;

    // here you can select all of its image children and adjust their size

});

let myDiv=window.getElementById("container")
resize_ob.observe(myDiv)

You could then turn of this event listener after some time, or as user interacted with your page somehow:

//end resize observation
resize_ob.unobserve(myDiv);

See detailed example here. Note that it is hacky, and as cesare-polonara mentioned:

probably will lead to weird behaviours used in that way since he won't listen to width change made programmatically

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文