尝试使用指令淡入点击图像。该指令在图像第一次出现(初始页面加载)时效果很好,可以很好地淡入淡出,但这不会发生在点击上,它只是在没有淡入淡出的情况下切换。我如何获得指令fade-in
与ng-click="onClick()"
事件一起使用?使用指令点击图像时的角度褪色
我试着在directive
的$element.addClass("ng-hide-add");
周围加了一个超时时间,但没有奏效。
这里的HTML:
<img ng-src="img/{{randTarotImg}}.jpg" class="tarot animate-show" ng-click="onClick()" fade-in/>
这里的JS:
angular.module('TarotApp')
.controller('TarotCtrl', function ($scope) {
$scope.tarotImg = [];
for (var i=1;i<=6;i++) {
$scope.tarotImg.push(i);
}
$scope.randTarotImg = $scope.tarotImg[Math.floor(Math.random() * $scope.tarotImg.length)];
$scope.onClick = function() {
$scope.randTarotImg = $scope.tarotImg[Math.floor(Math.random() * $scope.tarotImg.length)];
};
})
.directive('fadeIn', function($timeout){
return {
restrict: 'A',
link: function($scope, $element, attrs){
$element.addClass("ng-hide-remove");
$element.on('load', function() {
$element.addClass("ng-hide-add");
});
}
};
});
这里的CSS:
.animate-show.ng-hide-add, .animate-show.ng-hide-remove {
transition: all linear 0.5s;
display: block !important;
}
.animate-show.ng-hide-add.ng-hide-add-active, .animate-show.ng-hide-remove {
opacity: 0;
}
.animate-show.ng-hide-add, .animate-show.ng-hide-remove.ng-hide-remove-active {
opacity: 1;
}
您是否试图在点击时更改图像,并且每次图像加载时都想淡入? –
@ravishankar是的,确切地说。我使用按钮尝试了这一点,但是我遇到了同样的问题,指令没有开火,(并且试图让按钮完全看不见是一个挑战):D,所以我用ng-click代替。 –
添加和删除类不会让你得到结果,你需要保持它们之间的时间延迟。在添加'ng-hide-add'类之前用'setTimeOut()'试试 –