Fixing vertical jump at end of jQuery slideDown animation

前端 未结 2 942
攒了一身酷
攒了一身酷 2021-02-13 16:44

I am new to Jquery but have written a simple vertical accordion. It seems to to the job I require, but at the end of the slide down there is a visible jerk.

If anyone co

2条回答
  •  南笙
    南笙 (楼主)
    2021-02-13 16:53

    In your custom code, I got rid of the 'jump' by making a small change in the CSS and specifying the height of the p tags within the accordion.

    Since you hide them all via script, before you do:

    $(".accordion p:not(:first)").hide(); 
    

    maybe you could walk through and get the calculated heights of each piece and add that to each items style, thereby eliminating that "jerk" you get at the end.

    Something along these lines:

    $('.accordion p').each(function(index) {
       $(this).css('height', $(this).height());
    });
    

    Edit

    I went ahead and downloaded a copy of your page and tested this, and it seems to work fine in a few quick browser tests, so here's your revised vaccordian.js:

    $(document).ready(function(){   
        $('.accordion p').each(function(index) {
           $(this).css('height', $(this).height());
        });
    
    
        $(".accordion h3:first").addClass("active");
        $(".accordion p:not(:first)").hide();
    
    
        $(".accordion h3").click(function(){
            $(this).next("p").slideToggle("slow")
            .siblings("p:visible").slideUp("slow");
            $(this).toggleClass("active");
            $(this).siblings("h3").removeClass("active");
        });
    });
    

    TL;DR - by setting an explicit height on each 'opening' part of the accordion, it removes the jerky animation. so we set those heights via script.

提交回复
热议问题