I try to set the navbar li object to active when i currently watch the corresponding section. In other words: When scrolling through my page (single page with multiple sections), the section i'm watching should be highlighted in the navbar.
I already got the scrolling position via directive. Where I fail is to trigger ng-class on the .nav ul li.
<li class="" ng-class="{'active': $parent.scrollPos > document.getElementById('something').offset()}">
<a class="" ng-click="isCollapsed = true" href="#something" du-smooth-scroll>Something</a>
</li>
I doesn't make any difference when I use
ng-class="{'active': $parent.scrollPos > angular.element('#something').offset()}"
How do I get the distance from document-top of any element?
Thanks a lot.
PS: I use jQuery (not lite)!
EDIT How it works (Thanks to @Sharikov Vladislav): On body I use the "PageCtrl":
<body id="page-top" class="index bg-default" ng-app="previewApp" ng-controller="PageCtrl">
which looks like that:
angular.module('previewApp')
.controller('PageCtrl', function($scope, $element) {
$scope.scrollPos = 0;
$scope.getOffset = function (elementId) {
var element = $element.find(elementId);
return element.offset().top;
};
});
Right after the body I use a span
<span scroll-position="scrollPos"></span>
to initialise my "scrollPosition"-directive, which allows me to access the current scroll position over the $scope.scrollPos in PageCtrl. Directive:
angular.module('previewApp')
.directive('scrollPosition', function ($window) {
return {
scope: {
scrollPos: "=scrollPosition"
},
link: function (scope, element, attrs) {
var windowElement = angular.element($window);
var handler = function () {
scope.scrollPos = windowElement.scrollTop();
}
windowElement.on('scroll', scope.$apply.bind(scope, handler));
handler();
}
};
});
In my .nav ul li looks like the following. The "- 150" is for a more acurate highlighting.
<div class="navbar-collapse" uib-collapse="isCollapsed" id="menu-center">
<ul class="nav navbar-nav navbar-right">
<li class="hidden">
<a href="#page-top"></a>
</li>
<li ng-class="{'active': $parent.scrollPos > $parent.getOffset('#section1') - 150 && $parent.scrollPos < $parent.getOffset('#section2') - 150}">
<a ng-click="isCollapsed = true" href="#section1" du-smooth-scroll>Section1</a>
</li>
<li ng-class="{'active': $parent.scrollPos > $parent.getOffset('#section2') - 150 && $parent.scrollPos < $parent.getOffset('#section3')}">
<a ng-click="isCollapsed = true" href="#section2" du-smooth-scroll>Section2</a>
</li>
</ul>
</div>
I hope this helps somebody out there who is struggling over the same problem like me. Greets