当用户向下滚动到屏幕底部时,我想进行Ajax调用。出于某种原因,我的代码不起作用。我认为这可能是一个语法错误,但我检查了我的控制台,我没有看到错误。欢迎提出任何建议,改进或实例。
<div id="content">
<?php include('post-1.php'); ?>
</div>
<div id="loading-image" style="display: none;"></div>
<div id="part2"></div>
<script>
window.onscroll = function(ev) {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
$('#loading-image').show();
$.ajax({
url: "<?php echo get_stylesheet_directory_uri();?>/post-2.php",
cache: false,
success: function(html){
$("#part2").html(result);
},
complete: function(){
$('#loading-image').hide();
}
});
}
};
</script>
答案 0 :(得分:1)
您可以使用this.Works here
function CallAjax(){
$.ajax({
url: "<?php echo get_stylesheet_directory_uri();?>/post-2.php",
cache: false,
success: function(html){
$("#part2").html(result);
},
complete: function(){
$('#loading-image').hide();
}
});
}
$(window).scroll(function() {
var divTop = $('#yourDivId').offset().top,
divHeight = $('#yourDivId').outerHeight(),
wHeight = $(window).height(),
windowScrTp = $(this).scrollTop();
if (windowScrTp > (divTop+divHeight-wHeight)){
console.log('reached');
// add your ajax method here;
CallAjax();
}
});