简体   繁体   English

当用户向下滚动到屏幕底部时,如何拨打Ajax电话

[英]How do you make an Ajax call when a user scrolls down to the bottom of the screen

I would like to make an Ajax call when a user scrolls down to the bottom of the screen. 当用户向下滚动到屏幕底部时,我想拨打Ajax电话。 For some reason my code isn't working. 由于某种原因,我的代码无法正常工作。 I think it could be a syntax error but I checked my console and I don't see an error. 我认为这可能是语法错误,但是我检查了控制台,但没有看到错误。 Any suggestions, improvements or examples are welcomed. 欢迎任何建议,改进或示例。

<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>

You can use 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();
       }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建固定在屏幕底部的页脚,但当用户向下滚动时,它不再是固定页脚? - How do I create a footer that is fixed at the bottom of the screen but when the user scrolls down it is no longer fixed footer? 仅当用户向下滚动到该区域时如何加载ajax - How to load an ajax only when user scrolls down to that area 当用户垂直滚动或单击底部导航标签之一时,如何制作显示下一张幻灯片的内容滑块? - How do I make a content slider that shows the next slide when the user scrolls vertically or clicks one of the bottom nav tabs? 当用户向下滚动时,如何更改徽标并在固定标题的底部添加彩色边框? - How to change logo and add a colored border to the bottom of my fixed header when user scrolls down? 用户滚动到底部时多次触发Ajax函数 - Ajax function firing multiple times when user scrolls to bottom 当用户仅滚动一次到页面底部时如何使用ajax加载更多内容 - How to load more content with ajax when user scrolls to the bottom the of page only once 无论访问者滚动的页面有多低,如何使DIV停留在屏幕顶部? - How do I make a DIV stay at the top of the screen no matter ow far down the page my visitor scrolls? 当用户上下滚动屏幕时旋转图像 - Rotating an image when user scrolls up and down the screen 当用户滚动到页面底部附近时调用函数 - call a function when user scrolls near bottom of page 当用户向下滚动时,如何使另一个 HTML 页面出现在 HTML 页面中? - How can I make another HTML page appear within an HTML page when the User Scrolls down?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM