有人知道以下问题的解决方案吗?
我试图在一个固定的 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>
最佳答案
您要定义的高度是多少?如果数字太小,您将在循环的每次迭代中创建大量 DOM 元素,这可能会终止浏览器呈现代码。尝试增加每次刷新之间的间隔并降低每次添加的内容。
关于固定的位置,好吧,您是在告诉元素卡住在原地,这样无论您走到哪里,它都可以留在那里。此功能通常用于粘性标题或导航栏,我不明白你为什么要在这里使用它。
关于jquery - 在固定的 div 内无限滚动内容循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32526323/