Animating inline elements with jQuery

前端 未结 6 512
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 08:40

I am trying to show and hide an inline element (eg a span) using jQuery.

If I just use toggle(), it works as expected but if I use toggle(\"slow\") to give it an ani

6条回答
  •  感动是毒
    2020-12-14 08:59

    toggle() has a bunch of weird things with it, including hiding or transforming odd elements at times. here's a similar solution:

    $('.toggle').click(function() {
      $('.hide').animate({
        'opacity' : 'toggle',
      });
    });
    

    edit: here's a way to add smooth sliding, with minimal extra HTML markup:

    var hidepos = $('.hide').offset().left;
    var slidepos = $('.slide').offset().left;
    
    $('.toggle').click(function() {
        var goto = ($('.slide').offset().left < slidepos) ? slidepos : hidepos;
    
        $('.slide').css({
            'left' : $('.slide').offset().left,
            'position' : 'fixed',
        }).animate({
            'left' : goto,
        }, function() {
            $(this).css('position', 'static');
        });
    
        $('.hide').animate({
            'opacity' : 'toggle',
        });
    });
    

    html:

    Hello there jquery

提交回复
热议问题