Angularjs Angular.js多属性指令独立工作
我有一个指令,我用它作为属性。该指令添加和删除一个类,该类触发css动画以淡入淡出div。我在我的页面上的多个位置都有这个;但是,一旦第一个div拾取该值,其余div(不可见)也会拾取该值。我如何使这些指令独立工作 指令:Angularjs Angular.js多属性指令独立工作,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我有一个指令,我用它作为属性。该指令添加和删除一个类,该类触发css动画以淡入淡出div。我在我的页面上的多个位置都有这个;但是,一旦第一个div拾取该值,其余div(不可见)也会拾取该值。我如何使这些指令独立工作 指令: .directive("scroll", function ($window) { return function (scope, element, attrs) { function getScrollOffsets(w) { // Use th
.directive("scroll", function ($window) {
return function (scope, element, attrs) {
function getScrollOffsets(w) {
// Use the specified window or the current window if no argument
w = w || window;
// This works for all browsers except IE versions 8 and before
if (w.pageXOffset != null) return {
x: w.pageXOffset,
y: w.pageYOffset
};
}
angular.element($window).bind("scroll", function (e) {
var offset = getScrollOffsets($window);
if (offset.y >= 10) {
e.preventDefault();
e.stopPropagation();
element.removeClass('not-in-view');
element.addClass('in-view');
} else {
e.preventDefault();
e.stopPropagation();
element.removeClass('in-view');
element.addClass('not-in-view');
}
scope.$apply();
});
};
});
HTML:
内容
更多内容
您应该通过以下方式检查元素在视口中是否可见:
function isElementInViewport (el) {
//special bonus for those using jQuery
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
}
var rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
);
}
这是我的最终解决方案,感谢@kwan,我对它进行了一些调整
.directive("scroll", function ($window) {
return{
scope:true,
link: function (scope, el, attrs) {
function isElementInViewport (el) {
//special bonus for those using jQuery
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
}
var height = $(el).height();
var rect = el.getBoundingClientRect();
return (
rect.top >= -(height / 1.25) &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight)+ (height / 1.25) && /*or $(window).height() */
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
);
}
angular.element($window).bind("scroll", function (e) {
if (isElementInViewport(el)) {
e.preventDefault();
e.stopPropagation();
el.removeClass('not-in-view');
el.addClass('in-view');
} else {
e.preventDefault();
e.stopPropagation();
el.removeClass('in-view');
el.addClass('not-in-view');
}
scope.$apply();
});
}
};
})
指令(“滚动”,函数($window){
返回{
范围:正确,
链接:功能(范围、el、属性){
函数IsElementViewPort(el){
//为使用jQuery的用户提供特别奖励
if(jQuery的类型==“函数”&&el实例jQuery){
el=el[0];
}
变量高度=$(el).height();
var rect=el.getBoundingClientRect();
返回(
rect.top>=-(高度/1.25)&&
rect.left>=0&&
在执行滚动逻辑帮助之前,rect.bottom是否会检查元素是否可见?请设置一个plunkerThank!我的逻辑不正确,因为触发的是基于窗口的元素,而不是元素。我还隔离了作用域并使其工作。
angular.element($window).bind("scroll", function (e) {
if (isElementInViewport(element)) {
e.preventDefault();
e.stopPropagation();
element.removeClass('not-in-view');
element.addClass('in-view');
} else {
e.preventDefault();
e.stopPropagation();
element.removeClass('in-view');
element.addClass('not-in-view');
}
scope.$apply();
});
.directive("scroll", function ($window) {
return{
scope:true,
link: function (scope, el, attrs) {
function isElementInViewport (el) {
//special bonus for those using jQuery
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
}
var height = $(el).height();
var rect = el.getBoundingClientRect();
return (
rect.top >= -(height / 1.25) &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight)+ (height / 1.25) && /*or $(window).height() */
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
);
}
angular.element($window).bind("scroll", function (e) {
if (isElementInViewport(el)) {
e.preventDefault();
e.stopPropagation();
el.removeClass('not-in-view');
el.addClass('in-view');
} else {
e.preventDefault();
e.stopPropagation();
el.removeClass('in-view');
el.addClass('not-in-view');
}
scope.$apply();
});
}
};
})