-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathangular-parallax.js
61 lines (56 loc) · 2.24 KB
/
angular-parallax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
angular.module('angular-parallax', [
]).directive('parallax', ['$window', function($window) {
return {
restrict: 'A',
scope: {
parallaxRatio: '@',
parallaxVerticalOffset: '@',
parallaxHorizontalOffset: '@',
parallaxIf: '=?',
},
link: function($scope, elem, attrs) {
var setPosition = function () {
if ($scope.parallaxIf === undefined || $scope.parallaxIf === true) {
if(!$scope.parallaxHorizontalOffset) $scope.parallaxHorizontalOffset = '0';
var calcValY = $window.pageYOffset * ($scope.parallaxRatio ? $scope.parallaxRatio : 1.1 );
if (calcValY <= $window.innerHeight) {
var topVal = (calcValY < $scope.parallaxVerticalOffset ? $scope.parallaxVerticalOffset : calcValY);
var hozVal = ($scope.parallaxHorizontalOffset.indexOf("%") === -1 ? $scope.parallaxHorizontalOffset + 'px' : $scope.parallaxHorizontalOffset);
elem.css('transform', 'translate(' + hozVal + ', ' + topVal + 'px)');
}
}
};
setPosition();
angular.element($window).bind("scroll", setPosition);
angular.element($window).bind("touchmove", setPosition);
} // link function
};
}]).directive('parallaxBackground', ['$window', function($window) {
return {
restrict: 'A',
transclude: true,
template: '<div ng-transclude></div>',
scope: {
parallaxRatio: '@',
parallaxVerticalOffset: '@',
parallaxIf: '=?',
},
link: function($scope, elem, attrs) {
var setPosition = function () {
if ($scope.parallaxIf === undefined || $scope.parallaxIf === true) {
var calcValY = (elem.prop('offsetTop') - $window.pageYOffset) * ($scope.parallaxRatio ? $scope.parallaxRatio : 1.1) - ($scope.parallaxVerticalOffset || 0);
// horizontal positioning
elem.css('background-position', "50% " + calcValY + "px");
}
};
// set our initial position - fixes webkit background render bug
angular.element($window).bind('load', function(e) {
setPosition();
$scope.$apply();
});
angular.element($window).bind("scroll", setPosition);
angular.element($window).bind("touchmove", setPosition);
} // link function
};
}]);