2012-05-04 61 views
6

我正在使用航點創建粘貼div的頁面,該頁面的位置向下滾動:fixed,直到達到其父div的底部。代碼im使用按照預期工作,直到調用$ .waypoints('refresh'),然後粘性div跳回頁面頂部並失去其固定位置。Jquery Waypoints刷新

繼承人的代碼:

$('#content').waypoint(function(event, direction){ 
if (direction === 'down') { 
    $(this).removeClass('sticky').addClass('bottomed'); 
} 
else { 
    $(this).removeClass('bottomed').addClass('sticky'); 
} 
}, { 
    offset: function() { 
    return $('#leftCol').outerHeight() - $('#content').outerHeight(); 
} 

}).find('#leftCol').waypoint(function(event, direction) { 
    $(this).parent().toggleClass('sticky', direction === "down"); 
    event.stopPropagation(); 
     });   

哪裏#leftCol是向下滾動頁面,#內容是其母公司股利股利。

CSS的我已經是:

#content { 
width: 975px; 
height: auto; 
position: relative; 
margin-top: 10px; 
margin-bottom: 20px; 
min-height: 800px; 
} 

#leftCol { 
position: absolute; 
width: 974px; 
} 

.sticky #leftCol { 
position:fixed !important; 
top:0 !important; 
left: 50% !important; 
width: 974px !important; 
margin-left: -488px; 
} 

.bottomed #leftCol { 
position: absolute !important; 
bottom: 0px; 
} 

就如何維護#leftCol的位置時$ .waypoints(「刷新」)被稱爲將是非常讚賞的任何想法。

感謝

回答

23

不要,不要,不要老是用fixed位置元素作爲航點。查看GitHub上所有以下已解決的問題:#64#44,#32,#26,#24,#13, #10,#4

這是關於Waypoint的最容易被誤解的事情,絕對是我在傳達Waypoint如何運作的問題上的錯。我計劃在插件的下一次迭代中更清楚地說明這一點。

對於任何人想知道:Waypoints通過查看元素的頁面偏移量來工作。但是當用戶滾動時,固定位置元素的頁面偏移量會不斷變化。所以無論什麼時候調用刷新,手動添加,添加另一個航點,或調整瀏覽器大小,該航點的位置都會隨着用戶在頁面滾動時的位置而變化。你想要的是一個正常的靜態位置元素,不會讓文檔流成爲路點。在Waypoints項目網站上給出的示例中,航點是一個包裝元素,它保持原位,而nav它包裝收益並通過CSS丟失固定定位。對於不懂頁面偏移和CSS的人來說,這很容易做到OP在這裏完成的任務,因爲它看起來很直觀。再次,這將在將來的文檔/示例中更直接地解決。

+0

啊可愛,那就做到了!感謝這麼快的迴應 – user1258303

+0

這給了我一個關於如何解決我的問題的提示。除了窗口大小調整以外,我的一切都工作得很好。 http://stackoverflow.com/questions/18221351/debugging-jquery-waypoints-script-see-fiddle-cannot-find-bug –

+0

再次閱讀我的答案的第一行。你仍然這樣做。你應該讓你的**包裝**成爲航點。 – imakewebthings