Firefox 在 iframe 中没有给出正确的高度

发布于 2024-10-17 21:07:45 字数 642 浏览 2 评论 0原文

此错误类似于:

jQuery 在 Firefox iframe 中报告不正确的元素高度

他返回的号码不正确。我的区别在于,当我的框架加载时 - 它给出零。

我试图将其内容的高度发送到父窗口。它在加载时不断发回零。如果我设置 50 毫秒的间隔,我会看到数字不断增加,直到达到正确的数字。零始终是间隔之前给出的第一个数字。

内部至少有一个元素具有预定义的高度 - 有些带有 css,有些带有内联。因此,如果元素仍在加载,则没有理由发送回零。

如果我 console.log $('#element'),该元素看起来是隐藏的(在最新版本中褪色)。如果我在 Chrome 中运行它,它会完美运行。我还尝试了本机 clientHeight,它也给出了 0,所以我不认为它是 jQuery。

编辑: 在我尝试计算高度之前,这些元素是通过 JS 添加的。它们在开始时不在页面上可能是一个问题..将尝试制作一个简单的演示页面来发布。

Firefox 4 beta 上不会发生这种情况

This bug is similar to:

jQuery reports incorrect element height in Firefox iframe

He gets back an incorrect number. My difference is that when my frame loads - it's giving zero.

I am trying to send the height of its contents to the parent window. It keeps sending back zero on load. If I set an interval for 50ms, I'll see the number increase up until the correct number. Zero is ALWAYS the first number given before the interval.

At least one of the elements inside has a predefined height - some with css and some with inline. So there is no reason it should be sending back zero if elements are still loading.

If I console.log $('#element'), the element looks hidden ( faded in latest versions ). If I run this in Chrome it runs perfect. I also tried native clientHeight which also gave 0, so I don't think it's jQuery.

EDIT:
The elements are added via JS before I try to calculate the height. Them not being on page at start might be an issue.. going to try and make a simple demo page to post.

This does NOT happen on Firefox 4 beta

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

束缚m 2024-10-24 21:07:45

我知道这是一个老问题,但我在 Firefox 中遇到了类似的问题,iframe 预加载到 Bootstrap 模式中,最终显示时被截断。在那之前我无法计算出正确的高度,但我可以使用一些 jQuery 让它合理地无缝调整大小(对于最终用户)。我在这里回答,以防其他人继续寻找类似问题的解决方案:

$("#myIframe").load(function() {
  $m = $(".modal-body");
  var mh = $m.height();

  var wait = setInterval(function() {
    var ih = $("#myIframe").contents().find("body").height();
    if (ih == mh) {
      clearInterval(wait);
    } else if (ih > 0) {
      clearInterval(wait);
      $m.animate({ height: ih });
    }
  }, 50);

  $(".modal").modal("show");
});

这个想法是,当加载 iframe 时,如果浏览器计算出其高度为 0,则我会循环直到其报告的高度发生变化,然后平滑地设置容器尺寸的动画以适应。

I know this is an old question, but I had a similar problem in Firefox, with an iframe pre-loaded into a Bootstrap modal, which was truncated when finally shown. I can't calculate the correct height until then, but I can make it resize reasonably seamlessly (to the end user) with a bit of jQuery. I'm answering here in case others follow on the hunt for a solution to a similar problem:

$("#myIframe").load(function() {
  $m = $(".modal-body");
  var mh = $m.height();

  var wait = setInterval(function() {
    var ih = $("#myIframe").contents().find("body").height();
    if (ih == mh) {
      clearInterval(wait);
    } else if (ih > 0) {
      clearInterval(wait);
      $m.animate({ height: ih });
    }
  }, 50);

  $(".modal").modal("show");
});

The idea is that when the iframe is loaded, if a browser calculates its height is 0, then I loop until its reported height changes, and then smoothly animate the container size to fit.

独享拥抱 2024-10-24 21:07:45

iframe 在计算时是 display: none 因为我不想在一切设置完成之前显示它。因此,即使它在技术上是一个不同的窗口,它仍然认为元素的父元素不显示。

The iframe is display: none at the time of the calculation because I didn't want to show it until everything is set. So even though it's technically a different window, it's still thinking the parent of the elements is display none.

强者自强 2024-10-24 21:07:45

如果没有看到你的 HTML、jQuery 等,就很难确定。

我想说的是,当返回的高度似乎不准确时,我很幸运地使用了offsetHeight。你试过这个吗?

编辑:另一个想法:我过去注意到,在内容完全加载之前通过 JavaScript 访问 IFRAME 可能会出现问题。只是要记住一些事情。

Without seeing your HTML, jQuery, etc it's hard to say for sure.

I will say that I've had luck using offsetHeight when the height being returned doesn't seem to be accurate. Have you tried this?

EDIT: Another thought: I've noticed in the past that accessing an IFRAME via JavaScript before the contents are fully loaded can be problematic. Just something to keep in mind.

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