我设计了一个包含Bootstrap 4 Beta的页面。我有一个导航栏(固定顶部),其中包含一个下拉菜单。我想在滚动页面时切换此菜单,因此它会产生以下行为:
// Closing or opening Dropdown, when window is scrolled
$(window).scroll(function() {
if ($("#mobile-only").is(':hidden')) {
if ($(window).scrollTop() === 0) {
$("#example-dropdown").dropdown('toggle');
}
}
});

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
<nav class="navbar fixed-top navbar-expand-sm navbar-dark bg-primary">
<!--...-->
<ul class="navbar-nav">
<li class="nav-item">
<div id="example-dropdown" class="dropdown">
<a id="example-dropdown-link" class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu" aria-labelledby="example-dropdown-link">
<a class="dropdown-item" href="#">Item1</a>
<a class="dropdown-item" href="#">Item2</a>
</div>
</div>
</li>
</ul>
</nav>
<div id="mobile-only" class="row d-sm-none">
<!--...-->
</div>
<div style="height:1000px;">
<p>Dummy Container to generate scrollbar</p>
</div>
&#13;
修改:我添加了我的方法。它可以运行。