jQuery slide is jumpy

前端 未结 27 1730
悲&欢浪女
悲&欢浪女 2020-12-07 16:52

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:

相关标签:
27条回答
  • 2020-12-07 17:22

    There are a lot of suggestions here and a lot of back and forth as to what works. For me, the behavior problem was when the animation of expanding the container would over expand and then bounce back to the correct expansion height (all done as part of the one animation). In way of example, the animation would expand to a height of 500px initially and then retract to 450px. There was no problem with collapse.

    The solution that worked was to add to the expanding/collapsing div, a CSS of: white-space: nowrap;

    That worked perfectly - smooth expansion to the correct height.

    0 讨论(0)
  • 2020-12-07 17:23

    You could write a custom animation using the animate method. This will give you absolute control over all details.

    0 讨论(0)
  • 2020-12-07 17:23

    I just learned that this problem can also occur if there are floated elements in the expanding/collapsing element. In that case, a clearfix (clear: both;) at the end (still within) the animated element can get rid of it.

    0 讨论(0)
  • 2020-12-07 17:24

    Adding my solution: turned out my issue was flexbox (only in chrome). I had align-items: baseline; set on the parent element. Set align-self: center; to my slideToggling full-width child element and it cleared it right up. Great use of two hours.

    0 讨论(0)
  • 2020-12-07 17:26

    I find animate() is the most reliable way to animate anything in jQuery (cross browser at least).

    This dynamically wraps the content in a div, then animates the height of that div wrapper by using the height of its inner content.

    http://jsfiddle.net/BmWjy/13/

    $('a').click(function(event) {
            event.preventDefault();
            xToggleHeight($(this).next());
    });
    
    //For each collapsible element.
    $('.collapsible').each(function() {
            //Wrap a div around and set to hidden.
            $(this).wrap('<div style="height:0;overflow:hidden;visibility:hidden;"/>');
    });
    
    function xToggleHeight(el){
            //Get the height of the content including any margins.
            var contentHeight = el.children('.collapsible').outerHeight(true);
            //If the element is currently expanded.
            if(el.hasClass("expanded")){
                    //Collapse
                    el.removeClass("expanded")
                            .stop().animate({height:0},5000,
                            function(){
                                    //on collapse complete
                                    //Set to hidden so content is invisible.
                                    $(this).css({'overflow':'hidden', 'visibility':'hidden'});
                            }
                    );
            }else{
                    //Expand
                    el.addClass("expanded").css({'overflow':'', 'visibility':'visible'})
                            .stop().animate({height: contentHeight},5000,
                            function(){
                                    //on expanded complete
                                    //Set height to auto incase browser/content is resized afterwards.
                                    $(this).css('height','');
                            }
                    );
            }
    }
    
    0 讨论(0)
  • 2020-12-07 17:26

    Make sure you don't have CSS transition rules set globally or on your container or any included elements. It will also cause jerkiness.

    0 讨论(0)
提交回复
热议问题