kangax wrote:
>
> There's a patch in a trac [1] which might or might not make its way
> into the core. The patch makes sure #getDimensions temporarily
> "displays" all "hidden" ancestors of an element, allowing clientWidth/
> clientHeight (used internally) to yield the correct results.
I applied the patch and it works perfectly for me. Thank you.
> We are deciding on some performance issues at the moment, as this
> could lead to undesired overhead.
>
In my case it works grate and I haven't experience much performance
influence by this change but I'll test it more before I'll put patched
prototype.js into my production env.
Thanks once more for your help and much thanks for all of you who
writes prototype.
Regards
Szymon Wilkolazki
> On Feb 20, 1:10 pm, SWilk <wilkola...@gmail.com> wrote:
>> Hi,
>>
>> I have found an irritating behaviour of Element.getWidth(), or a bug
>> maybe.
>>
>> Assume we have some code:
>>
>> <div style="display: none;">
>> <!-- many elements here: a table with about 60 inputs, labels etc -->
>> <input id="elem" class="a_class_that_sets_the_width" />
>> </div>
[cut]
kangax wrote:
> Szymon,
>
> There's a patch in a trac [1] which might or might not make its way
[...]
> We are deciding on some performance issues at the moment, as this
> could lead to undesired overhead.
>
Ok, I noticed performance issues with this patch. It happens in one of
my pages where I call getDimensions in onScroll event. The element
which dimensions I need to fetch is visible.
I assumed that if the element has either width or height non-zero,
then it is visible and we can safely return those values. If both
width and height are zeros, then we can't be sure if the element is
visible but width and height set to zeros or if it or it's ancestors
are invisible, so we execute the code staaky provided in his patch [1].
Please correct me if this assumption is incorrect.
I modified my copy of prototype.js (previously patched with [1],
elementgetdimensions_displaynone_anc_reccollect_unit.diff)
so it looks like this:
getDimensions: function(element) {
element = $(element);
+ var dimensions = {
+ width: element.clientWidth,
+ height: element.clientHeight
+ };
+ if (dimensions.width == 0 && dimensions.height == 0) {
var restore = element.ancestors(function(element) {
return !element.visible() }),
styles = [];
restore.push(element);
// All *Width and *Height properties give 0 on elements with
// display none,
// or when ancestors have display none, so enable those
// temporarily
restore.each(function(r) {
styles.push({
display: r.getStyle('display'),
position: r.getStyle('position'),
visibility: r.getStyle('visibility')
});
r.setStyle({display: 'block',
position: 'absolute', visibility: 'visible'});
});
- var dimensions = {
+ dimensions = {
width: element.clientWidth,
height: element.clientHeight
};
restore.each(function(r, index) {
r.setStyle(styles[index]);
});
+ }
return dimensions;
},
Now the function works fast when it is possible and provides correct
results for both hidden and shown elements.
I will optimize my code, too. It should not execute getDimensions with
every onscroll event.
> [1] http://dev.rubyonrails.org/ticket/11142
>
Regards,
Szymon Wilkolazki