<div class="top-head>some content</div>
<div class="b_head">this div will stay at top as user scrolls</div>
.b_head {
height: auto;
z-index: 12;
position: relative;
display: block;}
<script type='text/javascript'>
$(document).load(function() {
$window = $(window),
$sidebar = $(".b_head "),
sidebarTop = $sidebar.position().top,
$sidebar.addClass('fixed');
$window.scroll(function(event) {
scrollTop = $window.scrollTop(),
topPosition = Math.max(0, sidebarTop - scrollTop),
$sidebar.css('top', topPosition);
});
});
</script>
上面是我的代码,试图让 .b_head div 在用户滚动页面时浮动在顶部(类似于 9gag.com 浮动标题),但它不起作用,谁能帮帮我。
如果我改变“位置:固定;” 对于 b_head,那么它和页面顶部之间会有空白空间,因为它上面还有另一个 div。