我試圖創建一個div,保持固定頂部時,用戶向下滾動,當他滾動備份,回到原來的位置。如何創建一個在滾動時保持在最前面的酒吧?
我需要這個完全一樣的行爲9GAG提供 - >http://9gag.com/gag/293756
謝謝!
我試圖創建一個div,保持固定頂部時,用戶向下滾動,當他滾動備份,回到原來的位置。如何創建一個在滾動時保持在最前面的酒吧?
我需要這個完全一樣的行爲9GAG提供 - >http://9gag.com/gag/293756
謝謝!
執行以下操作:
$(window)
創建scroll event handler。top: 0
將元素設置爲position: fixed
。您可能還需要調整左側屬性,具體取決於您的佈局。position: static
。你試過了什麼嗎?在http://jsfiddle.net上放一下。如果你卡住了,我會幫忙的。 – Pat
創建完全相同的行爲9gag.com代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- tags html, header, body, divs, anything -->
<div id="post-control-bar" class="spread-bar-wrap">
<div class="spread-bar" style="width:600px">
Facebook button, Twitter button, anything...
</div>
</div>
<script type="text/javascript">
window.onscroll = function()
{
if(window.XMLHttpRequest) {
if (document.documentElement.scrollTop > 173 || self.pageYOffset > 173) {
$('post-control-bar').style.position = 'fixed';
$('post-control-bar').style.top = '0';
} else if (document.documentElement.scrollTop < 173 || self.pageYOffset < 173) {
$('post-control-bar').style.position = 'absolute';
$('post-control-bar').style.top = '';
}
}
}
</script>
<!-- content, footer, anything -->
使用Jquery的航點插件:http://imakewebthings.github.com/jquery-waypoints/
非常容易實現。
如果你需要*確切的*相同的行爲,你檢查了源代碼? – Alex