change slideToggle() behaviour to display:inline-block instead of display:block?

56,394

Solution 1

A little birdie told me...

$('#my-block').slideToggle('medium', function() {
    if ($(this).is(':visible'))
        $(this).css('display','inline-block');
});

Solution 2

Just try to hide your block via scripts (dont display:none via styles)

HTML

<div class="ib">inline-block</div> <a href="#" class="toggle">toggle this</a>

CSS

.ib{
    display:inline-block;
    background:red;
}

JavaScript

$(".ib").hide();
$(".toggle").bind("click", function(){
    $(".ib").slideToggle();
    return false;
})

example

Solution 3

I needed the display:flex for reordering elements with the order property. Changing to display:flex worked but it had to wait for the animation to complete to rearrange the elements, so there was a moment where everything looked clearly disordered.

What did the trick for me was using the start option (see doc):

$("your-selector").slideToggle({
  duration: 200,
  easing: "easeOutQuad",
  start: function() {
    jQuery(this).css('display','flex');
  }
});

Solution 4

If you find yourself seeing an unwanted "Flash of Unstyled Content" you could also use an inline style. The usual wisdom of "don't put style inline" is really meant for your main styling, not for visual effects (JS effects all just add inline styles after all).

Of course, the content won't be seen by JS-disabled search engine spiders, if that's important. If it's not important, you're good!

Update of @fliptheweb's fiddle: http://jsfiddle.net/GregP/pqrdS/3/

Solution 5

Are you on an old version of jQuery? This should have been fixed already, see discussion here:

http://bugs.jquery.com/ticket/2185

Share:
56,394

Related videos on Youtube

iHaveacomputer
Author by

iHaveacomputer

i leave this blank for aesthetic reasons.

Updated on July 09, 2022

Comments

  • iHaveacomputer
    iHaveacomputer almost 2 years

    my target slideToggle() div needs to be display:inline-block instead of display:block when it's open. Is there a way to change the jquery behavior here?

    Edit:

    i'm using jQuery 1.7.0 at the moment. Also, the <div> is initially at display:none and should expand to display:inline-block after a click on a link; but apparently the default state for slideToggle() in this situation is display:block ...

  • iHaveacomputer
    iHaveacomputer over 12 years
    Thanks, but i don't think this bug applies here - see my edit.
  • Alex Peattie
    Alex Peattie over 12 years
    I see. In that case, I recommend @fliptheweb's solution
  • Admin
    Admin almost 11 years
    $('#my-block').slideToggle('medium', function() { if ($(this).is(':visible')) $('this').css('display','inline-block'); }); Minor edit.
  • K. Weber
    K. Weber over 10 years
    Actually, it's easier to set CSS for #element as display: inline-block; (in <style> block or style sheet) and display: none; as style attribute of field. Then, when it toggles to visible it just takes default style defined
  • tylerlindell
    tylerlindell about 10 years
    I really like this answer best, the only issue that I had was using an ID instead of a Class. .hide() works great with classes though! Thank you!
  • limeandcoconut
    limeandcoconut almost 10 years
    I know this is pretty necro but could someone explain @K.Weber's comment in a little more depth?
  • K. Weber
    K. Weber almost 10 years
    It means use display:none as inline style attribute, this is, <div id="to_toggle" style="display:none"> and set a default style in css stylesheet (display:inline-block), this way, .slideToggle(), when the element turns visible, will take default style defined in stylesheet (display:inline-block)
  • The Onin
    The Onin over 8 years
    @K.Weber This doesn't work once DOM (modal window, for instance), has been closed with fadeToggle
  • Fanky
    Fanky over 6 years
    this didn't work for me, but the solution in @K.Weber's comment worked, wonder if it's cross-browser
  • Abraham Brookes
    Abraham Brookes about 5 years
    This worked great for me, although there is a little bit of a content-jump just before the opening animation finishes. Much more desirable than having all my slides animate closed on page load though, +1!
  • GeorgeP
    GeorgeP about 5 years
    Works perfectly. Thank you for this!