AngularJS 指令 - 检测滚动是否停止并执行函数
AngularJS Directive - detect if scrolling has stopped and execute a function
在 angularjs 指令中,如何检测用户是否已停止滚动。
.directive('scrollable', ['$document', '$window', function ($document, $window, ) {
return {
link: function (scope, element, attrs) {
$document.bind('scroll', function() {
// detect if scroll has stop and execute a function
});
}
};
}]);
你需要在一段时间后手动检测它,例如 250ms :-
$document.bind('scroll', function() {
clearTimeout( $.data( this, "scrollCheck" ) );
$.data( this, "scrollCheck", setTimeout(function() {
//Here you can call a function after scroll stopped
}, 250) );
});
更新:-
工作plunker
在 angularjs 指令中,如何检测用户是否已停止滚动。
.directive('scrollable', ['$document', '$window', function ($document, $window, ) {
return {
link: function (scope, element, attrs) {
$document.bind('scroll', function() {
// detect if scroll has stop and execute a function
});
}
};
}]);
你需要在一段时间后手动检测它,例如 250ms :-
$document.bind('scroll', function() {
clearTimeout( $.data( this, "scrollCheck" ) );
$.data( this, "scrollCheck", setTimeout(function() {
//Here you can call a function after scroll stopped
}, 250) );
});
更新:-
工作plunker