jQuery Animation - Smooth Size Transition

前端 未结 8 2175
天命终不由人
天命终不由人 2020-11-28 07:30

So this might be really simple, but I haven\'t been able to find any examples to learn off of yet, so please bear with me. ;)

Here\'s basically what I want to do:

相关标签:
8条回答
  • 2020-11-28 07:59

    You can smooth out the jQuery animation using dequeue. Test for presence of class (set upon hover and removed on mouseOut animate callback) before staring new animation. When new animation does start, dequeue.

    Here's a quick demo.

    var space = ($(window).width() - 100);
    $('.column').width(space/4);
    
    $(".column").click(function(){
        if (!$(this).hasClass('animated')) {
            $('.column').not($(this).parent()).dequeue().stop().animate({width: 'toggle', opacity: '0.75'}, 1750,'linear', function () {});
        }
    
      $(this).addClass('animated');
        $('.column').not($(this).parent()).dequeue().stop().animate({width: 'toggle', opacity: '0.75'}, 1750,'linear', function () {
              $(this).removeClass('animated').dequeue();
    
          });
        $(this).dequeue().stop().animate({
            width:(space/4)
        }, 1400,'linear',function(){
          $(this).html('AGAIN');
        });
    });
    

    The demo is setup as 5 full-height columns, clicking any of the columns 2 thru 5 will animate toggle width of the other 3 and move the clicked element to the far left.

    enter image description here

    enter image description here

    0 讨论(0)
  • 2020-11-28 08:03

    maybe something like this?

    $(".testLink").click(function(event) {
        event.preventDefault();
        $(".testDiv").hide(400,function(event) {
            $(this).html("Itsy-bitsy bit of content!").show(400);
        });
    });
    

    Close to what I think you wanted, also try slideIn/slideOut or look at the UI/Effects plugin.

    0 讨论(0)
  • 2020-11-28 08:05

    Hello meyahoocoma4c5ki0pprxr19sxhajsogo6jgks5dt.

    You could wrap the 'content div' with an 'outer div' which is set to an absolute width value. Inject the new content with a "hide()" or "animate({width})" method, shown in the other answers. This way, the page doesn't reflow in between because the wrapper div holds a steady width.

    0 讨论(0)
  • 2020-11-28 08:09

    To piggy-back on the jquery plugin solution (too low of reputation to add this as a comment) jQuery.html() will remove any event handlers on the appended html. Changing:

    // Modify the element's contents. Element will resize.
    el.html(html);
    

    to

    // Modify the element's contents. Element will resize.
    el.append(html);
    

    will retain the event handlers of the "html" elements

    0 讨论(0)
  • 2020-11-28 08:10

    Here is how I fixed this, I hope this will be usefull ! The animation is 100% smooth :)

    HTML:

    <div id="div-1"><div id="div-2">Some content here</div></div>
    

    Javascript:

    // cache selectors for better performance
    var container = $('#div-1'),
        wrapper = $('#div-2');
    
    // temporarily fix the outer div's width
    container.css({width: wrapper.width()});
    // fade opacity of inner div - use opacity because we cannot get the width or height of an element with display set to none
    wrapper.fadeTo('slow', 0, function(){
        // change the div content
        container.html("<div id=\"2\" style=\"display: none;\">new content (with a new width)</div>");
        // give the outer div the same width as the inner div with a smooth animation
        container.animate({width: wrapper.width()}, function(){
            // show the inner div
            wrapper.fadeTo('slow', 1);
        });
    });
    

    There might be a shorter version of my code, but I just kept it like this.

    0 讨论(0)
  • 2020-11-28 08:10

    This does the job for me. You can also add a width to the temp div.

    $('div#to-transition').wrap( '<div id="tmp"></div>' );
    $('div#tmp').css( { height: $('div#to-transition').outerHeight() + 'px' } );
    $('div#to-transition').fadeOut('fast', function() {
      $(this).html(new_html);
      $('div#tmp').animate( { height: $(this).outerHeight() + 'px' }, 'fast' );
      $(this).fadeIn('fast', function() {
        $(this).unwrap();
      });
    });
    
    0 讨论(0)
提交回复
热议问题