我正在使用Coldfusion开发网站,想要设置无限滚动。由于我对Javascript和JQuery不太熟悉,所以在理解这些方面遇到了一些问题。是否需要在我的网站上添加分页才能使用无限滚动插件,或者有没有其他方法可以实现呢?
我正在使用Coldfusion开发网站,想要设置无限滚动。由于我对Javascript和JQuery不太熟悉,所以在理解这些方面遇到了一些问题。是否需要在我的网站上添加分页才能使用无限滚动插件,或者有没有其他方法可以实现呢?
你不需要无限滚动插件来实现这个功能。使用jQuery可以检测滚动条是否到达页面底部,如下:
$(window).scroll(function () {
if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
//Add something at the end of the page
}
});
我正在使用Hussein的回答进行AJAX请求。我修改了代码,将其触发点从10px改为300px,但由于在300px范围内滚动调用频率比10px范围内更高,所以它开始导致我的附加内容倍增而AJAX请求尚未完成。
为了解决这个问题,我添加了一个在成功加载AJAX时会被触发的触发器。我的代码看起来更像是这样:
var scrollLoad = true;
$(window).scroll(function () {
if (scrollLoad && $(window).scrollTop() >= $(document).height() - $(window).height() - 300) {
scrollLoad = false;
//Add something at the end of the page
}
});
然后在我的 AJAX 响应中,我将 scrollLoad
设置为 true
。
我在Hussein的示例基础上构建了一个jQuery小部件。 它支持localStorage以临时保存附加的结果,并具有暂停功能,以便每隔一段时间停止附加,需要单击才能继续。
试试吧:
$(function(){
$(window).scroll(function(){
if($(document).height()<=$(window).scrollTop()+$(window).height()+100){
alert('end of page');
}
});
});
有人要求解释,所以这里是解释:
$(document).height()-->是整个文档的高度。在大多数情况下,这等于当前文档的 元素。
$(window).height()-->是窗口(浏览器)的高度,表示在浏览器中看到的任何内容的高度。
$(window).scrollTop()-->Element.scrollTop 属性获取或设置元素向上滚动的像素数。元素的 scrollTop 是元素顶部到其最顶部可见内容的距离的测量值。当元素内容不生成垂直滚动条时,其 scrollTop 值默认为 0。
$(document).height()<=$(window).scrollTop()+$(window).height()+100
将 $(window).scrollTop() 与 $(window).height() 相加,现在检查结果是否等于您的文档高度。如果相等,表示您已经到达了底部。我们添加了100,因为我想在离文档底部100像素之前进行检查(请注意条件中的 <=)。
如果我有错误,请纠正我。
我曾经遇到过同样的问题,但没有找到适合我需求的插件。因此,我编写了以下代码。这段代码通过使用ajax和分页获取数据,并将模板附加到元素上。 为了检测用户何时滚动到div底部,我使用了以下条件:
var t = $("#infiniteContent").offset().top;
var h = $("#infiniteContent").height();
var ws = $(window).scrollTop();
var dh = $(document).height();
var wh = $(window).height();
if (dh - (wh + ws) < dh - (h + t)) {
//now you are at bottom of #infiniteContent element
}
$(document).ready(function(){
$.getJSON("https://jsonplaceholder.typicode.com/comments", { _page: 1, _limit:3 }, function (jsonre) {
appendTemplate(jsonre,1);
});
});
function appendTemplate(jsonre, pageNumber){
//instead of this code you can use a templating plugin like "Mustache"
for(var i =0; i<jsonre.length; i++){
$("#infiniteContent").append("<div class='item'><h2>"+jsonre[i].name+"</h2><p>"+jsonre[i].body+"</p></div>");
}
if (jsonre.length) {
$("#infiniteContent").attr("data-page", parseInt(pageNumber)+1);
$(window).on("scroll", initScroll);
//scroll event will not trigger if window size is greater than or equal to document size
var dh = $(document).height() , wh = $(window).height();
if(wh>=dh){
initScroll();
}
}
else {
$("#infiniteContent").attr("data-page", "");
}
}
function initScroll() {
var t = $("#infiniteContent").offset().top;
var h = $("#infiniteContent").height();
var ws = $(window).scrollTop();
var dh = $(document).height();
var wh = $(window).height();
if (dh - (wh + ws) < dh - (h + t)) {
$(window).off('scroll');
var p = $("#infiniteContent").attr("data-page");
if (p) {
$.getJSON("https://jsonplaceholder.typicode.com/comments", { _page: p, _limit:3 }, function (jsonre) {
appendTemplate(jsonre, p);
});
}
}
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<div id="infiniteContent"></div>
$(function () {
var s = $(".your-scrollable-element");
var list = $("#your-table-list");
/* On element scroll */
s.scroll(function () {
/* The scroll top plus element height equals to table height */
if ((s.scrollTop() + s.height()) == list.height()) {
/* you code */
}
});
});
<div id="feed"></div>
js
var infScroll = infiniteScroll({
cb: function () {
return doSomethingPossiblyAnAJAXPromise();
}
});
如果您想暂时停止该源,可以在cb方法中返回false。如果您已经到达了该源的末尾,则此方法非常有用。您可以通过调用infiniteScroll返回的对象方法“setShouldLoad”,并传入true和示例来重新启动它。
infScroll.setShouldLoad(true);
无限滚动的函数是这个
function infiniteScroll (options) {
// these options can be overwritten by the sent in options
var defaultOptions = {
binder: $(window), // parent scrollable element
loadSpot: 300, //
feedContainer: $("#feed"), // container
cb: function () { },
}
options = $.extend(defaultOptions, options);
options.shouldLoad = true;
var returnedOptions = {
setShouldLoad: function (bool) { options.shouldLoad = bool; if(bool) { scrollHandler(); } },
};
function scrollHandler () {
var scrollTop = options.binder.scrollTop();
var height = options.binder[0].innerHeight || options.binder.height();
if (options.shouldLoad && scrollTop >= (options.binder[0].scrollHeight || $(document).height()) - height - options.loadSpot) {
options.shouldLoad = false;
if(typeof options.cb === "function") {
new Promise(function (resolve) {resolve();}).then(function() { return options.cb(); }).then(function (isNotFinished) {
if(typeof isNotFinished === "boolean") {
options.shouldLoad = isNotFinished;
}
});
}
}
}
options.binder.scroll(scrollHandler);
scrollHandler();
return returnedOptions;
}