2015-09-11 62 views
0

有沒有人知道以下問題的解決方案?無限滾動內容循環內的固定格

我試圖做一個固定div內的內容循環的無限滾動, 我已經修改了這個代碼......但是當我試圖讓「.content」div固定並給他一個定義的高度時,它會殺死始終是整個代碼。

感謝

$(function() { 
 
    // every 1 seconds, check + update 
 
    setInterval(appendContent, 400); 
 
    setInterval(continueScrolling, 100); 
 
}); 
 

 
var b = $('.content'); 
 

 
// if almost at bottom, append more content 
 
function appendContent() { 
 
    if($(window).scrollTop() + $(window).height() * 2 > $(".content").height()) { 
 
     b.html(b.html() + b.html()); // double content 
 
    }  
 
} 
 

 
// continue scrolling linearly 
 
function continueScrolling() { 
 
    y = $(window).scrollTop(); 
 
    $("html, body").stop().animate({ 
 
     scrollTop: y+1000 
 
    }, { 
 
     duration: 10000, 
 
     easing: 'linear', 
 
     queue: false 
 
    }); 
 
} 
 

 

 
var page = $("html, body"); 
 

 
$(function() { 
 

 
    page.on("scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove", function(){ 
 
     page.stop(); 
 
    }); 
 

 
    page.animate({ scrollTop: $(this).position().top }, 'slow', function(){ 
 
     page.off("scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove"); 
 
    }); 
 

 
    return false; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="content"> 
 
============================<br> 
 
test1<br> 
 
test2<br> 
 
test3<br> 
 
test4<br> 
 
test5<br> 
 
============================<br> 
 
test1<br> 
 
test2<br> 
 
test3<br> 
 
test4<br> 
 
test5<br> 
 
============================<br> 
 
test1<br> 
 
test2<br> 
 
test3<br> 
 
test4<br> 
 
test5<br> 
 
============================<br> 
 
test1<br> 
 
test2<br> 
 
test3<br> 
 
test4<br> 
 
test5<br> 
 
============================<br> 
 
test1<br> 
 
test2<br> 
 
test3<br> 
 
test4<br> 
 
test5<br> 
 
============================<br> 
 
test1<br> 
 
test2<br> 
 
test3<br> 
 
test4<br> 
 
test5<br> 
 
============================<br> 
 
test1<br> 
 
test2<br> 
 
test3<br> 
 
test4<br> 
 
test5<br> 
 
============================<br> 
 
test1<br> 
 
test2<br> 
 
test3<br> 
 
test4<br> 
 
test5<br> 
 
============================<br> 
 
test1<br> 
 
test2<br> 
 
test3<br> 
 
test4<br> 
 
test5<br> 
 
============================<br> 
 
test1<br> 
 
test2<br> 
 
test3<br> 
 
test4<br> 
 
test5<br> 
 
</div>

回答

0

是你要定義什麼高度?如果數量太小,您將在循環的每次迭代中創建大量的DOM元素,這可能會導致代碼的瀏覽器死掉。嘗試增加每次刷新之間的時間間隔並降低每次添加的內容。

關於固定的位置,好吧,您告訴元素凍結到位,以便它可以隨時停留在那裏。這個函數通常用於粘滯標題或導航欄,我不明白你爲什麼要在這裏使用它。