Javascript 如何在向下滚动时将标题固定在列表顶部?

Javascript 如何在向下滚动时将标题固定在列表顶部?,javascript,jquery,html,Javascript,Jquery,Html,我愿意显示一个分为两类的可滚动列表。 每个类别都有一个标题,我希望在向下滚动列表时这些标题保持可见 我知道有人问过类似的问题,我也尝试过使用scrollTop,但我无法在列表中使用它 非常感谢您的帮助。只需将要保留的元素的CSS位置设置为“固定” #fixedDiv{ position:fixed; } 这是我过去用过的。它在 您可能需要使用一些VAR var scrollLabel = false; var scrollPadding = 40; //height from top of

我愿意显示一个分为两类的可滚动列表。 每个类别都有一个标题,我希望在向下滚动列表时这些标题保持可见

我知道有人问过类似的问题,我也尝试过使用scrollTop,但我无法在列表中使用它


非常感谢您的帮助。

只需将要保留的元素的CSS位置设置为“固定”

#fixedDiv{
position:fixed; 
} 

这是我过去用过的。它在
您可能需要使用一些VAR

var scrollLabel = false;
var scrollPadding = 40; //height from top of page
//use window.scroll NOT document.scroll for IE8, 7, 6
$(window).scroll(function () {

    var bottomScroll = $('.header').offset().top; //container of Tag above
    var maxScrolling = bottomScroll - (maxHeightOfContainerToFix) - (scrollPadding);//(scrollPadding) may not be needed for you
    var startScrolling = $('.ten').offset().top - scrollPadding;
    if ($(window).scrollTop() > startScrolling && $(window).scrollTop() < maxScrolling) {
        $('#containerToFix').css({ 'position': 'fixed', 'top': scrollPadding + 'px' });
        $('#containerToFix').addClass('ie7Fixed');
    }
    else if ($(window).scrollTop() < startScrolling) {
        $('#containerToFix').css({ 'position': '' });
    }
    else if ($(window).scrollTop() > maxScrolling) {
        scrollPosition = maxScrolling - $(window).scrollTop();
        $('#containerToFix').css({ 'top': scrollPosition + scrollPadding + 2 });
    };
});
var scrollabel=false;
var=40//距页面顶部的高度
//对IE8、7、6使用window.scroll而不是document.scroll
$(窗口)。滚动(函数(){
var bottomScroll=$('.header').offset().top;//上面标记的容器
var maxScrolling=bottomScroll-(maxHeightOfContainerToFix)-(scrollPadding);/(scrollPadding)可能不需要
var startScrolling=$('.ten').offset().top-scrollpading;
if($(窗口).scrollTop()>startScrolling&&$(窗口).scrollTop()maxScrolling){
scrollPosition=maxScrolling-$(窗口).scrollTop();
$(#containerToFix').css({'top':scrollPosition+scrollPadding+2});
};
});

您应该发布示例代码,让我们看看您在说什么