Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Getting computed/current style

Reply
Thread Tools

Getting computed/current style

 
 
Eric Bednarz
Guest
Posts: n/a
 
      03-13-2010

As a side effect of looking into browser-scripting animations I recently
started to look into retrieving CSS values with script (I do agree that
the start values of an animation should ideally be set automatically).

Incidentally, the entity known as inappropriate to cite other than as
work in progress[0] but encouraged to be referenced by the entity formerly
known as the specification/recommendation[1] says:
“Note. Dynamic movement of relatively positioned boxes can produce
animation effects in scripting environments […]”
<http://www.w3.org/TR/CSS2/visuren.html> 9.4.3

Since I never attempted to retrieve offset values of relatively
positioned elements by script, I wrote a little test case. I included
a column for a popular general purpose library to see if there’s any
magic bullet catch provided.

<http://bednarz.nl/tmp/relative/>

To my surprise the results are actually even worse than I expected. Some
notable observations:
- the only browser I could find that returns useful results is Firefox
3.6 (with the exception of resolving conflicts)
- Opera is totally broken
- jQuery does nothing more than returning the wrong results provided by the
getComputedStyle/currentStyle branch

Encouraging start of my new hobby.


Pointers to any good in depth research on the general topic would be
appreciated.




[0] <http://www.w3.org/TR/CSS2/> Status of this document
[1] <http://www.w3.org/TR/2008/REC-CSS2-20080411/>
 
Reply With Quote
 
 
 
 
David Mark
Guest
Posts: n/a
 
      03-13-2010
Eric Bednarz wrote:
> As a side effect of looking into browser-scripting animations I recently
> started to look into retrieving CSS values with script (I do agree that
> the start values of an animation should ideally be set automatically).


In many cases, yes. There are three types of animations in My Library:
those that work as transitions (i.e. as effects options for
showElement), those that don't (e.g. the "move" effect) and those that
are dual-purpose (e.g. the "grow" effect). The latter two have to
determine their starting points. Though they are as general purpose in
this task as they come, it is a better idea to know the contexts that
work and design with those in mind.

Some styles are easier animate cross-browser than others. My latest
add-on/example, which I am polishing up today creates effects using CSS3
transform styles and was fairly challenging to get working
cross-browser. Some of my older animations (dating back to the IE4
days) use the clip style extensively, but are strictly transitional
effects (e.g. "slide"), so there was never any need to determine
starting points for those.

I've noticed that many libraries go with a kitchen sink approach and
attempt with a single effect that ostensibly animates any style. I've
always considered that to be a mistake (particularly for performance).
The CSS3 stuff I am working was inspired by Dojo's recent botched
attempt to leverage "complex styles" (e.g. clip, transforms) to create
some new "wowie" effects with their already botched animation mechanism.
They went the kitchen sink route, which ensures that calculating
starting points will be impossible.

>
> Incidentally, the entity known as inappropriate to cite other than as
> work in progress[0] but encouraged to be referenced by the entity formerly
> known as the specification/recommendation[1] says:
> “Note. Dynamic movement of relatively positioned boxes can produce
> animation effects in scripting environments […]”
> <http://www.w3.org/TR/CSS2/visuren.html> 9.4.3


Not sure what relative positioning has to do with it.

>
> Since I never attempted to retrieve offset values of relatively
> positioned elements by script, I wrote a little test case. I included
> a column for a popular general purpose library to see if there’s any
> magic bullet catch provided.


In general, relative positions would be the last thing I would want to
animate as the elements' movement often affects surrounding elements
(and in the worst case, the entire document). But in certain contexts
they can be useful.

>
> <http://bednarz.nl/tmp/relative/>


Yes, they can be a pain to retrieve as well.

>
> To my surprise the results are actually even worse than I expected. Some
> notable observations:
> - the only browser I could find that returns useful results is Firefox
> 3.6 (with the exception of resolving conflicts)
> - Opera is totally broken
> - jQuery does nothing more than returning the wrong results provided by the
> getComputedStyle/currentStyle branch


Of course. If you use getElementPositionStyle in My Library, you should
have much better luck. For dimensions, getElementSizeStyle.

>
> Encouraging start of my new hobby.


Why aren't you using My Library? By coincidence, give me a day or
two and I will have a useful set of examples up that demonstrates how to
create custom effects for it. In creating the Transform add-on's
demonstration page, I refreshed my own memory of how my (somewhat
ancient and some might say primitive) effects mechanisms work and
decided it would be a good idea to share how the new add-on was created,
how it can be used and _most importantly_ how it compares to previously
created effects (e.g. the transition vs. non-transition vs. dual-purpose
concept). It will include copy and paste code snippets (some
dynamically generated) and also a primer on the alert and scrolling
anchors add-ons (the latter of which uses scrolling effects, which fall
into the non-transition category).

>
>
> Pointers to any good in depth research on the general topic would be
> appreciated.


Here's the thing. You can make use of offsetLeft/Top to check your
work. For instance, take your relative position quandary. Note that
you should not do this with elements that have 'none' as their display
style. This should be obvious and My Library makes no attempt to shield
you from such a mistake. Other libraries will go to a lot of trouble to
display, calculate and re-hide the element in this case, but I've always
considered that a mistake (if your app is trying to determine the size
or position of an element that is not part of the layout, then it
clearly has gaps in its logic that should be exposed rather than
spackled over). Sorry to wander off in that direction, but I recently
had somebody claim mine was "broken" because of this behavior. So
back to the thing at hand, grab the offsetLeft and offsetTop (verify
beforehand that they are available and numbers of course) then set the
left and top styles to those. Simply put:-

var offsetLeft = el.offsetLeft;
var offsetTop = el.offsetTop;

el.style.left = offsetLeft + 'px';
el.style.top = offsetTop + 'px';

And now for the cunning bit. Compare the new offsetLeft/Top property
values to the old. If they are the same, you "guessed" right. If they
are off, vive la difference!

That's basically it. You can do sizes in the exact same way. If you
think about it, there are several others (e.g. margins) that can be
determined in similar fashion. But then there are a ton of styles that
cannot be determined in any way but to deal with the computed/current
style mess and that's why context is often key to these things (i.e. you
can save yourself a lot of trouble if you consider the issues in your
CSS design). That's why I don't like GP effects (or GP anything for
that matter).

I've tested this and similar techniques in a heart-stopping number of
browsers and have never been surprised by the fact that it works in
virtually all of them.

HTH.
 
Reply With Quote
 
 
 
 
David Mark
Guest
Posts: n/a
 
      03-13-2010
David Mark wrote:
> Eric Bednarz wrote:
>> As a side effect of looking into browser-scripting animations I recently
>> started to look into retrieving CSS values with script (I do agree that
>> the start values of an animation should ideally be set automatically).

>
> In many cases, yes. There are three types of animations in My Library:
> those that work as transitions (i.e. as effects options for
> showElement), those that don't (e.g. the "move" effect) and those that
> are dual-purpose (e.g. the "grow" effect). The latter two have to
> determine their starting points. Though they are as general purpose in
> this task as they come, it is a better idea to know the contexts that
> work and design with those in mind.
>
> Some styles are easier animate cross-browser than others. My latest
> add-on/example, which I am polishing up today creates effects using CSS3
> transform styles and was fairly challenging to get working
> cross-browser. Some of my older animations (dating back to the IE4
> days) use the clip style extensively, but are strictly transitional
> effects (e.g. "slide"), so there was never any need to determine
> starting points for those.
>
> I've noticed that many libraries go with a kitchen sink approach and
> attempt with a single effect that ostensibly animates any style. I've
> always considered that to be a mistake (particularly for performance).
> The CSS3 stuff I am working was inspired by Dojo's recent botched
> attempt to leverage "complex styles" (e.g. clip, transforms) to create
> some new "wowie" effects with their already botched animation mechanism.
> They went the kitchen sink route, which ensures that calculating
> starting points will be impossible.
>
>> Incidentally, the entity known as inappropriate to cite other than as
>> work in progress[0] but encouraged to be referenced by the entity formerly
>> known as the specification/recommendation[1] says:
>> “Note. Dynamic movement of relatively positioned boxes can produce
>> animation effects in scripting environments […]”
>> <http://www.w3.org/TR/CSS2/visuren.html> 9.4.3

>
> Not sure what relative positioning has to do with it.
>
>> Since I never attempted to retrieve offset values of relatively
>> positioned elements by script, I wrote a little test case. I included
>> a column for a popular general purpose library to see if there’s any
>> magic bullet catch provided.

>
> In general, relative positions would be the last thing I would want to
> animate as the elements' movement often affects surrounding elements
> (and in the worst case, the entire document). But in certain contexts
> they can be useful.
>
>> <http://bednarz.nl/tmp/relative/>

>
> Yes, they can be a pain to retrieve as well.
>
>> To my surprise the results are actually even worse than I expected. Some
>> notable observations:
>> - the only browser I could find that returns useful results is Firefox
>> 3.6 (with the exception of resolving conflicts)
>> - Opera is totally broken
>> - jQuery does nothing more than returning the wrong results provided by the
>> getComputedStyle/currentStyle branch

>
> Of course. If you use getElementPositionStyle in My Library, you should
> have much better luck. For dimensions, getElementSizeStyle.
>


I just glanced at the one for positions and it is a little more involved
that the simple example given, but that's mostly because (at the time) I
didn't want it to go through the on-the-fly testing if it didn't have
to. So there is a feature test for broken position reporting (which is
likely not comprehensive) and some other checks to skip some known
problem cases. I should really just simplify it to do the same thing in
all cases as _getting_ the styles doesn't have to be quick.

I also see you are trying to get the right and bottom styles. That I
would advise against and animate the size instead. But given some
thought, if that's the abstraction you really want, I think you can
determine those with similar techniques.
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      03-13-2010
David Mark wrote:
> David Mark wrote:
>> Eric Bednarz wrote:
>>> As a side effect of looking into browser-scripting animations I recently
>>> started to look into retrieving CSS values with script (I do agree that
>>> the start values of an animation should ideally be set automatically).

>> In many cases, yes. There are three types of animations in My Library:
>> those that work as transitions (i.e. as effects options for
>> showElement), those that don't (e.g. the "move" effect) and those that
>> are dual-purpose (e.g. the "grow" effect). The latter two have to
>> determine their starting points. Though they are as general purpose in
>> this task as they come, it is a better idea to know the contexts that
>> work and design with those in mind.
>>
>> Some styles are easier animate cross-browser than others. My latest
>> add-on/example, which I am polishing up today creates effects using CSS3
>> transform styles and was fairly challenging to get working
>> cross-browser. Some of my older animations (dating back to the IE4
>> days) use the clip style extensively, but are strictly transitional
>> effects (e.g. "slide"), so there was never any need to determine
>> starting points for those.
>>
>> I've noticed that many libraries go with a kitchen sink approach and
>> attempt with a single effect that ostensibly animates any style. I've
>> always considered that to be a mistake (particularly for performance).
>> The CSS3 stuff I am working was inspired by Dojo's recent botched
>> attempt to leverage "complex styles" (e.g. clip, transforms) to create
>> some new "wowie" effects with their already botched animation mechanism.
>> They went the kitchen sink route, which ensures that calculating
>> starting points will be impossible.
>>
>>> Incidentally, the entity known as inappropriate to cite other than as
>>> work in progress[0] but encouraged to be referenced by the entity formerly
>>> known as the specification/recommendation[1] says:
>>> “Note. Dynamic movement of relatively positioned boxes can produce
>>> animation effects in scripting environments […]”
>>> <http://www.w3.org/TR/CSS2/visuren.html> 9.4.3

>> Not sure what relative positioning has to do with it.
>>
>>> Since I never attempted to retrieve offset values of relatively
>>> positioned elements by script, I wrote a little test case. I included
>>> a column for a popular general purpose library to see if there’s any
>>> magic bullet catch provided.

>> In general, relative positions would be the last thing I would want to
>> animate as the elements' movement often affects surrounding elements
>> (and in the worst case, the entire document). But in certain contexts
>> they can be useful.
>>
>>> <http://bednarz.nl/tmp/relative/>

>> Yes, they can be a pain to retrieve as well.
>>
>>> To my surprise the results are actually even worse than I expected. Some
>>> notable observations:
>>> - the only browser I could find that returns useful results is Firefox
>>> 3.6 (with the exception of resolving conflicts)
>>> - Opera is totally broken
>>> - jQuery does nothing more than returning the wrong results provided by the
>>> getComputedStyle/currentStyle branch

>> Of course. If you use getElementPositionStyle in My Library, you should
>> have much better luck. For dimensions, getElementSizeStyle.
>>

>
> I just glanced at the one for positions and it is a little more involved
> that the simple example given, but that's mostly because (at the time) I
> didn't want it to go through the on-the-fly testing if it didn't have
> to. So there is a feature test for broken position reporting (which is
> likely not comprehensive) and some other checks to skip some known
> problem cases. I should really just simplify it to do the same thing in
> all cases as _getting_ the styles doesn't have to be quick.
>


I see there is also some additional screwiness in there for determining
hypothetical styles for inline elements (i.e. what would be its style if
it were positioned). So this is not a pure position reporting function.
IIRC, the point of this added complexity was for determining the styles
needed for a positioned overlay.

So I definitely think a simplified version based on the example I posted
will serve you better than that function. It's a common theme in My
Library that some functions are too complex for their own good. I'm
making a note to break that one in two (one for getting true positions
and another to determine positions for aspiring overlays).
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      03-13-2010
On Mar 13, 1:39*pm, Eric Bednarz <(E-Mail Removed)> wrote:
> (...)
> Pointers to any good in depth research on the general topic would be
> appreciated.


Whenever I have wanted to obtain the actual position of an element I
have used .offset[Top,Left,Height,Width] rather than getComputedStyle.
--
Jorge.
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      03-13-2010
Eric Bednarz wrote:
> As a side effect of looking into browser-scripting animations I recently
> started to look into retrieving CSS values with script (I do agree that
> the start values of an animation should ideally be set automatically).
>
> Incidentally, the entity known as inappropriate to cite other than as
> work in progress[0] but encouraged to be referenced by the entity formerly
> known as the specification/recommendation[1] says:
> “Note. Dynamic movement of relatively positioned boxes can produce
> animation effects in scripting environments […]”
> <http://www.w3.org/TR/CSS2/visuren.html> 9.4.3
>
> Since I never attempted to retrieve offset values of relatively
> positioned elements by script, I wrote a little test case. I included
> a column for a popular general purpose library to see if there’s any
> magic bullet catch provided.
>
> <http://bednarz.nl/tmp/relative/>
>
> To my surprise the results are actually even worse than I expected. Some
> notable observations:
> - the only browser I could find that returns useful results is Firefox
> 3.6 (with the exception of resolving conflicts)
> - Opera is totally broken
> - jQuery does nothing more than returning the wrong results provided by the
> getComputedStyle/currentStyle branch
>


I notice in FF3.6 that all is as expected until box #6, where the right
style is declared as 1px, expected to be computed as "-1px" and the
browser returns "1px" instead. Realize that the "computed" styles
offered by browsers have historically differed from what the
specification calls computed. Regardless, this is a case where the
specification semantics are irrelevant. The results are only wrong (in
a practical sense) if setting the respective styles to the retrieved
values changes the position (or size) of the box (which you will likely
find doesn't happen very often). That gets back to the techniques I
mentioned in the other post. For the right (as opposed to left)
position, the acid test should be something like:-

var offsetWidth = el.offsetWidth;
var rightStyle = yourComputedStyleWrapper(el, 'right');

if (rightStyle !== null) {
el.style.right = rightStyle;

if (el.offsetWidth != offsetWidth) {
// Adjust expectations based on the difference
}
}

A one-off feature test should not be too difficult. But again, I would
stick to animating height/width and forget about right/bottom as those
two have always been notably screwy cross-browser (e.g. some browsers
don't even render them properly).
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      03-13-2010
Jorge wrote:
> On Mar 13, 1:39 pm, Eric Bednarz <(E-Mail Removed)> wrote:
>> (...)
>> Pointers to any good in depth research on the general topic would be
>> appreciated.

>
> Whenever I have wanted to obtain the actual position of an element I
> have used .offset[Top,Left,Height,Width] rather than getComputedStyle.


But that is perfectly useless for animations in many cases as you can't
set those.
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      03-13-2010
On Mar 13, 4:02*pm, David Mark <(E-Mail Removed)> wrote:
> Jorge wrote:
> > Whenever I have wanted to obtain the actual position of an element I
> > have used .offset[Top,Left,Height,Width] rather than getComputedStyle.

>
> But that is perfectly useless for animations in many cases as you can't
> set those. *


I said "to obtain" not "to set", David Mark.
--
Jorge.
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      03-13-2010
Jorge wrote:
> On Mar 13, 4:02 pm, David Mark <(E-Mail Removed)> wrote:
>> Jorge wrote:
>>> Whenever I have wanted to obtain the actual position of an element I
>>> have used .offset[Top,Left,Height,Width] rather than getComputedStyle.

>> But that is perfectly useless for animations in many cases as you can't
>> set those.

>
> I said "to obtain" not "to set", David Mark.


Which has no bearing on this discussion, "Jorge".
 
Reply With Quote
 
Garrett Smith
Guest
Posts: n/a
 
      03-14-2010
Eric Bednarz wrote:
> As a side effect of looking into browser-scripting animations I recently
> started to look into retrieving CSS values with script (I do agree that
> the start values of an animation should ideally be set automatically).
>
> Incidentally, the entity known as inappropriate to cite other than as
> work in progress[0] but encouraged to be referenced by the entity formerly
> known as the specification/recommendation[1] says:
> “Note. Dynamic movement of relatively positioned boxes can produce
> animation effects in scripting environments […]”
> <http://www.w3.org/TR/CSS2/visuren.html> 9.4.3
>
> Since I never attempted to retrieve offset values of relatively
> positioned elements by script, I wrote a little test case. I included
> a column for a popular general purpose library to see if there’s any
> magic bullet catch provided.
>


"Offset" values and top/left style values are different things.
Offset", to me, is about the badly defined and highly inconsistent
"offsetTop", "offsetLeft", and "offsetParent". These properties are
different in IE8, IE7, IE6, Safari 4, Safari 3, Safari 2, and so on. For
every major browser version, there is a different meaning to offsetTop.
So take 5 browsers and take the latest 3 versions, that is a total of 15
different implementations of offsetTop.

I think of animation as being a transition from one state to another in
a given amount of time.

That transition is a dynamic abstraction, and so if we are talking about
animation css style properties, then the animation would not use
offsets, but would use style properties.

> <http://bednarz.nl/tmp/relative/>
>


FWIS, the code appends relatively positioned children to an BODY that,
in most modern browsers, will have a margin.

What I do not understand is the "expected" column. What are the
expectations coming from?

> To my surprise the results are actually even worse than I expected. Some
> notable observations:
> - the only browser I could find that returns useful results is Firefox
> 3.6 (with the exception of resolving conflicts)
> - Opera is totally broken
> - jQuery does nothing more than returning the wrong results provided by the
> getComputedStyle/currentStyle branch
>


To retrieve element coordinate offsets with jQuery, jQuery's offset
method would be the method for that.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid header style inconsistent with sortable column style cedoucette@alum.rpi.edu ASP .Net 0 10-14-2005 12:13 AM
All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer Rob Nicholson ASP .Net 3 05-28-2005 03:11 PM
Getting DOM element position(style.left and style.top) Amir Hardon Javascript 0 05-31-2004 01:18 AM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Style sheets, include one style within another (not inheritance) foldface@yahoo.co.uk HTML 1 11-24-2003 01:37 PM



Advertisments