简体   繁体   English

jQuery加载DIV宽度在Firefox中不起作用

[英]jQuery to load DIV width not working in Firefox

I have a website that uses a JavaScript/jQuery script to get the width of a div based on the screen size. 我有一个网站,该网站使用JavaScript / jQuery脚本根据屏幕尺寸获取div的宽度。

When I pull the website up in Firefox the div does not appear because the width is being set to 0px. 当我在Firefox中打开网站时,div不会出现,因为宽度设置为0px。 The div appears fine in Chrome and IE, so I checked to make sure JavaScript was enabled in Firefox and it is. div在Chrome和IE中看起来不错,所以我检查以确保在Firefox中启用了JavaScript。

Any ideas on a solution? 关于解决方案有什么想法吗?

The script I'm using is: 我使用的脚本是:

$(document).ready(function () {
  var w = $('#content').css("margin-left");
  $('#wrapper').css("width", w);
});

If you open this JSfiddle in Firefox and inspect the sidebar wrapper div, you'll see the width is being set to 0, where in Chrome it has a value. 如果您在Firefox中打开此JSfiddle并检查侧边栏包装器div,则会看到该宽度被设置为0,在Chrome中该宽度为一个值。

Seems like everything is working on jsFiddle. 似乎一切都在jsFiddle上工作。

http://jsfiddle.net/wnxLr9bo/ http://jsfiddle.net/wnxLr9bo/

<div id="wrapper">width of: </div>
<div id="content">the margin of: </div>

    $().ready(function() {
      var w = $('#content').css("margin-left");
      $('#content').append($('#content').css("margin-left"));
      $('#wrapper').css("width", w);
      $('#wrapper').append(w);
    });

#content {
    margin-left: 100px;
}

#wrapper {
    width: 0px;
}

So here's what I found on another thread...I guess Firefox isn't going to let it happen. 所以这是我在另一个线程上发现的...我认为Firefox不会让它发生。

Unfortunately, this comes down to browser differences. 不幸的是,这归结于浏览器的差异。 To quote an answer to a similar problem : 引用类似问题的答案

As to why Chrome and IE return different values: .css() provides a unified gateway to the browsers' computed style functions, but it doesn't unify the way the browsers actually compute the style. 关于Chrome和IE返回不同值的原因: .css()为浏览器的计算样式函数提供了一个统一的网关,但是并没有统一浏览器实际计算样式的方式。 It's not uncommon for browsers to decide such edge cases differently. 浏览器以不同的方式来决定这种极端情况并不少见。

So you're kinda screwed. 因此,您有点上当了。 You have a few options to make this consistent. 您可以选择几种方法来保持一致。

You can reliably return auto by hiding the element before you compute the style. 您可以通过在计算样式之前隐藏元素来可靠地返回auto Something like this might work: 这样的事情可能会起作用:

var $bar = $('.bar');
$bar.hide();
var barMarginRight = $('.bar').margin().right; // "auto"
// do whatever you need to do with this value
$bar.show();

You can also use jQuery's $('.bar').offset() , which returns properties you might be able to use. 您还可以使用jQuery的$('.bar').offset() ,它返回您可能可以使用的属性。

// This might not be exactly what you want, but...
$('.spec').css('margin-left', $('.bar').offset().left + 'px');

You can also try to fix the problem with CSS, though we'd need to see your whole page to decide about that. 您也可以尝试使用CSS修复问题,尽管我们需要查看整个页面来做出决定。

The reason for your problem is a strange behaviour of Firefox and Safari , thats clearly against the W3C specification of getComputedStyle() and should be reported as a bug. 问题的原因是FirefoxSafari的异常行为,这显然违反了getComputedStyle()的W3C规范,应将其报告为错误。

You have set the margin of your #content to: 0px auto . 您已将#content的margin设置为: 0px auto Thats a common way to center a blocklevel element with given width in its containing block. 这是将包含给定宽度的块级元素居中的一种常见方法。 Browsers take the width of the parent, subtract the width of the the element and divide by two. 浏览器采用父级的宽度,减去元素的宽度并除以二。 The resulting value is is used for marginLeft and marginRight and should be returned as computedValue for both properties. 结果值将用于marginLeft和marginRight,并且应将这两个属性的值作为computeValue形式返回。 After calculation the element gets rendered (correctly centered). 计算后,将渲染元素(正确居中)。 But Firefox and Safari see the keyword auto and set computedStyle to 0px (the default value for auto) before doing calculation and rendering. 但是Firefox和Safari在执行计算和渲染之前,会看到关键字auto,并将computeStyle设置为0px(auto的默认值)。

Don't believe you can always rely on the values other browsers return. 不要相信您可以始终依靠其他浏览器返回的值。 At that moment the parent or grandparent itself have some margin or padding or the boxSizing property is changed somewhere, they get confused and return wrong values even when the rendering is correct. 在那一刻,父母或祖父母本身有一些边距或填充或boxSizing属性在某处更改,即使呈现正确,他们也会感到困惑并返回错误的值。

If you try to detect such cases by looking for the (shorthand) property margin , you'll fail again: Firefox, InternetExplorer and Safari answere with silence (empty string). 如果您尝试通过查找(简写)属性margin来检测到这种情况,则您将再次失败:Firefox,InternetExplorer和Safari沉默(空字符串)。 Chrome and Opera return the margin shorthand, but the values there are often different from what you get with marginLeft and marginRight. Chrome和Opera返回的是margin的简写形式,但其中的值通常与marginLeft和marginRight的值不同。

A workaround for marginLeft uses the difference between the left offset of the element and its parent (or its offset-parent, when element has position: 'absolute'). marginLeft一种变通方法是使用元素与其父元素(或其偏移父元素,当元素位置为“ absolute”时)的左偏移之间的差。 Refering to your code it looks like: 参照您的代码,它看起来像:

$(document).ready(function () {
    var con = $("#content"), cpos = con.css('position'), par, w;
    if (cpos === 'static' || cpos === 'relative') par = con.parent();
    else par = con.offsetParent();
    w = con.offset().left - par.offset().left;
    $("#wrapper").css('width', w);
});

I can not guarantee that it works in all usecases, but I havn't found problems until now. 我不能保证它在所有用例中都有效,但是直到现在我还没有发现问题。 Working JSFiddle here . 在这里工作的JSFiddle Now even Firefox and safari gets a width for the sidebar. 现在,即使是Firefox和safari,也可以使用边栏的宽度。 Second JSFiddle . 第二个JSFiddle Here you can toggle between using marginLeft and offsetLeft for a quick browser test. 在这里,您可以在使用marginLeft和offsetLeft之间进行切换,以进行快速的浏览器测试。

Since there is no offset right, the workaround for marginRight needs some more calculations.If you need it, post a comment and I will extend my answer. 由于没有补偿权,因此marginRight的解决方法需要更多计算。如果需要,请发表评论,我将扩展我的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM