16
16
* It also watches the `$location.hash()` and scrolls whenever it changes to match any anchor.
17
17
* This can be disabled by calling `$anchorScrollProvider.disableAutoScrolling()`.
18
18
*
19
+ * Additionally, you can specify a scroll offset (in pixels) during the configuration phase by
20
+ * calling `$anchorScrollProvider.setScrollOffset(<valueOrGetter>)`. The offset can be either a
21
+ * fixed value or a getter function that returns a value dynamically.
22
+ *
19
23
* @example
20
24
<example module="anchorScrollExample">
21
25
<file name="index.html">
50
54
}
51
55
</file>
52
56
</example>
57
+ *
58
+ * <hr />
59
+ * The example below illustrates the use of scroll offset (specified as a fixed value).
60
+ *
61
+ * @example
62
+ <example module="anchorScrollOffsetExample">
63
+ <file name="index.html">
64
+ <div class="fixed-header" ng-controller="headerCtrl">
65
+ <a href="" ng-click="gotoAnchor(x)" ng-repeat="x in [1,2,3,4,5]">
66
+ Go to anchor {{x}}
67
+ </a>
68
+ </div>
69
+ <div id="anchor{{x}}" class="anchor" ng-repeat="x in [1,2,3,4,5]">
70
+ Anchor {{x}} of 5
71
+ </div>
72
+ </file>
73
+ <file name="script.js">
74
+ angular.module('anchorScrollOffsetExample', [])
75
+ .config(['$anchorScrollProvider', function($anchorScrollProvider) {
76
+ $anchorScrollProvider.setScrollOffset(50); // always scroll by 50 extra pixels
77
+ }])
78
+ .controller('headerCtrl', ['$anchorScroll', '$location', '$scope',
79
+ function ($anchorScroll, $location, $scope) {
80
+ $scope.gotoAnchor = function(x) {
81
+ // Set the location.hash to the id of
82
+ // the element you wish to scroll to.
83
+ $location.hash('anchor' + x);
84
+
85
+ // Call $anchorScroll()
86
+ $anchorScroll();
87
+ };
88
+ }
89
+ ]);
90
+ </file>
91
+ <file name="style.css">
92
+ body {
93
+ padding-top: 50px;
94
+ }
95
+
96
+ .anchor {
97
+ border: 2px dashed DarkOrchid;
98
+ padding: 10px 10px 200px 10px;
99
+ }
100
+
101
+ .fixed-header {
102
+ background-color: rgba(0, 0, 0, 0.2);
103
+ height: 50px;
104
+ position: fixed;
105
+ top: 0; left: 0; right: 0;
106
+ }
107
+
108
+ .fixed-header > a {
109
+ display: inline-block;
110
+ margin: 5px 15px;
111
+ }
112
+ </file>
113
+ </example>
53
114
*/
54
115
function $AnchorScrollProvider ( ) {
116
+ // TODO(gkalpak): The $anchorScrollProvider should be documented as well
117
+ // (under the providers section).
118
+
119
+ var DEFAULT_OFFSET = 0 ;
55
120
56
121
var autoScrollingEnabled = true ;
122
+ var scrollOffsetGetter = function ( ) { return DEFAULT_OFFSET ; } ;
57
123
58
124
this . disableAutoScrolling = function ( ) {
59
125
autoScrollingEnabled = false ;
60
126
} ;
61
127
128
+ this . setScrollOffset = function ( newScrollOffset ) {
129
+ if ( isFunction ( newScrollOffset ) ) {
130
+ scrollOffsetGetter = function ( ) { return newScrollOffset ( ) ; } ;
131
+ } else if ( isNumber ( newScrollOffset ) ) {
132
+ scrollOffsetGetter = function ( ) { return newScrollOffset ; } ;
133
+ }
134
+ } ;
135
+
62
136
this . $get = [ '$window' , '$location' , '$rootScope' , function ( $window , $location , $rootScope ) {
63
137
var document = $window . document ;
64
138
@@ -76,20 +150,33 @@ function $AnchorScrollProvider() {
76
150
return result ;
77
151
}
78
152
153
+ function scrollTo ( elem ) {
154
+ if ( elem ) {
155
+ elem . scrollIntoView ( ) ;
156
+ var offset = scrollOffsetGetter ( ) ;
157
+ var actualOffset = offset && ( offset - ( elem . offsetTop - document . body . scrollTop ) ) ;
158
+ if ( actualOffset ) {
159
+ $window . scrollBy ( 0 , - 1 * actualOffset ) ;
160
+ }
161
+ } else {
162
+ $window . scrollTo ( 0 , 0 ) ;
163
+ }
164
+ }
165
+
79
166
function scroll ( ) {
80
167
var hash = $location . hash ( ) , elm ;
81
168
82
169
// empty hash, scroll to the top of the page
83
- if ( ! hash ) $window . scrollTo ( 0 , 0 ) ;
170
+ if ( ! hash ) scrollTo ( null ) ;
84
171
85
172
// element with given id
86
- else if ( ( elm = document . getElementById ( hash ) ) ) elm . scrollIntoView ( ) ;
173
+ else if ( ( elm = document . getElementById ( hash ) ) ) scrollTo ( elm ) ;
87
174
88
175
// first anchor with given name :-D
89
- else if ( ( elm = getFirstAnchor ( document . getElementsByName ( hash ) ) ) ) elm . scrollIntoView ( ) ;
176
+ else if ( ( elm = getFirstAnchor ( document . getElementsByName ( hash ) ) ) ) scrollTo ( elm ) ;
90
177
91
178
// no element and hash == 'top', scroll to the top of the page
92
- else if ( hash === 'top' ) $window . scrollTo ( 0 , 0 ) ;
179
+ else if ( hash === 'top' ) scrollTo ( null ) ;
93
180
}
94
181
95
182
// does not scroll when user clicks on anchor link that is currently on
@@ -107,4 +194,3 @@ function $AnchorScrollProvider() {
107
194
return scroll ;
108
195
} ] ;
109
196
}
110
-
0 commit comments