[英]Run function, disable scroll, but enable on callback
I want to make a infinite scroll page, but I have a problem. 我想制作一个无限滚动页面,但是我有一个问题。
My loadElements
works with $.get
, like this: 我的
loadElements
与$.get
,如下所示:
loadElements: function() {
// fades out page
$.get(//..).done(function() {// fades in page});
}
And scrolling: 并滚动:
$(window).scroll(function() {
Functions.loadElements();
});
What I want is to run Functions.loadElements();
我想要的是运行
Functions.loadElements();
once on scroll, wait for the // fades in page
, then reenable scrolling again. 滚动后,等待
// fades in page
,然后重新启用滚动。 I tried bind/unbind
, but with no success. 我尝试了
bind/unbind
,但是没有成功。 How can I achieve this? 我该如何实现?
EDIT: 编辑:
I tried using bind
and unbind
like this: 我尝试像这样使用
bind
和unbind
:
$(window).scroll(function() {
Functions.loadElements();
$(window).unbind('scroll');
}
and in loadElements
: 并在
loadElements
:
loadElements: function() {
// fades out page
$.get(//..).done(function() {// fades in page; $(window).bind('scroll'); });
}
But it didn't work. 但这没有用。
Maybe instead of rebinding the scroll function that contains the logic, try binding a function that "disables" scrolling and then remove this function once your content is ready to scroll again. 也许与其重新绑定包含逻辑的滚动功能,不如尝试绑定一个“禁用”滚动的功能,然后在您的内容准备好再次滚动时将其删除。 You could use jquery namespace events for this
您可以为此使用jquery名称空间事件
Try with 试试看
$(window).scroll(function() {
Functions.loadElements();
$(window).bind('scroll.StopScroll', function (e) {
e.preventDefault(); e.stopImmediatePropagation();
});
}
And then in the .done
unbind it 然后在
.done
解除绑定
$(window).unbind('scroll.StopScroll');
I can't give you a solution exactly. 我无法为您提供确切的解决方案。 but try something like this.
但是尝试这样的事情。 You can maintain a status with a globle variable.
您可以使用globle变量维护状态。
var isLoading = false;
$(window).scroll(function(e) {
if( !isLoading )
{
Functions.loadElements();
isLoading = true;
}
e.preventDefault();
}
loadElements: function() {
// fades out page
$.get(//..).done(function() {// fades in page; isLoading = false; });
}
I'd do it with the get
promise 我会用做
get
承诺
var waiting;
$(window).scroll(function(e) {
if(waiting){
e.preventDefault();
return;
}
Functions.loadElements().then(function(){
waiting = false;
});
waiting = true;
});
and return the get
promise from loadElements
并从
loadElements
返回get
promise
loadElements: function() {
// fades out page
return $.get(...).done(function() {...});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.