[英]Run WordPress ajax function only if div is visible
I have an AJAX function running in Wordpress to fetch 5 of the latest posts, but I would like to run this function only when the user scrolls to a point on the page where a specific div is visible - is this possible using jQuery? 我在Wordpress中运行AJAX函数以获取5个最新帖子,但仅在用户滚动到页面上可以看到特定div的点时才想运行此函数-使用jQuery是否可以?
My code so far looks like… 到目前为止,我的代码看起来像…
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
}
});
You should use waypoints. 您应该使用路标。
var waypoint = new Waypoint({
element: document.getElementById('#matching_div'),
handler: function(direction) {
// your ajax call
}
})
Waypoints library is available here: http://imakewebthings.com/waypoints/ Waypoints库位于此处: http : //imakewebthings.com/waypoints/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.