45

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
  • 1,427
  • 4
  • 14
  • 30

6 Answers6

63

A little birdie told me...

$('#my-block').slideToggle('medium', function() {
    if ($(this).is(':visible'))
        $(this).css('display','inline-block');
});
Janaka Dombawela
  • 1,337
  • 4
  • 26
  • 49
black
  • 654
  • 6
  • 2
  • 3
    `$('#my-block').slideToggle('medium', function() { if ($(this).is(':visible')) $('this').css('display','inline-block'); });` Minor edit. –  Aug 15 '13 at 22:36
  • 20
    Actually, it's easier to set CSS for _#element_ as `display: inline-block;` (in – K. Weber Nov 06 '13 at 21:57
  • 1
    I know this is pretty necro but could someone explain @K.Weber's comment in a little more depth? – limeandcoconut Jun 30 '14 at 05:27
  • 8
    It means use `display:none` as inline style attribute, this is, ` – K. Weber Jun 30 '14 at 22:26
  • @K.Weber This doesn't work once DOM (modal window, for instance), has been closed with fadeToggle – The Onin Oct 09 '15 at 00:40
  • this didn't work for me, but the solution in @K.Weber's comment worked, wonder if it's cross-browser – Fanky Dec 08 '17 at 15:53
15

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

dantiston
  • 5,161
  • 2
  • 26
  • 30
fliptheweb
  • 1,168
  • 1
  • 6
  • 14
  • 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! – tylerlindell Apr 20 '14 at 02:53
14

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');
  }
});
Eloy Ruiz
  • 739
  • 1
  • 8
  • 14
  • 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! – Abraham Brookes Mar 20 '19 at 04:45
  • Works perfectly. Thank you for this! – GeorgeP Apr 23 '19 at 00:27
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/

Greg Pettit
  • 10,749
  • 5
  • 53
  • 72
2

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

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

Alex Peattie
  • 26,633
  • 5
  • 50
  • 52
0

I suggest the following trick. It will allow you to use a class for "hidden" while avoiding any flashes due to layout change happening prematurely or belatedly.

(using display:flex with direction:row to make it clear that it's displayed as flex and not as block)

function toggle() {
  let elem = $(".component")
  if (elem.hasClass("hidden")) {
    elem.css("display", "none");
    elem.removeClass("hidden");
  }
  elem.slideToggle(200);
}
.component {
  display: flex;
  flex-direction: row;
}

.hidden {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<a href="#" class="toggle" onclick="toggle();">toggle this</a>
<div class="component hidden">
  <div>item1</div>
  <div>item2</div>
</div>
Tomer
  • 610
  • 1
  • 7
  • 12