Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit f19163a

Browse files
committed
Merge branch 'add-offset-to-$anchorScroll' of https://github.com/gkalpak/angular.js into add-offset-to-$anchorScroll
2 parents 449c6bf + 40592f3 commit f19163a

File tree

6 files changed

+34
-59
lines changed

6 files changed

+34
-59
lines changed

docs/app/src/app.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ angular.module('docsApp', [
1313
'search',
1414
'tutorials',
1515
'versions',
16-
'scrollOffset',
1716
'bootstrap',
1817
'ui.bootstrap.dropdown'
1918
])
2019

2120
.config(['$locationProvider', function($locationProvider) {
2221
$locationProvider.html5Mode(true).hashPrefix('!');
23-
}]);
22+
}]);

docs/app/src/directives.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ angular.module('directives', [])
2828
element.html(window.prettyPrintOne(html, lang, linenums));
2929
}
3030
};
31-
});
31+
})
32+
33+
.directive('scrollYOffsetElement', ['$anchorScroll', function($anchorScroll) {
34+
return function(scope, element) {
35+
$anchorScroll.yOffset = element;
36+
};
37+
}]);

docs/app/src/scroll-offset.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

docs/config/templates/indexPage.template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
</head>
7171
<body>
7272
<div id="wrapper">
73-
<header class="header header-fixed" scroll-offset-element>
73+
<header scroll-y-offset-element class="header header-fixed">
7474
<section class="navbar navbar-inverse docs-navbar-primary" ng-controller="DocsSearchCtrl">
7575
<div class="container">
7676
<div class="row">

src/ng/anchorScroll.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,12 @@ function $AnchorScrollProvider() {
116116
// TODO(gkalpak): The $anchorScrollProvider should be documented as well
117117
// (under the providers section).
118118

119-
var DEFAULT_OFFSET = 0;
120-
121119
var autoScrollingEnabled = true;
122-
var scrollOffsetGetter = function() { return DEFAULT_OFFSET; };
123120

124121
this.disableAutoScrolling = function() {
125122
autoScrollingEnabled = false;
126123
};
127124

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-
136125
this.$get = ['$window', '$location', '$rootScope', function($window, $location, $rootScope) {
137126
var document = $window.document;
138127

@@ -148,14 +137,33 @@ function $AnchorScrollProvider() {
148137
return result;
149138
}
150139

140+
function getYOffset() {
141+
142+
var offset = scroll.yOffset;
143+
144+
if (isElement(offset)) {
145+
146+
var style = $window.getComputedStyle(scroll.yOffset[0]);
147+
var top = parseInt(style.top,10);
148+
var height = parseInt(style.height,10);
149+
return style.position === 'fixed' ? (top + height) : 0;
150+
151+
} else if (isFunction(offset)) {
152+
return offset();
153+
154+
} else if (isNumber(offset)) {
155+
return offset;
156+
157+
} else {
158+
return 0;
159+
}
160+
161+
}
162+
151163
function scrollTo(elem) {
152164
if (elem) {
153165
elem.scrollIntoView();
154-
var offset = scrollOffsetGetter();
155-
var actualOffset = offset && (offset - (elem.offsetTop - document.body.scrollTop));
156-
if (actualOffset) {
157-
$window.scrollBy(0, -1 * actualOffset);
158-
}
166+
$window.scrollBy(0, -1 * getYOffset());
159167
} else {
160168
$window.scrollTo(0, 0);
161169
}

test/ng/anchorScrollSpec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ describe('$anchorScroll', function() {
5252
elmSpy = {};
5353
$provide.value('$window', {
5454
scrollTo: jasmine.createSpy('$window.scrollTo'),
55+
scrollBy: jasmine.createSpy('$window.scrollBy'),
5556
document: document,
5657
navigator: {}
5758
});

0 commit comments

Comments
 (0)