【发布时间】:2013-12-23 19:46:23
【问题描述】:
我的网站有一种搜索框 div,当用户向下滚动查看数据时,我想将它放在顶部。虽然当我向下滚动时,div 保持在顶部,但一切都变形了。
编辑:我没有为 div 使用任何额外的 CSS。它是 jumbotron 类 div 内的 Bootstrap 容器类 div。
Here is what it ends up looking like.
<div class="container" id="searcher" style="text-align: center;">
<h2>Where Do You Want To Go?</h2>
<input type="text" class="typeahead form-control"></input>
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" id="pType" data-toggle="dropdown">
Select Permit Type
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="#">Every Type</a></li>
<li><a href="#">Commuter Zone A</a></li>
<li><a href="#">Commuter Zone B</a></li>
<li><a href="#">Commuter Zone C</a></li>
<li><a href="#">Commuter Zone D</a></li>
<li><a href="#">Commuter Zone L</a></li>
<li><a href="#">Night Commuter</a></li>
<li><a href="#">Resident Permits</a></li>
<li><a href="#">Newark and Camden Permits (L)</a></li>
</ul>
</div>
</div>
我正在使用的代码:
function fixDiv() {
var $cache = $('#searcher');
if ($(window).scrollTop() > 100)
$cache.css({'position': 'fixed', 'top': '10px'});
else
$cache.css({'position': 'relative', 'top': 'auto'});
}
$(window).scroll(fixDiv);
fixDiv();
【问题讨论】:
-
能否添加相关的 CSS 代码?
-
真的没有任何其他的CSS。它只是 jumbotron 类 div 中的一个 Bootstrap 容器类 div。