Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS animate image on src change

I have an AnuglarJS app, where I load/change some images from a webservice...

Controller

.controller('PlayerCtrl', function($scope, programService) {
    ....
    programService.refresh(function(data) {
        $scope.program = data;
    });
    ....

Template

<img src="{{program.image}}" />

When my app updates from the webservice the images changes as expected, I just want to make an fadeout / fadein when this happens, how can that be done?

Is it possible to always make a fadeout/in when a image src changes?

like image 849
pkdkk Avatar asked Sep 08 '14 07:09

pkdkk


2 Answers

Thanks for the responses -

I ended up doing this, and it works ;)

--- Directive ---

.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");
            });
        }
    };
})

--- Template ---

<img ng-src="{{program.image}}" class="animate-show" fade-in />

--- 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;
}
like image 76
pkdkk Avatar answered Nov 02 '22 18:11

pkdkk


Update 1.5.x - with Angular 1.5.x you can use ngAnimateSwap to achieve this effect.

like image 24
Aides Avatar answered Nov 02 '22 17:11

Aides