2011-10-26 43 views

回答

7

你需要找出標題的高度,並在網頁上的位置,然後只顯示或隱藏取決於使用jQuery的scrollTop的值股利。

例如:

// Get the headers position from the top of the page, plus its own height 
var startY = $('header').position().top + $('header').outerHeight(); 

$(window).scroll(function(){ 
    checkY(); 
}); 

function checkY(){ 
    if($(window).scrollTop() > startY){ 
     $('.fixedDiv').slideDown(); 
    }else{ 
     $('.fixedDiv').slideUp(); 
    } 
} 

// Do this on load just in case the user starts half way down the page 
checkY(); 

然後你只需要在.fixedDiv設置位置:固定:頂部:0;左:0;

編輯:我添加了一個checkY()函數,您可以在頁面加載和滾動時調用該函數。最初要隱藏它,只要使用CSS。

+0

工作就像一個魅力,謝謝! – checkenginelight

+0

對不起,只有一件快事..我怎樣才能阻止它在開始顯示?這裏是我的文件:http://jsfiddle.net/nHnrd/14/ – checkenginelight

+1

啊,是的,看到我的編輯,應該排序! – will

0

您可能希望只顯示和隱藏DIV,而不是僞類,並隱藏和顯示

開始: $(「#mydiv」)隱藏(); 然後(滾動): $(「#mydiv」)。show();

一套你希望你的DIV的樣子,即0,0和固定

使用保持簡單的方法是什麼!