Javascript 什么可能导致无限滚动过早加载?
我正在使用Paul Irish的类无限滚动插件。我的基本HTML结构如下所示:Javascript 什么可能导致无限滚动过早加载?,javascript,jquery,Javascript,Jquery,我正在使用Paul Irish的类无限滚动插件。我的基本HTML结构如下所示: <div class="container"> <div class="track"> a bunch of other html </div> <div class="track"> a bunch of other html </div> 一堆其他的html 一堆其他的html 我有100首歌,每首歌10人一组。对分页的调用工作正常,因此我不包括该
<div class="container">
<div class="track">
a bunch of other html
</div>
<div class="track">
a bunch of other html
</div>
一堆其他的html
一堆其他的html
我有100首歌,每首歌10人一组。对分页的调用工作正常,因此我不包括该代码。以下是我的无限滚动jQuery代码:
jQuery(function($) {
$('.charts-container').infinitescroll({
navSelector: '.navigation',
nextSelector: '.next-page',
itemSelector: '.track',
loading: { img: "<?php echo get_template_directory_uri() . '/images/ajax-loader-spinner.gif'; ?>",
msgText: '',
finishedMsg: ''
}
}, function(arrayOfNewElems) {
// bind newly generated audio elements -- media events don't bubble
$('audio').on('ended', function(e) {
$(e.currentTarget).prev('.play-button').find('.pause-img').addClass('js-hidden');
$(e.currentTarget).prev('.play-button').find('.play-img').removeClass('js-hidden');
});
});
jQuery(函数($){
$('.charts container').infinitescroll({
导航选择器:'.navigation',
下一个选择器:'.下一页',
itemSelector:“.track”,
正在加载:{img:“”,
msgText:“”,
finishedMsg:“”
}
},函数(ArrayOfNewLems){
//绑定新生成的音频元素--媒体事件不冒泡
$('audio')。在('end',函数(e)上{
$(e.currentTarget).prev('.play button').find('.pause img').addClass('js-hidden');
$(e.currentTarget).prev('.play button').find('.play img').removeClass('js-hidden');
});
});
这是可行的。它获取正确的数据并正确填充页面,顺利执行回调。问题是,当我开始在页面上滚动时,加载就开始了,因此,在我甚至还远未接近页面底部之前,已经加载了整整20首曲目,其余的曲目随后会很快加载
这就好像是有什么东西触发了无限滚动,因为它肯定不是我在屏幕上的位置。当页面加载时,容器的底部不可见。是什么触发了这一点?不是回调,因为我最近才添加了回调,我以前遇到过这些问题。你可以稍微修改一下e并尝试以下方法:
$(window).scroll(function() {
if($(window).offset().bottom < 200) {
$(".charts-container").DoSomething//ETC...
}
});
$(窗口)。滚动(函数(){
如果($(窗口).offset().bottom<200){
$(“.charts container”).DoSomething//等。。。
}
});
只有当视口距离底部200px时,此函数才会启动。尝试使用$(document)。ready()?它可能在呈现所有页面元素之前加载。不幸的是,没有骰子:-(我有同样的问题,您是否解决过此问题?offset()。底部似乎不是真实的东西。即使我尝试这样做(我确实这么做了,我想我使用了window.offset()和window.height()之类的工具),但是,它只适用于第一轮延迟加载。在第一轮加载之后,我又遇到了同样的问题,即在加载所有内容时都没有滚动到底部。可能是一个我对插件不熟悉的错误。