-
Notifications
You must be signed in to change notification settings - Fork 10
/
sidebar.js
40 lines (31 loc) · 1.19 KB
/
sidebar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
angular.module('sidebar', [])
.controller('SidebarController', ["$window", "$scope", function($window, $scope){
this.scrollTo = function (index) {
$window.scrollTo(0, document.getElementsByTagName("h2").item(index).offsetTop - 25);
};
angular.element($window).on('scroll', function(){
var i = 0,
h2 = document.getElementsByTagName("h2");
while (i < h2.length) {
if (h2.item(i).offsetTop >= $window.scrollY + 75)
break;
i++;
}
$scope.sidebarCtrl.highlighted = i - 1;
$scope.$apply();
});
}])
.directive('sidebar', function() {
return {
restrict : 'E',
template: function(elem, attr){
var html = "<div id=\"sidebar\" ng-controller=\"SidebarController as sidebarCtrl\"><div>",
h2 = document.getElementsByTagName("h2");
for (var i = 0; i < h2.length; i++) {
html += "<a href=\"\" ng-click=\"sidebarCtrl.scrollTo(" + i + ")\" ng-class=\"{'highlighted':( " + i + " <= sidebarCtrl.highlighted)}\">" + h2.item(i).innerHTML + "</a>";
}
html += "</div></div>";
return html;
}
};
});