2016-04-08 26 views
1

我一直在嘗試一些東西,但無法讓它工作。我在右欄中有一個div,我希望它在滾動時隨頁面浮動到某個點。保持div類與頁面一起浮動的問題

<div class="stickyDiv"> 
<div class="section mod-sticky" style="position: fixed; top: 70px;"> 
<p>Always float with page</p> 
</div> 
</div> 

有沒有簡單的方法來做到這一點與jQuery?或者只是CSS?

做了一個快速的jsfiddle我想要什麼,只要它達到類contentb它將與頁面滾動

https://jsfiddle.net/agwrbjh8/1/

感謝

+2

你可以分享一個現場'jsfiddle'演示嗎? – Pedram

+0

你需要添加一個$(window).on(「scroll」)函數,並且你需要檢查'$(document).scrollTop()'你想要的任何值 - 你提到的某一點。 –

+0

https://jsfiddle.net/agwrbjh8/1/ –

回答

1

我不知道我的理解,你是。你的意思是這樣的:

.mod-sticky { 
 
    position: fixed; 
 
    top: 70px; 
 
    border: 1px solid green; 
 
    right: 0; 
 
}
<div class="stickyDiv"> 
 
    <div class="section mod-sticky"> 
 
    <p>floater</p> 
 
    </div> 
 
    dasf asd asdf asdf 
 
    <p style="height:800px">Lorem... long text</p> 
 
</div>

+0

'float'不適用於'fixed'元素。所以我刪除了它。還有那些200'
'正在扼殺我早上的咖啡...... –

+0

哈!謝謝...... br只是用於滾動視覺 – ochi

+2

所以是'

'; =) –

0

我會看着sticky-kit jQuery plugin

而且你會使用它像這樣:

$('.stickyDiv').stick_in_parent({ 
    parent: $('.content'), 
    offset_top: 70 
}); 

JSFIDDLE DEMO

不得不調整你的HTML,這樣.stickyDiv是你打算內貼內容DIV的孩子,然後還添加一個margin-left以使其位於內容區域的右側。可能不適合你的情況,但我不知道如何讓它堅持你想要的原始標記。