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

别来无恙 提交于 2019-12-17 16:13:08

问题


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 ...


回答1:


A little birdie told me...

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



回答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




回答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');
  }
});



回答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/




回答5:


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

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



来源:https://stackoverflow.com/questions/8144944/change-slidetoggle-behaviour-to-displayinline-block-instead-of-displayblock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!