我想记录窗口距文档顶部的偏移量,但 jquery 滚动不起作用。普通的 JavaScript 滚动事件监听器可以在 Angular 中工作吗?
app.directive('owlCarouselItem', function($touch, $timeout, $rootScope, $window){
return {
restrict: 'C',
transclude: false,
link: function(scope, element) {
// this is the part of my directive the on scroll event is not firing
$('html, body').on('scroll', function() {
if($(this).scrollTop() == 0){
console.log($(this).scrollTop());
canSwipeDown = true;
}else{
console.log($(this).scrollTop());
canSwipeDown = false;
}
});
最佳答案
使用 angular.element($window) 尝试此代码:
.directive('scrollDir', function($window) {
return {
restrict: 'EAC',
link: function(scope, attrs, element) {
var canSwipeDown = false
// this is the part of my directive the on scroll event is not firing
angular.element($window).on('scroll', function() {
canSwipeDown = element.scrollTop() === 0
scope.$apply()
});
}
};
});
您可以将指令粘贴到 body 标记上 像这样 HTML:
<body scroll-dir>
关于javascript - 普通的 JavaScript 滚动事件可以在 AngularJS 上工作吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39504405/