有几个插件可以很容易地做到这一点。
给这个一杆----->http://viget.com/inspire/jquery-stick-em
演示在这里:----->http://davist11.github.io/jQuery-Stickem/
我目前使用这种硬代码来完成类似的事情,所以这可能是使用还有:
var $window = $(window),
$mainMenuBar = $('#fixed-div'), //This div will scroll until top
$menuBarOffset = $mainMenuBar.offset().top,
window_top = 0,
footer_offset = $("#end-div").offset().top, //this div tells #fixed-div when to start scrolling
content = $("#unaffected-div"), //This div scrolls like normal
panel_height = $mainMenuBar.outerHeight()+'px';
$window.scroll(function() {
window_top = $window.scrollTop();
if (window_top >= $menuBarOffset) {
if (window_top >= footer_offset) {
$mainMenuBar.removeClass('stick');
content.css('margin-top', 0);
} else {
$mainMenuBar.addClass('stick');
content.css('margin-top', panel_height);
}
}
else {
$mainMenuBar.removeClass('stick');
content.css('margin-top', 0);
}
});
您还需要将此元素添加到您的.css文件
.stick {
position: fixed;
top: 0;
}
感谢您的意见。我尝试过使用stick-em,但没有运气。我使用基金会作为框架工作,它禁用容器上的定位。该脚本被加载,但它不是,但它不激活类更改。我会更多地和它一起玩。如果我无法弄清楚,我会举个例子。再次感谢您的回复。 – Cpawl 2014-09-25 19:54:53
这里是一个例子 - 这么简单但不知道问题可能是什么http://cpawl.com/test/scroll/ – Cpawl 2014-09-25 20:24:01
stickem不工作的原因是因为它没有正确加载插件脚本。它看起来像你还没有加载任何jQuery库 包括此为在你的文档中的第一个脚本链接,看看你是否有更好的结果 – 2014-09-25 21:04:33