viewScroll.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * @ngdoc object
  3. * @name ui.router.state.$uiViewScrollProvider
  4. *
  5. * @description
  6. * Provider that returns the {@link ui.router.state.$uiViewScroll} service function.
  7. */
  8. function $ViewScrollProvider() {
  9. var useAnchorScroll = false;
  10. /**
  11. * @ngdoc function
  12. * @name ui.router.state.$uiViewScrollProvider#useAnchorScroll
  13. * @methodOf ui.router.state.$uiViewScrollProvider
  14. *
  15. * @description
  16. * Reverts back to using the core [`$anchorScroll`](http://docs.angularjs.org/api/ng.$anchorScroll) service for
  17. * scrolling based on the url anchor.
  18. */
  19. this.useAnchorScroll = function () {
  20. useAnchorScroll = true;
  21. };
  22. /**
  23. * @ngdoc object
  24. * @name ui.router.state.$uiViewScroll
  25. *
  26. * @requires $anchorScroll
  27. * @requires $timeout
  28. *
  29. * @description
  30. * When called with a jqLite element, it scrolls the element into view (after a
  31. * `$timeout` so the DOM has time to refresh).
  32. *
  33. * If you prefer to rely on `$anchorScroll` to scroll the view to the anchor,
  34. * this can be enabled by calling {@link ui.router.state.$uiViewScrollProvider#methods_useAnchorScroll `$uiViewScrollProvider.useAnchorScroll()`}.
  35. */
  36. this.$get = ['$anchorScroll', '$timeout', function ($anchorScroll, $timeout) {
  37. if (useAnchorScroll) {
  38. return $anchorScroll;
  39. }
  40. return function ($element) {
  41. $timeout(function () {
  42. $element[0].scrollIntoView();
  43. }, 0, false);
  44. };
  45. }];
  46. }
  47. angular.module('ui.router.state').provider('$uiViewScroll', $ViewScrollProvider);