Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

javascript - 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:

<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. It works well in Chrome but not in Firefox (where it gives the value of clientWidth instead).

Here is a live demonstration: http://jsfiddle.net/5fPGy/3/ (try it with Chrome and Firefox)

Does anyone know why? and how to get that full width?

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 returns for scrollWidth the clientWidth value. It should be fixed in 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. Please see: 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...