sticky.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v0.11.4
  6. */
  7. goog.provide('ng.material.components.sticky');
  8. goog.require('ng.material.components.content');
  9. goog.require('ng.material.core');
  10. /**
  11. * @ngdoc module
  12. * @name material.components.sticky
  13. * @description
  14. * Sticky effects for md
  15. *
  16. */
  17. angular
  18. .module('material.components.sticky', [
  19. 'material.core',
  20. 'material.components.content'
  21. ])
  22. .factory('$mdSticky', MdSticky);
  23. /**
  24. * @ngdoc service
  25. * @name $mdSticky
  26. * @module material.components.sticky
  27. *
  28. * @description
  29. * The `$mdSticky`service provides a mixin to make elements sticky.
  30. *
  31. * @returns A `$mdSticky` function that takes three arguments:
  32. * - `scope`
  33. * - `element`: The element that will be 'sticky'
  34. * - `elementClone`: A clone of the element, that will be shown
  35. * when the user starts scrolling past the original element.
  36. * If not provided, it will use the result of `element.clone()`.
  37. */
  38. function MdSticky($document, $mdConstant, $$rAF, $mdUtil) {
  39. var browserStickySupport = checkStickySupport();
  40. /**
  41. * Registers an element as sticky, used internally by directives to register themselves
  42. */
  43. return function registerStickyElement(scope, element, stickyClone) {
  44. var contentCtrl = element.controller('mdContent');
  45. if (!contentCtrl) return;
  46. if (browserStickySupport) {
  47. element.css({
  48. position: browserStickySupport,
  49. top: 0,
  50. 'z-index': 2
  51. });
  52. } else {
  53. var $$sticky = contentCtrl.$element.data('$$sticky');
  54. if (!$$sticky) {
  55. $$sticky = setupSticky(contentCtrl);
  56. contentCtrl.$element.data('$$sticky', $$sticky);
  57. }
  58. var deregister = $$sticky.add(element, stickyClone || element.clone());
  59. scope.$on('$destroy', deregister);
  60. }
  61. };
  62. function setupSticky(contentCtrl) {
  63. var contentEl = contentCtrl.$element;
  64. // Refresh elements is very expensive, so we use the debounced
  65. // version when possible.
  66. var debouncedRefreshElements = $$rAF.throttle(refreshElements);
  67. // setupAugmentedScrollEvents gives us `$scrollstart` and `$scroll`,
  68. // more reliable than `scroll` on android.
  69. setupAugmentedScrollEvents(contentEl);
  70. contentEl.on('$scrollstart', debouncedRefreshElements);
  71. contentEl.on('$scroll', onScroll);
  72. var self;
  73. return self = {
  74. prev: null,
  75. current: null, //the currently stickied item
  76. next: null,
  77. items: [],
  78. add: add,
  79. refreshElements: refreshElements
  80. };
  81. /***************
  82. * Public
  83. ***************/
  84. // Add an element and its sticky clone to this content's sticky collection
  85. function add(element, stickyClone) {
  86. stickyClone.addClass('md-sticky-clone');
  87. var item = {
  88. element: element,
  89. clone: stickyClone
  90. };
  91. self.items.push(item);
  92. $mdUtil.nextTick(function() {
  93. contentEl.prepend(item.clone);
  94. });
  95. debouncedRefreshElements();
  96. return function remove() {
  97. self.items.forEach(function(item, index) {
  98. if (item.element[0] === element[0]) {
  99. self.items.splice(index, 1);
  100. item.clone.remove();
  101. }
  102. });
  103. debouncedRefreshElements();
  104. };
  105. }
  106. function refreshElements() {
  107. // Sort our collection of elements by their current position in the DOM.
  108. // We need to do this because our elements' order of being added may not
  109. // be the same as their order of display.
  110. self.items.forEach(refreshPosition);
  111. self.items = self.items.sort(function(a, b) {
  112. return a.top < b.top ? -1 : 1;
  113. });
  114. // Find which item in the list should be active,
  115. // based upon the content's current scroll position
  116. var item;
  117. var currentScrollTop = contentEl.prop('scrollTop');
  118. for (var i = self.items.length - 1; i >= 0; i--) {
  119. if (currentScrollTop > self.items[i].top) {
  120. item = self.items[i];
  121. break;
  122. }
  123. }
  124. setCurrentItem(item);
  125. }
  126. /***************
  127. * Private
  128. ***************/
  129. // Find the `top` of an item relative to the content element,
  130. // and also the height.
  131. function refreshPosition(item) {
  132. // Find the top of an item by adding to the offsetHeight until we reach the
  133. // content element.
  134. var current = item.element[0];
  135. item.top = 0;
  136. item.left = 0;
  137. while (current && current !== contentEl[0]) {
  138. item.top += current.offsetTop;
  139. item.left += current.offsetLeft;
  140. current = current.offsetParent;
  141. }
  142. item.height = item.element.prop('offsetHeight');
  143. item.clone.css('margin-left', item.left + 'px');
  144. if ($mdUtil.floatingScrollbars()) {
  145. item.clone.css('margin-right', '0');
  146. }
  147. }
  148. // As we scroll, push in and select the correct sticky element.
  149. function onScroll() {
  150. var scrollTop = contentEl.prop('scrollTop');
  151. var isScrollingDown = scrollTop > (onScroll.prevScrollTop || 0);
  152. // Store the previous scroll so we know which direction we are scrolling
  153. onScroll.prevScrollTop = scrollTop;
  154. //
  155. // AT TOP (not scrolling)
  156. //
  157. if (scrollTop === 0) {
  158. // If we're at the top, just clear the current item and return
  159. setCurrentItem(null);
  160. return;
  161. }
  162. //
  163. // SCROLLING DOWN (going towards the next item)
  164. //
  165. if (isScrollingDown) {
  166. // If we've scrolled down past the next item's position, sticky it and return
  167. if (self.next && self.next.top <= scrollTop) {
  168. setCurrentItem(self.next);
  169. return;
  170. }
  171. // If the next item is close to the current one, push the current one up out of the way
  172. if (self.current && self.next && self.next.top - scrollTop <= self.next.height) {
  173. translate(self.current, scrollTop + (self.next.top - self.next.height - scrollTop));
  174. return;
  175. }
  176. }
  177. //
  178. // SCROLLING UP (not at the top & not scrolling down; must be scrolling up)
  179. //
  180. if (!isScrollingDown) {
  181. // If we've scrolled up past the previous item's position, sticky it and return
  182. if (self.current && self.prev && scrollTop < self.current.top) {
  183. setCurrentItem(self.prev);
  184. return;
  185. }
  186. // If the next item is close to the current one, pull the current one down into view
  187. if (self.next && self.current && (scrollTop >= (self.next.top - self.current.height))) {
  188. translate(self.current, scrollTop + (self.next.top - scrollTop - self.current.height));
  189. return;
  190. }
  191. }
  192. //
  193. // Otherwise, just move the current item to the proper place (scrolling up or down)
  194. //
  195. if (self.current) {
  196. translate(self.current, scrollTop);
  197. }
  198. }
  199. function setCurrentItem(item) {
  200. if (self.current === item) return;
  201. // Deactivate currently active item
  202. if (self.current) {
  203. translate(self.current, null);
  204. setStickyState(self.current, null);
  205. }
  206. // Activate new item if given
  207. if (item) {
  208. setStickyState(item, 'active');
  209. }
  210. self.current = item;
  211. var index = self.items.indexOf(item);
  212. // If index === -1, index + 1 = 0. It works out.
  213. self.next = self.items[index + 1];
  214. self.prev = self.items[index - 1];
  215. setStickyState(self.next, 'next');
  216. setStickyState(self.prev, 'prev');
  217. }
  218. function setStickyState(item, state) {
  219. if (!item || item.state === state) return;
  220. if (item.state) {
  221. item.clone.attr('sticky-prev-state', item.state);
  222. item.element.attr('sticky-prev-state', item.state);
  223. }
  224. item.clone.attr('sticky-state', state);
  225. item.element.attr('sticky-state', state);
  226. item.state = state;
  227. }
  228. function translate(item, amount) {
  229. if (!item) return;
  230. if (amount === null || amount === undefined) {
  231. if (item.translateY) {
  232. item.translateY = null;
  233. item.clone.css($mdConstant.CSS.TRANSFORM, '');
  234. }
  235. } else {
  236. item.translateY = amount;
  237. item.clone.css(
  238. $mdConstant.CSS.TRANSFORM,
  239. 'translate3d(' + item.left + 'px,' + amount + 'px,0)'
  240. );
  241. }
  242. }
  243. }
  244. // Function to check for browser sticky support
  245. function checkStickySupport($el) {
  246. var stickyProp;
  247. var testEl = angular.element('<div>');
  248. $document[0].body.appendChild(testEl[0]);
  249. var stickyProps = ['sticky', '-webkit-sticky'];
  250. for (var i = 0; i < stickyProps.length; ++i) {
  251. testEl.css({position: stickyProps[i], top: 0, 'z-index': 2});
  252. if (testEl.css('position') == stickyProps[i]) {
  253. stickyProp = stickyProps[i];
  254. break;
  255. }
  256. }
  257. testEl.remove();
  258. return stickyProp;
  259. }
  260. // Android 4.4 don't accurately give scroll events.
  261. // To fix this problem, we setup a fake scroll event. We say:
  262. // > If a scroll or touchmove event has happened in the last DELAY milliseconds,
  263. // then send a `$scroll` event every animationFrame.
  264. // Additionally, we add $scrollstart and $scrollend events.
  265. function setupAugmentedScrollEvents(element) {
  266. var SCROLL_END_DELAY = 200;
  267. var isScrolling;
  268. var lastScrollTime;
  269. element.on('scroll touchmove', function() {
  270. if (!isScrolling) {
  271. isScrolling = true;
  272. $$rAF.throttle(loopScrollEvent);
  273. element.triggerHandler('$scrollstart');
  274. }
  275. element.triggerHandler('$scroll');
  276. lastScrollTime = +$mdUtil.now();
  277. });
  278. function loopScrollEvent() {
  279. if (+$mdUtil.now() - lastScrollTime > SCROLL_END_DELAY) {
  280. isScrolling = false;
  281. element.triggerHandler('$scrollend');
  282. } else {
  283. element.triggerHandler('$scroll');
  284. $$rAF.throttle(loopScrollEvent);
  285. }
  286. }
  287. }
  288. }
  289. MdSticky.$inject = ["$document", "$mdConstant", "$$rAF", "$mdUtil"];
  290. ng.material.components.sticky = angular.module("material.components.sticky");