仅当div可见时才运行WordPress ajax函数

时间:2015-06-03 11:16:15

标签: javascript php jquery ajax wordpress

我有一个在Wordpress中运行的AJAX函数来获取5个最新的帖子,但我想只在用户滚动到页面上某个特定div可见的点时运行此函数 - 这是否可以使用jQuery ?

到目前为止我的代码看起来像......

jQuery.ajax({
    type: 'POST',
    url: '/wp-admin/admin-ajax.php',
    data: {
        action: 'get_latest', // the PHP function to run
    },
    success: function(data, textStatus, XMLHttpRequest) {
        jQuery('#posts-load').html(''); // empty an element
        jQuery('#posts-load').append(data); // put our list of links into it
    }
});

1 个答案:

答案 0 :(得分:0)

你应该使用航路点。

var waypoint = new Waypoint({
  element: document.getElementById('#matching_div'),
  handler: function(direction) {
    // your ajax call
  }
})

Waypoints库可在此处获得: http://imakewebthings.com/waypoints/