Bootstrap 完成响应式导航栏的过渡动画的方式与折叠时相同,即使用 CSS3 过渡。要仅关闭导航栏的转换(保留任何其他转换),您只需覆盖 CSS。
我建议在您的可折叠容器中添加一个类,例如 no-transition(但名称可以是任意的)
<div class="nav-collapse no-transition">
然后定义一个 CSS 规则,该规则将禁用 Bootstrap 定义的 CSS3 过渡(确保您的 CSS 规则在 bootstrap.css 之后被解析):
.no-transition {
-webkit-transition: height 0;
-moz-transition: height 0;
-ms-transition: height 0;
-o-transition: height 0;
transition: height 0;
}
但是,不要止步于此! Bootstrap 的 JavaScript 是硬编码的,它假定如果浏览器支持转换,就会发生转换。如果您只是进行上述更改,您会发现可折叠对象在一个打开/关闭周期后“锁定”,因为它仍在等待浏览器的转换结束事件触发。有两种不太理想的解决方法:
第一个选项:破解 bootstrap-collapse.js 以不等待过渡结束事件。乱用 Bootstrap 从来都不是一个好主意,因为它会让未来的更新对你来说很痛苦。这种特殊的解决方法也需要类似地应用于您希望向其传递 no-transition 类的任何其他 Bootstrap JavaScript 组件。
bootstrap-collapse.js:107
this.$element.hasClass('no-transition') || (this.transitioning = 1);
第二个推荐选项:使用超短过渡时间而不是禁用过渡。这并没有完全消除您所要求的过渡动画,但它实现了类似的结果,可能对您的低功耗移动设备的性能没有明显的负面影响。这种方法的好处是您不必破解任何 Bootstrap JS 文件,并且您可以将no-transition 应用于任何元素,而不仅仅是崩溃!
.no-transition {
-webkit-transition: height 0.01s;
-moz-transition: height 0.01s;
-ms-transition: height 0.01s;
-o-transition: height 0.01s;
transition: height 0.01s;
}