简体   繁体   English

为什么在Firefox中,scrollWidth在这种情况下不起作用?

[英]Why scrollWidth doesn't work in this case in Firefox?

I'm trying to make a simple marquee in Javascript, and need to get the full content width of innerDIV in the following: 我正在尝试使用Javascript制作一个简单的字幕,并且需要在以下内容中获取innerDIV的完整内容宽度

<div id="container" style="width: 100%; overflow: hidden;">
    <div id="innerDiv" style="direction: rtl; white-space: nowrap; overflow: visible; position: relative;">
        very long ... text
    </div>
</div>

I tried scrollWidth . 我尝试了scrollWidth It works well in Chrome but not in Firefox (where it gives the value of clientWidth instead). 它在Chrome中效果很好, 但在Firefox中效果不佳(而是提供clientWidth的值)。

Here is a live demonstration: http://jsfiddle.net/5fPGy/3/ (try it with Chrome and Firefox) 这是一个现场演示: http : //jsfiddle.net/5fPGy/3/ (在Chrome和Firefox中尝试)

Does anyone know why? 有人知道为什么吗? and how to get that full width? 以及如何获得整个宽度?

Thanks in advance. 提前致谢。

I had the same issue and I found an answer for your questions. 我遇到了同样的问题,并且为您的问题找到了答案。 This is a bug in Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=833542 . 这是Firefox中的错误: https : //bugzilla.mozilla.org/show_bug.cgi?id=833542

Firefox returns for scrollWidth the clientWidth value. Firefox为scrollWidth返回clientWidth值。 It should be fixed in Firefox 21. 应该在Firefox 21中修复。

For now, I used a workaround to get the correct scrollWidth on Firefox: set the overflow to hidden, get the correct scrollWidth and revert the overflow to visible. 现在,我使用一种解决方法在Firefox上获取正确的scrollWidth:将溢出设置为隐藏,获取正确的scrollWidth并将溢出恢复为可见。 Please see: http://jsfiddle.net/5fPGy/5/ 请参阅: http : //jsfiddle.net/5fPGy/5/

var scrollWidth = $(ele).css("overflow", "hidden")[0].scrollWidth;
alert('clientWidt h = ' + ele.clientWidth + ',  scrollWidth = ' + scrollWidth  );
$(ele).css("overflow", "visible");

Best of luck, 祝你好运

Andrei 安德烈

This seems to do the trick: 这似乎可以解决问题:

docWidth = Math.max(
        Math.max(frameDoc.body.scrollWidth, frameDoc.documentElement.scrollWidth),
        Math.max(frameDoc.body.offsetWidth, frameDoc.documentElement.offsetWidth)                               
);

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

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