简体   繁体   English

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

[英]How to fix titles to the top of a list when scrolling down?

I'm willing to display a scrollable list divided in 2 categories. 我愿意显示一个分为2类的可滚动列表。 Each category has a title and I'd like these titles to remain visible when I scroll down the list. 每个类别都有一个标题,当我向下滚动列表时,我希望这些标题保持可见。

I know similar questions have been asked and I've tried using scrollTop but it I couldn't make it work within a list. 我知道有人问过类似的问题,我已经尝试过使用scrollTop,但是我无法使其在列表中工作。

Any help is much appreciated. 任何帮助深表感谢。

Simply set the CSS position of the element you want to keep in place to 'fixed'. 只需将要保留的元素的CSS位置设置为“固定”即可。

#fixedDiv{
position:fixed; 
} 

this is what I used in the past. 这是我过去使用的。 it works on the <tag id="containerToFix"> there are a number of VAR's that you may need to play with 它适用于<tag id="containerToFix"> ,您可能需要使用许多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 });
    };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM