Bas*_*sit 136 javascript ajax jquery scroll
我只是想知道如果div.loading可见,我如何才能在滚动上实现更多数据.
通常我们会查找页面高度和滚动高度,以查看是否需要加载更多数据.但是下面的例子比那复杂一点.
下图是完美的例子.在下拉框中有两个.loading div.当用户滚动内容时,无论哪个可见,它都应该开始为其加载更多数据.
那么如何才能找出.din div是否对用户可见?所以我可以开始只加载该div的数据.
dee*_*ssn 272
在jQuery中,检查是否使用滚动功能点击了页面底部.一旦你点击它,做一个ajax调用(你可以在这里显示一个加载图像直到ajax响应)并获得下一组数据,将它附加到div.当您再次向下滚动页面时,将执行此功能.
$(window).scroll(function() {
if($(window).scrollTop() == $(document).height() - $(window).height()) {
// ajax call get data from server and append to the div
}
});
Run Code Online (Sandbox Code Playgroud)
def*_*u1t 84
你听说过jQuery Waypoint插件吗?
下面是调用waypoints插件并让页面在滚动到达底部时加载更多内容的简单方法:
$(document).ready(function() {
var $loading = $("<div class='loading'><p>Loading more items…</p></div>"),
$footer = $('footer'),
opts = {
offset: '100%'
};
$footer.waypoint(function(event, direction) {
$footer.waypoint('remove');
$('body').append($loading);
$.get($('.more a').attr('href'), function(data) {
var $data = $(data);
$('#container').append($data.find('.article'));
$loading.detach();
$('.more').replaceWith($data.find('.more'));
$footer.waypoint(opts);
});
}, opts);
});
Run Code Online (Sandbox Code Playgroud)
The*_*lal 12
如果不是所有文档都滚动,例如,当您在文档中滚动时div
,则上述解决方案在没有调整的情况下将无法工作。下面是如何检查 div 的滚动条是否已经触底:
$('#someScrollingDiv').on('scroll', function() {
let div = $(this).get(0);
if(div.scrollTop + div.clientHeight >= div.scrollHeight) {
// do the lazy loading here
}
});
Run Code Online (Sandbox Code Playgroud)
这是一个例子:
<!DOCTYPE html>
<html>
<head>
<title>Demo: Lazy Loader</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<style>
#myScroll {
border: 1px solid #999;
}
p {
border: 1px solid #ccc;
padding: 50px;
text-align: center;
}
.loading {
color: red;
}
.dynamic {
background-color:#ccc;
color:#000;
}
</style>
<script>
var counter=0;
$(window).scroll(function () {
if ($(window).scrollTop() == $(document).height() - $(window).height() && counter < 2) {
appendData();
}
});
function appendData() {
var html = '';
for (i = 0; i < 10; i++) {
html += '<p class="dynamic">Dynamic Data : This is test data.<br />Next line.</p>';
}
$('#myScroll').append(html);
counter++;
if(counter==2)
$('#myScroll').append('<button id="uniqueButton" style="margin-left: 50%; background-color: powderblue;">Click</button><br /><br />');
}
</script>
</head>
<body>
<div id="myScroll">
<p>
Contents will load here!!!.<br />
</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
<p >This is test data.<br />Next line.</p>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我建议使用更多 Math.ceil 以避免某些屏幕上出现错误。
因为在几个不同的屏幕上它并不绝对准确,
我意识到当我console.log时。
console.log($(window).scrollTop()); //5659.20123123890
Run Code Online (Sandbox Code Playgroud)
和
console.log$(document).height() - $(window).height()); // 5660
Run Code Online (Sandbox Code Playgroud)
所以我认为我们应该编辑你的代码
$(window).scroll(function() {
if(Math.ceil($(window).scrollTop())
== Math.ceil(($(document).height() - $(window).height()))) {
// ajax call get data from server and append to the div
}
});
Run Code Online (Sandbox Code Playgroud)
或者允许在滚动到底部之前从服务器加载数据。
if ($(window).scrollTop() >= ($(document).height() - $(window).height() - 200)) {
// Load data
}
Run Code Online (Sandbox Code Playgroud)
当窗口放大到> 100%时,这个问题的可接受答案与chrome有关。这是chrome开发人员推荐的代码,作为我在同一个错误中提出的一部分。
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() >= $(document).height()){
//Your code here
}
});
Run Code Online (Sandbox Code Playgroud)
以供参考:
改进@deepakssn 的答案。您可能希望在我们实际滚动到底部之前加载一些数据。
var scrollLoad = true;
$(window).scroll(function(){
if (scrollLoad && ($(document).height() - $(window).height())-$(window).scrollTop()<=800){
// fetch data when we are 800px above the document end
scrollLoad = false;
}
});
Run Code Online (Sandbox Code Playgroud)
[varscrollLoad] 用于阻止调用,直到附加一个新数据。
希望这可以帮助。