我的网页上有一个Bootstrap
collapse panel.我需要在转换完成后面板完全展开后执行一个函数.我找到了这个示例,说明当面板折叠并添加hidden.bs.collapse时如何访问回调.
How to trigger a JavaScript function after “Bootstrap: collapse plugin” transition is done.
How to trigger a JavaScript function after “Bootstrap: collapse plugin” transition is done.
但是,当应用.collapse.in类时,我似乎无法触发类似的代码.
$('#collapseExample').on('.collapse.in',function(){ alert("hi"); });
我还发现这个代码使用promises在切换类后触发函数.它应该等到所有动画完成. jquery function on toggleClass complete?这对我的问题似乎也不起作用.
这段代码尝试了jQuery promise技术.理想情况下,它会在面板完成扩展后添加类.red但会立即触发.
http://www.bootply.com/Vx9oeyYy03#
$(document).on('click','.btn',function(){ $('#collapseExample').collapse("toggle").promise().done(function () { $('.well').addClass('red'); }) });
解决方法
您可以使用显示的动画完成后触发的
event handler
$('#collapseExample').on('shown.bs.collapse',function() { snippet.log("shown"); }).on('show.bs.collapse',function() { snippet.log("show"); });
<!-- Provides the `snippet` object,see http://Meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> <script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.css" /> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap-theme.css" /> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.js"></script> <div id="collapseExample" class="panel-group" role="tablist" aria-multiselectable="true"> <div class="panel panel-default"> <div class="panel-heading" role="tab" id="headingOne"> <h4 class="panel-title"> <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapSEOne" aria-expanded="true" aria-controls="collapSEOne"> Collapsible Group Item #1 </a> </h4> </div> <div id="collapSEOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne"> <div class="panel-body"> Anim pariatur cliche reprehenderit,enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute,non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor,sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica,craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table,raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading" role="tab" id="headingTwo"> <h4 class="panel-title"> <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"> Collapsible Group Item #2 </a> </h4> </div> <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo"> <div class="panel-body"> Anim pariatur cliche reprehenderit,raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading" role="tab" id="headingThree"> <h4 class="panel-title"> <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree"> Collapsible Group Item #3 </a> </h4> </div> <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree"> <div class="panel-body"> Anim pariatur cliche reprehenderit,raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </div> </div> </div> </div>