On 13/11/2011 15:35, Thomas 'PointedEars' Lahn wrote:
> J.R. wrote:
>
>> Thomas 'PointedEars' Lahn wrote:
>>> J.R. wrote:
>>>> On 10/11/2011 12:09, David Mark wrote:
>>>>> if (document.documentElement&& typeof
>>>>> document.documentElement.clientWidth == 'number') {
>>>>> var getInnerDimensions = function(el) {
>>>>> return [el.clientHeight, el.clientWidth];
>>>>> };
>>>>> }
>>>>
>>>> Considering an element having scrollbars, we might use:
>>>>
>>>> return [el.scrollTop + el.clientHeight,
>>>> el.scrollLeft + el.clientWidth];
>>>
>>> Please explain what the scroll position has to do with the element
>>> dimensions.
>>
>> […]
>> A better approach would be using scrollWidth and scrollHeight, but these
>> properties are "buggy" in IE and Opera, according to
>> <http://www.quirksmode.org/dom/w3c_cssom.html#elementview>. The irony
>> here is that scrollWidth and scrollHeight were introduced by Microsoft
>> in their MSIE's DHTML object model...
>>
>> Therefore, if we want a "getInnerDimensions" function to return the
>> correct dimensions for the element's content we will need to add the
>> scrollTop and scrollLeft values to the clientHeight and clientWidth
>> values respectively, otherwise we would not get the correct inner
>> dimensions if the element is scrolled all the way down or to the right.
>>
>> But what if the element doesn't have scrollbars? No problem, because
>> scrollTop and scrollLeft will return zero.
>
> And if I scroll down or right a scrollable element its content becomes
> higher/wider?
>
No, but you can't retrieve the width / height of a scrollable element's
content with just clientHeight and clientWidth properties.
>>>> Note: scrollWidth/scrollHeight are buggy in IE6-8 and Opera 10
>>>>
>>>> So, we'd need to compare (scrollTop + clientHeight) to scrollHeight too.
>>>> In IE8, scrollWidth is 5 pixels off. See
>>>> <http://www.quirksmode.org/dom/w3c_cssom.html>
>>>
>>> That states that *scrollHeight* is buggy in IE *5.5* to 7, and that
>>> scrollWidth is _correct_ in those versions. It also states that
>>> scrollWidth
>>> is 5 pixel off in IE 8, and that "Opera gives odd, incorrect values."
>>> Quite different from what you stated.
>>
>> I don't think it is *quite* different, perhaps a *little* different...
>> In fact, it is better to avoid scrollWidth/scrollHeight whenever we need
>> a cross-browser code.
>
> It is better to be aware of it and use *sensible* workarounds if necessary.
> This is actually one case where property inference (window.opera) is useful
> and acceptable; IE should be dealt with using Conditional Comments instead.
>
ACK.
--
Joao Rodrigues (J.R.)
|