views:

2809

answers:

4

Hi, my jQuery skills are pretty good normally but this is driving me mad!

It's a fairly simple accordian I've coded up from scratch. Using jQuery 1.3.2 so there shouldn't be any jumping bugs but basically if you take a look at the example:

http://www.mizudesign.com/jquery/accordian/basic.html

I'm displaying the height for the target div on the right - if it contains text it thinks it's shorter than it is and jumping. If it's an image there's no problem.

I can't figure out where I'm going wrong - it's obviously in the CSS somewhere but I've tried all the usual suspects like display:block

Any ideas would be gratefully received!

Yours, Chris

PS Please forgive the nature of the source code, I've ripped it out the whole project I'm working on so it does include some divs that don't really need to be there.

A: 

Get the height once the div has finished its animation from the callback. It's possible that you're getting the height while the div is being animated, and you're getting a transitional value.

If your animation is jumpy, try using the callbacks. Don't open a div and hide a div at the same time. Instead, hide your first div, and within the callback show your next div.

$(".someDiv").slideUp("normal", function(){
  /* This animation won't start until the first
     has finished */
  $(".someOtherDiv").slideDown();
});

Updated (From the comments):

redsquare: http://jqueryfordesigners.com/slidedown-animation-jump-revisited/

Jonathan Sampson
Thanks for the answer but I'm not trying to get the height really - that's just debug information.I just want it to animate nicely without jumping!
Mizu
What do you mean by jumping?
Daniel Moura
Can you not see the effect on the demo link?When you hit the first two tabs the content jumps at the bottom instead of sliding all the way down.It's not animating smoothly like it does with the third tab.According to the panel on the right of the page it's not pulling out the correct height to animate too. It seems to only work it out once it's animated it once.
Mizu
Try chaining your animations together through their callbacks.
Jonathan Sampson
Mizu, you're incorrect about the height. You're grabbing the height before the div is even visible completely. This will give you an incomplete height - it would be like me measuring your height when you bend down to tie your shoe.
Jonathan Sampson
Okay I've cleared confused the issue by including the height information.Simple question then, why doesn't slideDown() in this instance display smoothly. It displays the first x pixels and then jumps the rest.
Mizu
Mizu, use the callbacks. I've updated my answer with an example.
Jonathan Sampson
Thanks Jonathan, it's not that I don't believe and I appreciate the answer but it's still not right.Here's a real simple version with just one tab using slideToggle.http://www.mizudesign.com/jquery/accordian/simple.htmlIt still jumps down at the bottom rather than animating smoothly.
Mizu
you need a height/width on the div. Remy Sharp has a blog post about this issue see http://jqueryfordesigners.com/slidedown-animation-jump-revisited/
redsquare
if I add a width or height to that content the animation becomes smooth
redsquare
Adding a predefined height for a box of text is kinda...ugly, no?
Jonathan Sampson
jon - agreed yes. Another way is to load the same content offscreen and apply the height dynamically. Both not good and are a result of jq 1.3+ when the dimension code was rewritten
redsquare
Jonathan you're not wrong - especially as this is going to be dynamic content so I don't know what height it needs to be!Oh well :( Thanks for all your help.
Mizu
Good luck, Mizu.
Jonathan Sampson
I must admit I've found my own dynamic solution now. http://www.mizudesign.com/jquery/accordian/basic.html should be fixed. It's very simple really - just adds the height using .css before hiding the div.Works a treat :)
Mizu
+2  A: 

You need a width or height on the content for it to animate smoothly.

See Remy Sharps post here

redsquare
Thanks I had read the post but assumed (as I'd read a bug elsewhere) that it applied to a previous version of jQuery.Shame.
Mizu
Must admit I think I've found a better solution since playing around with it - as documented below! :)
Mizu
it still basiclly adds the height to the element!
redsquare
Except it stores the height in the DOM for reference at any point in time. I started to look at using jQuery .data to store the info but this seemed far more efficient :)
Mizu
A: 

I think the problem is, that when padding or margin is added then it jumps, this was the case by me. you have to animate the margin in the callback

Also "keep in mind" that tables behave buggy with slideDown slideUp and rather use fadeIn fadeOut

adardesign
+3  A: 

I must admit I've found my own dynamic solution now.

http://www.mizudesign.com/jquery/accordian/basic.html should be fixed.

It's very simple really - just adds the height using .css before hiding the div. Works a treat :)

$("#PlayerButtonsContent div").each (function() {
$(this).css("height", $(this).height());
});

$("#PlayerButtonsContent div").hide();
Mizu