Gordon ha scritto:
I am using a loop to initialize some elements for animation (befor
initialization they are allowed to float, but for the animation
process itself their positions need to be absolute). My code is as
follows.
$('.elemsToAnimate').each (function ()
{
var thisElem = $(this);
thisElem.css ({
top : this.offsetTop - parseInt
(thisElem.css ('margin-top'), 10),
left : this.offsetLeft - parseInt (thisElem.css
('margin-left'),
10),
width : 100,
height : 100
});
});
This achieves the desired effect, namely fixing elements that were
previously allowed to float to an absolute position for animating, but
the function runs rather slow. In fact it can take over 4 seconds to
run on a list containing 120 elements.
I could really do with some help finding a speedup, if you have any
tips I'd appreciate hearign them.
I think the best approach is to add a class to the elements and leave to
the stylesheet the position setting if possible.
At least you could avoid the
- parseInt (thisElem.css ('margin-top'), 10)
and
- parseInt (thisElem.css ('margin-left')
setting a new rule that nullify the margins.
If you are lucky, but it depends upon your layout, you could avoid to
call this.offsetTop and this.offsetLeft. In this case you could avoid
the each function too, calling a simple $('.elemsToAnimate').css() on
your elements.
Renato