我正在尝试以下操作,但根本没有回调
$("#panel").addClass("showPane");
$("#close_wikipedia").on("click", function() {
$("#panel").addClass("close_wiki");
});
if ($("#panel").hasClass("close_wiki")) {
$(this).bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function() {
$("#panel").removeClass("close_wiki showPane");
});
}
#panel {
position: fixed;
background: #444;
height: 100%;
width: 50vw;
right: -100vw;
transition: right 0.4s ease-in-out;
-o-transition: right 0.4s ease-in-out;
-ms-transition: right 0.4s ease-in-out;
-moz-transition: right 0.4s ease-in-out;
-webkit-transition: right 0.4s ease-in-out;
z-index: 9999;
top: 0;
bottom: 0;
height: 100vh;
overflow-y: auto;
}
#panel.showPane {
right: 0;
}
#panel.close_wiki {
right: -100vw;
transition: right 0.4s ease-in-out;
-o-transition: right 0.4s ease-in-out;
-ms-transition: right 0.4s ease-in-out;
-moz-transition: right 0.4s ease-in-out;
-webkit-transition: right 0.4s ease-in-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="panel">
<h2>Wikipedia results</h2>
<div class="panel-group" id="accordionWiki" role="tablist" aria-multiselectable="true"></div>
<button id="close_wikipedia" type="button" class="btn btn-danger">Close Wiki</button>
</div>
最佳答案
问题是因为您仅在页面加载时检查#panel
是否具有close_wiki
类-当单击按钮时添加它时,它将永远不会触发。
而是挂接transitionEnded
事件onload,并在CSS转换完成后等待它触发。另请注意,不建议使用bind()
。您应该改为使用on()
。试试这个:
$("#close_wikipedia").on("click", function() {
$("#panel").removeClass("showPane");
});
#panel {
position: fixed;
background: #444;
height: 100%;
width: 50vw;
right: -100vw;
transition: right 0.4s ease-in-out;
-o-transition: right 0.4s ease-in-out;
-ms-transition: right 0.4s ease-in-out;
-moz-transition: right 0.4s ease-in-out;
-webkit-transition: right 0.4s ease-in-out;
z-index: 9999;
top: 0;
bottom: 0;
height: 100vh;
overflow-y: auto;
}
#panel.showPane {
right: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="panel" class="showPane">
<h2>Wikipedia results</h2>
<div class="panel-group" id="accordionWiki" role="tablist" aria-multiselectable="true"></div>
<button id="close_wikipedia" type="button" class="btn btn-danger">Close Wiki</button>
</div>
还要注意,您可以使逻辑更简单(并且只需切换
transitionEnded
元素上的showPane
类,就根本不需要挂接到#Panel
。$('#panel').addClass('showPane');
$("#close_wikipedia").on("click", function() {
$("#panel").removeClass("showPane");
});
#panel {
position: fixed;
background: #444;
height: 100%;
width: 50vw;
right: -100vw;
transition: right 0.4s ease-in-out;
-o-transition: right 0.4s ease-in-out;
-ms-transition: right 0.4s ease-in-out;
-moz-transition: right 0.4s ease-in-out;
-webkit-transition: right 0.4s ease-in-out;
z-index: 9999;
top: 0;
bottom: 0;
height: 100vh;
overflow-y: auto;
}
#panel.showPane {
right: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="panel">
<h2>Wikipedia results</h2>
<div class="panel-group" id="accordionWiki" role="tablist" aria-multiselectable="true"></div>
<button id="close_wikipedia" type="button" class="btn btn-danger">Close Wiki</button>
</div>