【问题标题】:jQuery: When to query new outer height after CSS change?jQuery:CSS更改后何时查询新的外部高度?
【发布时间】:2016-07-11 18:46:29
【问题描述】:

如果我这样做

var theHeight;
element.css('min-height', h);
theHeight = element.outerHeight();

在 Chrome 中,我得到 old 外部高度。

我必须这样做

var theHeight;
element.css('min-height', h);
setTimeout(function() {
  theHeight = element.outerHeight();
}, 1);

改为(或者,当然,任何产生类似效果的东西)让outerHeight 报告“正确”高度(我希望这是更改min-height 后我们可以看到的持续真实高度)。

很遗憾,setTimeout 不适合我。我可以用什么代替?它应该适用于所有主流浏览器(当前版本)。

【问题讨论】:

  • setTimeout 可能是您的最佳选择
  • element 是否设置了height
  • 我无法重现您的问题。我得到了正确的高度。我用的是 Chrome 本身
  • @Zlatev: 没有设置height,而h 的值是'',这使得min-height 之前的效果消失了。
  • 我无法复制它,兄弟,检查这个imgur.com/m6bZTWL

标签: javascript jquery css google-chrome outerheight


【解决方案1】:

setTimeout 不是您的最佳选择。

您要做的是在 DOM 加载后应用一些 javascript(CSS 在 document.ready 之前应用)。

您可以使用JQuery 轻松完成。

$(document).ready(function(){
  modifySomeDomElement();
});

docs here.

希望对你有帮助,

里斯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-30
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多