简体   繁体   English

Bootstrap崩溃-如何添加其他取决于崩溃状态的点击行为

[英]Bootstrap collapse - how to add additional click behavior that depends on the state of collapse or not

I have a button that controls the expansion/collapse of a collapsible div. 我有一个按钮来控制可折叠div的展开/折叠。 But I want to add additional functionality on click that depends on whether the div is expanding or collapsing. 但我想在点击时添加其他功能,具体取决于div是在扩展还是在崩溃。 I wrote this code: 我写了这段代码:

    buttonElement.click(function() {
      if ($(this).hasClass("collapsed")) {
        // do the thing I want to do when expanding
      } else {
        // do the thing I want to do when collapsing
      }
    })

This works, but I'm wondering if it's robust. 这可行,但是我想知道它是否健壮。 It assumes that my click function runs before the data-toggle changes the class, and I'm not sure if it's appropriate to assume this. 它假定我的click函数在数据切换更改类之前运行,并且我不确定是否合适。

The best thing you can do is read Bootstrap documentation :) 最好的办法是阅读Bootstrap文档 :)

Your solution is not the best, because if you double click on your button fast enough it will execute code inside your if statement AND in else statement (with collapsing only once) 您的解决方案不是最好的,因为如果您足够快地双击按钮,它将在if语句和else语句中执行代码(仅折叠一次)

This event fires immediately when the show instance method is called. 调用show实例方法时,将立即触发此事件。

$('#myCollapsible').on('show.bs.collapse', function () {
  // do something…
});

This event is fired when a collapse element has been made visible to the user (will wait for CSS transitions to complete). 当折叠元素对用户可见时将触发此事件(将等待CSS转换完成)​​。

$('#myCollapsible').on('shown.bs.collapse', function () {
  // do something…
});

This event is fired immediately when the hide method has been called. 调用hide方法后,立即触发此事件。

$('#myCollapsible').on('hide.bs.collapse', function () {
  // do something…
});

This event is fired when a collapse element has been hidden from the user (will wait for CSS transitions to complete). 当用户已隐藏折叠元素时将触发此事件(将等待CSS转换完成)​​。

$('#myCollapsible').on('hidden.bs.collapse', function () {
  // do something…
});

Example: 例:

$('[data-toggle="collapse"]').click(function() {
  var collapsibleDiv = $(this).closest('.panel').find('.panel-collapse');
  collapsibleDiv.on('show.bs.collapse', function() {
    $('body').css('background', '#1abc9c');
  });
  collapsibleDiv.on('shown.bs.collapse', function() {
    $('body').css('background', '#e74c3c');
  });
  collapsibleDiv.on('hide.bs.collapse', function() {
    $('body').css('background', '#3498db');
  });
  collapsibleDiv.on('hidden.bs.collapse', function() {
    $('body').css('background', '#7f8c8d');
  });
});

CODEPEN CODEPEN

Your solution is the best for simple tasks that occur more frequently. 对于频繁发生的简单任务,您的解决方案是最好的。

In addition to the collapsed class, you can use a special attribute : 除了collapsed类之外,您还可以使用特殊属性

Be sure to add aria-expanded to the control element. 确保将aria-expanded添加到控件元素。 <...> The plugin will automatically toggle this attribute based on whether or not the collapsible element has been opened or closed. <...>插件将根据可折叠元素是否已打开或关闭来自动切换此属性。

Both of these methods indicate the status of the toggle button, but not the state of the object which is operated by the button. 这两种方法都指示切换按钮的状态,而不指示按钮所操作的对象的状态。 When an object is controlled by a few buttons, you need to look for other signs. 当一个对象由几个按钮控制时,您需要寻找其他标志。

You can use three other classes to check the status of the object itself: 您可以使用其他三个类来检查对象本身的状态:

The collapse plugin utilizes a few classes to handle the heavy lifting: 折叠插件利用一些类来处理繁重的工作:
.collapse hides the content .collapse隐藏内容
.collapse.in shows the content .collapse.in显示内容
.collapsing is added when the transition starts, and removed when it finishes .collapsing在过渡开始时添加,在过渡完成时删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM