Advertisement
Guest User

Untitled

a guest
May 26th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. 'use strict';
  2.  
  3. (function() {
  4.  
  5. var $window, parallaxItems, addParallaxItem, removeParallaxItem, updateParallax;
  6.  
  7. angular.module('app.Components.ParallaxBox', [])
  8.  
  9. .directive('parallaxBox', function() {
  10. return {
  11. restrict: 'A',
  12. controller: ['$element', function($element) {
  13. this.element = $element;
  14. return this;
  15. }],
  16. link: function (scope, element, attrs) {
  17. element.css({
  18. overflow: 'hidden'
  19. });
  20. }
  21. };
  22. })
  23.  
  24. .directive('parallaxMultiplier', function() {
  25. return {
  26. restrict: 'A',
  27. require: '^parallaxBox',
  28. priority: 0,
  29. link: function(scope, element, attrs, controller) {
  30. var parallaxItem;
  31. if (typeof Modernizr !== "undefined" && Modernizr !== null ? Modernizr.touch : void 0) {
  32. return;
  33. }
  34. parallaxItem = {
  35. parent: controller.element,
  36. element: element,
  37. multiplier: parseFloat(attrs.parallaxMultiplier)
  38. };
  39. addParallaxItem(parallaxItem);
  40. scope.$on('$destroy', function() {
  41. return removeParallaxItem(parallaxItem);
  42. });
  43. setTimeout(function () {
  44. var backgroundImageUrl = element.css('background-image').replace('url(','').replace(')','');
  45. if (backgroundImageUrl) {
  46. var backgroundImage = new Image();
  47. backgroundImage.onload = function () {
  48. setTimeout(updateParallax, 200);
  49. backgroundImage = null;
  50. };
  51. backgroundImage.src = backgroundImageUrl;
  52. }
  53. else {
  54. scope.$applyAsync(updateParallax);
  55. }
  56. });
  57. }
  58. };
  59. });
  60.  
  61. parallaxItems = [];
  62.  
  63. addParallaxItem = function (parallaxItem) {
  64. if (parallaxItems.length == 0) {
  65. $window.bind('scroll', updateParallax);
  66. }
  67. parallaxItems.push(parallaxItem);
  68. };
  69.  
  70. removeParallaxItem = function(parallaxItem) {
  71. parallaxItems = parallaxItems.filter(function(i) {
  72. return i !== parallaxItem;
  73. });
  74. if (parallaxItems.length == 0) {
  75. $window.unbind('scroll', updateParallax);
  76. }
  77. };
  78.  
  79. $window = angular.element(window);
  80. $window.scrollTop = function () {
  81. var doc = document.documentElement;
  82. // var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
  83. var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
  84. return top;
  85. };
  86.  
  87. if (!has3d()) {
  88. updateParallax = function() {
  89. var item, translateY, _i, _len, _results;
  90. _results = [];
  91. for (_i = 0, _len = parallaxItems.length; _i < _len; _i++) {
  92. item = parallaxItems[_i];
  93. translateY = Math.floor(($window.scrollTop() - item.parent[0].offsetTop) * item.multiplier);
  94. _results.push(item.element.css('margin-top', "" + translateY + "px"));
  95. }
  96. return _results;
  97. };
  98. } else {
  99. updateParallax = function() {
  100. var item, translateY, _i, _len;
  101. for (_i = 0, _len = parallaxItems.length; _i < _len; _i++) {
  102. item = parallaxItems[_i];
  103. translateY = Math.floor(($window.scrollTop() - item.parent[0].offsetTop) * item.multiplier);
  104. item.element.css({
  105. '-webkit-transform': "translate3d(0px, " + translateY + "px, 0px)",
  106. '-mox-transform': "translate3d(0px, " + translateY + "px, 0px)",
  107. '-o-transform': "translate3d(0px, " + translateY + "px, 0px)",
  108. '-ms-transform': "translateY(" + translateY + "px)",
  109. 'transform': "translate3d(0px, " + translateY + "px, 0px)"
  110. });
  111. }
  112. };
  113. }
  114.  
  115. function has3d(){
  116. var el = document.createElement('p'),
  117. has3d,
  118. transforms = {
  119. 'webkitTransform':'-webkit-transform',
  120. 'OTransform':'-o-transform',
  121. 'msTransform':'-ms-transform',
  122. 'MozTransform':'-moz-transform',
  123. 'transform':'transform'
  124. };
  125.  
  126. // Add it to the body to get the computed style
  127. document.body.insertBefore(el, null);
  128.  
  129. for(var t in transforms){
  130. if( el.style[t] !== undefined ){
  131. el.style[t] = 'translate3d(1px,1px,1px)';
  132. has3d = window.getComputedStyle(el).getPropertyValue(transforms[t]);
  133. }
  134. }
  135.  
  136. document.body.removeChild(el);
  137.  
  138. return (has3d !== undefined && has3d.length > 0 && has3d !== "none");
  139. }
  140.  
  141. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement