Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery slide is jumpy

I tried to slide in and out a DIV with the toggle function of jQuery but the result is always jumpy at the start and/or end of the animation. Here's the js code that I use:

$(document).ready(function(){     $('#link1').click(         function() {             $('#note_1').parent().slideToggle(5000);         } ); 

And the HTML:

<div class="notice">     <p>Here's some text. And more text. <span id="link1">Test1</span></p>     <div class="wrapper">         <div id="note_1">             <p>Some content</p>             <p>More blalba</p>         </div>     </div> </div> 

You can also see the complete example here: jQuery Slide test

I usually use Mootools and I can do this slide without any problems with it. But I'm starting a new project in Django and most app in Django use jQuery. So for that and after reading this jQuery vs Mootools I decided that will be a good occasion to start using jQuery. So my first need was to slide this DIV. And it didn't work properly.

I did more search and I found that's an old bug in jQuery with margin and padding applied to the DIV. The solution is to wrap the DIV in another DIV. It didn't fix the thing in my case.

Searching further I found this post Slidedown animation jumprevisited. It fix a jump at one end but not at the other (Test2 in jQuery Slide test).

On Stack Overflow I found this jQuery IE jerky slide animation. In the comments I saw that the problem is with the P tag inside the DIV. If I replace the P tags with DIV tags that fix the problem but that's not a proper solution.

Lastly I found this Weird jQuery behavior slide. Reading it I understood that the problem resolved by switching from P tag to DIV was with the margins of the P (not present in the DIV) and the collapsing of margins between elements. So if I switch the margins to paddings it fix the problem. But I loose the collapsing behavior of margins, collapsing that I want.

Honestly I can say that my first experience with jQuery is not really good. If I want to use one of the simplest effect in jQuery I have to not use the proper function (slideToggle) but instead use some hand made code AND wrap the DIV in another DIV AND switch margins to paddings, messing my layout.

Did I miss a simpler solution ?

As krdluzni suggest, I tried to write as custom script with the animate method. Here's my code:

var $div = $('#note_2').parent(); var height = $div.height();  $('#link2').click(     function () {         if ( $div.height() > 0 ) {             $div.animate({ height: 0 }, { duration: 5000 }).css('overflow', 'hidden');         } else {             $div.animate({ height : height }, { duration: 5000 });         }          return false; }); 

But that doesn't work either because jQuery always set the overflow to visible at the end of the animation. So the DIV is reapearing at the end of the animation but overlaid on the rest of the content.

I tried also with UI.Slide (and Scale and Size). It works but the content below the DIV doesn't move with the animation. It only jump at the end/start of the animation to fill the gap. I don't want that.

UPDATE:

One part of the solution is to set the height of the container DIV dynamically before anything. This solve one jumping. But not the one cause by collapsing margin. Here's the code:

var parent_div = $("#note_1").parent(); parent_div.css("height", parent_div.height()+"px"); parent_div.hide(); 

SECOND UPDATE:

You can see the bug on the jQuery own site at this page (Example B): Tutorials:Live Examples of jQuery

THIRD UPDATE:

Tried with jQuery 1.4, no more chance :-(

like image 755
Etienne Avatar asked Aug 26 '09 15:08

Etienne


1 Answers

I found what works consistently is setting an invisible 1px border:

border: 1px solid transparent;

No need to fix the width or height or anything else and the animation doesn't jump. Hooray!

like image 151
Muers Avatar answered Sep 24 '22 10:09

Muers