JQuery slide animation delay

My jQuery slide animation lags when expanding a div #res.

Any suggestions?

JQuery

$(document).ready(function(){

$('select.first').change(function(){

    $(this).prop('disabled', true);
    var codata = $(this).val();
    var page = 1;

    $.ajax({

        type:'POST',
        url:'page.php',
        dataType:'json',
        data:{country:codata, page:page},
        success: function(data) {

            var result='';
            $.each(data, function(i,e) {
            result += "<div id='outer'>"+e.sDo+'</div>';
            });
            $('#res').html(result);

        }


    }).done(function(){$('#res').slideDown();});

});

});

CSS

#res {

    background-color:gainsboro;
    border:1px solid gray;
    width:100%;
    display:none;
    padding-top:10px;
}


#outer {

    width:100px; 
    text-align:center; 
    border:1px dotted black;
    margin:0 0 10px 10px;
    display:inline-block;
    height:40px;

}
+5
source share
2 answers

To collapse an element without jumping, the element must have a fixed width. Here is a demo to demonstrate. http://jsfiddle.net/WtkUW/1/

The reason for this is because jQuery calculates the height of the address of an element based on its width and its contents. If its width is 100%, jQuery cannot accurately calculate the height leading to the jump. The greater the content, the greater the jump.

+10
source

First of all, how fast does your page.php send a response? This may be the answer in full.

-, 2 ajax-: A) .ajax() B) .done().

. jQuery 1.8 (.: jQuery.ajax : ":" vs ".done" ?)

.done():

$.ajax({
    type:'POST',
    url:'page.php',
    dataType:'json',
    data:{country:codata, page:page}    
})
.done( function(data) {

    var result='';
    $.each(data, function(i,e) {
    result += "<div id='outer'>"+e.sDo+'</div>';
    });
    $('#res').html(result);

    $('#res').slideDown();
});

, , .

+1

All Articles