私のページで無限にスクロールするための次の機能があります。 if文をトリガーするページに移動すると、それ以降のすべてのページがスクロールハンドラーをトリガーし続けます。私はスクロールするたびに 'ウィンドウスクロール'をコンソールに表示するが、 'if statement true'をトリガーしないので、これを知っている。 window.scrollは#infinite-scrolling
のページでのみ動作するようにするにはどうすればよいですか?window.scrollハンドラが他のページに永続することを防止する
function initPagination() {
var paginate = function(buttonPress) {
var url = $('.pagination .next_page').attr('href');
if (buttonPress || (url && $(window).scrollTop() > $(document).height() - $(window).height() - 60)) {
// TODO - Removing this line was causing duplicates to load so it's worth investigating the cause
$('.pagination').html('Please Wait...');
return $.getScript(url);
}
};
if ($('#infinite-scrolling').length) {
console.log('if statement true');
$(window).scroll(function() {
console.log('window scroll');
paginate(false);
});
return $(window).scroll();
}
$('#infinite-scrolling-button').on('click', function() {
$(this).html('Loading...');
paginate(true);
$(this).html('Load more');
});
}
編集:問題の一部は、ターボリンクに関連している可能性があります。私がページをリフレッシュするとき、スクロールハンドラはもはやトリガしていないようです。
Turbolinksはページを「
」に挿入します。つまり、すべてのイベントリスナーが保持されます。 –@ChrisGは、問題がturbolinksのように聞こえます。どうすれば回避できますか?何か案は? – user4584963