From 88194cb3c2bdba44086cbc9ec2b55abad3fbfcf9 Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Wed, 1 Oct 2014 19:42:34 +0300 Subject: [PATCH 1/3] WIP - feat($anchorScroll): add support for configurable scroll offset Add support for a configurable scroll offset to $anchorScrollProvider. The offset is expressed in pixels and can be specified either as a fixed value or as a function that return the offset it pixels dynamically. (This is a POC and a WIP: no docs, no tests) Related to #9368 Closes #2070, #9360 --- docs/app/src/app.js | 2 +- docs/app/src/directives.js | 1 - docs/app/src/scroll-offset.js | 39 ++++++++ docs/config/templates/indexPage.template.html | 2 +- src/ng/anchorScroll.js | 96 ++++++++++++++++++- 5 files changed, 132 insertions(+), 8 deletions(-) create mode 100644 docs/app/src/scroll-offset.js diff --git a/docs/app/src/app.js b/docs/app/src/app.js index af94d44a2b8d..11ac6fda73de 100644 --- a/docs/app/src/app.js +++ b/docs/app/src/app.js @@ -13,11 +13,11 @@ angular.module('docsApp', [ 'search', 'tutorials', 'versions', + 'scrollOffset', 'bootstrap', 'ui.bootstrap.dropdown' ]) - .config(['$locationProvider', function($locationProvider) { $locationProvider.html5Mode(true).hashPrefix('!'); }]); diff --git a/docs/app/src/directives.js b/docs/app/src/directives.js index 30077e83ff94..6ad4f6a38f23 100644 --- a/docs/app/src/directives.js +++ b/docs/app/src/directives.js @@ -29,4 +29,3 @@ angular.module('directives', []) } }; }); - diff --git a/docs/app/src/scroll-offset.js b/docs/app/src/scroll-offset.js new file mode 100644 index 000000000000..342eacdb5d35 --- /dev/null +++ b/docs/app/src/scroll-offset.js @@ -0,0 +1,39 @@ +angular.module('scrollOffset', []) + +/** + * scrollOffsetElement Directive + * + * @description Store the element whose height should be used to determine the scroll offset and its + * computed style. + */ +.directive('scrollOffsetElement', ['$window', 'SCROLL_OFFSET_ELEMENT', + function scrollOffsetElementDirective($window, SCROLL_OFFSET_ELEMENT) { + return { + restrict: 'A', + link: function scrollOffsetElementPostLink(scope, elem) { + elem = elem[0]; + SCROLL_OFFSET_ELEMENT.element = elem; + SCROLL_OFFSET_ELEMENT.computedStyle = $window.getComputedStyle(elem); + // README: Using `getComputedStyle()` renders this approach incompatible with IE8. + } + }; + } +]) + +.constant('SCROLL_OFFSET_ELEMENT', { + element: null, // README: This is not used, but keeping it here just in case... + computedStyle: null +}) + +.config(['$anchorScrollProvider', 'SCROLL_OFFSET_ELEMENT', + function($anchorScrollProvider, SCROLL_OFFSET_ELEMENT) { + var extraTopSpace = 15; + $anchorScrollProvider.setScrollOffset(function () { + var computedStyle = SCROLL_OFFSET_ELEMENT.computedStyle; + + if (!computedStyle || (computedStyle.position !== 'fixed')) return 0; + + return parseInt(computedStyle.height, 10) + extraTopSpace; + }); + } +]); diff --git a/docs/config/templates/indexPage.template.html b/docs/config/templates/indexPage.template.html index 852468434054..6582e92436fc 100644 --- a/docs/config/templates/indexPage.template.html +++ b/docs/config/templates/indexPage.template.html @@ -70,7 +70,7 @@
-
+