I’m new in javascript and I’ve a problem animating floating elements with jquery and hope someone can help me to fix it.
I would to be able to make a 100% width horizontal accordion.
For this, I have some floating divs in a wrapper. With javascript, i divide the wrapper’s width by the number of divs ( in percentage ). Then, on click, the div selected take the big size and the others the small size. The problem is during the animation, the last div goes at bottom. At the finish, it goes at the good place. I’ve seen the problem depend of the window’s size. In some case, it’s ok, but not always.
Here is a fiddle link : test
I’ve the solution to put the total width at 99% by making the open size for example at 69% instead of 70% but i really need to have it at 100% width and really don’t know how make it.
It could be nice if someone can help me to resolve this problem or find another solution.
PS: excuse my english 🙂
The problem is that you have two independent animations running that are not perfectly synchronized and you expect the total percentage of the two to always be less than 100%. When you start them both at the same time, sometimes this doesn’t happen and as soon as the total goes over 100%, the last one gets pushed to the next row.
The simplest workaround is to slightly delay the one you are growing so the total is always less than 100%. You can see a .delay(50) added to the second animation to make sure the growing element is always behind the shrinking element so the total is always less than 100%. Working demo: http://jsfiddle.net/jfriend00/Fkb5K/.
Snippet of code where
.delay(50)was added:Probably the best solution is a custom animation that changes both width percentages in the exact same animation (only one animation, not two) so they are always in perfect sync.
Here’s a custom animation. It removes the animation of the element that you are growing and instead adds a step function callback on all the ones that are shorter. The step function gets call anytime one of the elements changes size in an animation. In that step function, it sums the width of the shorter elements at that point in time and sets the longer one to track it exactly for a perfect sum of 100% every time.
Working demo here: http://jsfiddle.net/jfriend00/7zubL/