[英]jQuery rebind $(window).scroll() after a successful ajax call
我已经制作了此无限滚动脚本,但是取消绑定后无法重新绑定窗口滚动。 这是脚本:
$(function(){
$(window).scroll(function(){
var mostOfTheWayDown = ($(document).height() - $(window).height()) * 2 / 3;
if ($(window).scrollTop() >= mostOfTheWayDown) {
$(window).unbind('scroll');
$.ajax({
url: 'loadmore',
data: {lastrank: lastrank},
dataType: 'json',
type: 'POST',
success: function(json){
//some work here
$(window).bind('scroll');
}
});
}
});
});
成功调用ajax后如何重新绑定窗口滚动?
$(function(){
var scrollFunction = function(){
var mostOfTheWayDown = ($(document).height() - $(window).height()) * 2 / 3;
if ($(window).scrollTop() >= mostOfTheWayDown) {
$(window).unbind("scroll");
$.ajax({
url: "loadmore",
data: {lastrank: lastrank},
dataType: "json",
type: "POST",
success: function(json){
//some work here
$(window).scroll(scrollFunction);
}
});
}
};
$(window).scroll(scrollFunction);
});
您需要将回调函数传递给.bind()
方法,该方法执行滚动事件触发时想要发生的一切。 您还需要首先调用unbind('scroll')
,因此不会在用户每次滚动时最终执行两个函数。
与其他答案相似的方式,但是绑定。
$(function(){
function scrollStuff() {
var mostOfTheWayDown = ($(document).height() - $(window).height()) * 2 / 3;
if ($(window).scrollTop() >= mostOfTheWayDown)
{
$(window).unbind('scroll');
$.ajax({
url: 'loadmore',
data: {lastrank: lastrank},
dataType: 'json',
type: 'POST',
success: function(json){
//some work here
$(window).bind('scroll', scrollStuff);
}
});
}
}
$(window).bind('scroll', scrollStuff);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.