diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/scripts/angular-parallax.js b/scripts/angular-parallax.js index 6ec39e8..875d7eb 100644 --- a/scripts/angular-parallax.js +++ b/scripts/angular-parallax.js @@ -7,7 +7,7 @@ angular.module('angular-parallax', [ scope: { parallaxRatio: '@', parallaxVerticalOffset: '@', - parallaxHorizontalOffset: '@', + parallaxHorizontalOffset: '@' }, link: function($scope, elem, $attrs) { var setPosition = function () { @@ -23,8 +23,25 @@ angular.module('angular-parallax', [ setPosition(); - angular.element($window).bind("scroll", setPosition); - angular.element($window).bind("touchmove", setPosition); + // Handle events + var bind = function() { + angular.element($window).bind("scroll", setPosition); + angular.element($window).bind("touchmove", setPosition); + }; + var unbind = function() { + angular.element($window).unbind("scroll", setPosition); + angular.element($window).unbind("touchmove", setPosition); + }; + $scope.$on('$destroy', function() { + unbind(); + }); + attrs.$observe('parallax', function(val) { + if (val) { + bind() + } else { + unbind(); + } + }) } // link function }; }]).directive('parallaxBackground', ['$window', function($window) { @@ -48,8 +65,27 @@ angular.module('angular-parallax', [ $scope.$apply(); }); - angular.element($window).bind("scroll", setPosition); - angular.element($window).bind("touchmove", setPosition); + // Handle events + var bind = function() { + angular.element($window).bind("scroll", setPosition); + angular.element($window).bind("touchmove", setPosition); + }; + + // Unbind events on + var unbind = function() { + angular.element($window).unbind("scroll", setPosition); + angular.element($window).unbind("touchmove", setPosition); + } + $scope.$on('$destroy', function() { + unbind(); + }); + attrs.$observe('parallaxBackground', function(val) { + if (val) { + bind(); + } else { + unbind(); + } + }) } // link function }; }]);