2012-11-20 36 views
0

我願意展示分爲2類的可滾動列表。 每個類別都有一個標題,我希望這些標題在向下滾動列表時保持可見。如何在向下滾動時將標題固定到列表頂部?

我知道類似的問題已被問及我試過使用scrollTop,但它無法在列表中工作。

任何幫助,非常感謝。

+1

你應該張貼的示例代碼讓我們看到你在說什麼 –

回答

1

只需將要保留的元素的CSS位置設置爲「固定」即可。

#fixedDiv{ 
position:fixed; 
} 
0

這是我以前用過的東西。它的工作原理上<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 }); 
    }; 
}); 
相關問題