将slideToggle() 行为更改为display:inline-block 而不是display:block?

发布于 2024-12-16 17:37:47 字数 393 浏览 4 评论 0原文

我的目标 slideToggle() div 需要是 display:inline-block 而不是 display :block 当它打开时。有没有办法改变 jquery 的行为?

编辑:

我现在正在使用 jQuery 1.7.0。此外,

最初位于 display:none,单击链接后应扩展为 display:inline-block;但显然在这种情况下,slideToggle() 的默认状态是 display:block ...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

过期以后 2024-12-23 17:37:47

一只小鸟告诉我...

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

A little birdie told me...

$('#my-block').slideToggle('medium', function() {
    if ($(this).is(':visible'))
        $(this).css('display','inline-block');
});
皇甫轩 2024-12-23 17:37:47

只需尝试通过脚本隐藏您的块(不要通过样式 display:none

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;
})

示例

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

淡看悲欢离合 2024-12-23 17:37:47

我需要 display:flex 来使用 order 属性对元素进行重新排序。
更改为 display:flex 有效,但必须等待动画完成才能重新排列元素,因此有一段时间一切看起来都明显混乱。

对我来说,窍门是使用启动选项(参见文档):

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

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');
  }
});
长梦不多时 2024-12-23 17:37:47

如果您发现自己看到不需要的“Flash of Unstyled Content”,您也可以使用内联样式。通常的智慧“不要将样式内联”实际上是针对您的主要样式,而不是视觉效果(毕竟JS效果都只是添加内联样式)。

当然,如果内容很重要的话,禁用 JS 的搜索引擎蜘蛛将看不到该内容。如果这不重要的话,你就很好了!

@fliptheweb 的小提琴更新: http://jsfiddle.net/GregP/pqrdS/3/

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/

独自唱情﹋歌 2024-12-23 17:37:47

您使用的是旧版本的 jQuery 吗?这个问题应该已经修复了,请参阅此处的讨论:

http://bugs.jquery.com/ticket/2185< /a>

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

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

倾城°AllureLove 2024-12-23 17:37:47

我建议使用以下技巧。它将允许您使用“隐藏”类,同时避免由于过早或延迟发生布局更改而导致的任何闪烁。

(使用display:flex和direction:row来明确它显示为flex而不是块)

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>

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文