It seems that using $anchorScroll yOffset within a nested element is not possible according to my understanding: "To make yOffset function properly, the scrolling must occur on the document's root rather than a child element." https://docs.angularjs.org/api/ng/service/$anchorScroll
Here is an example (adapted from AngularJS documentation): I wish to click a link and have the word "between" displayed above the target of the scroll.
index.html
<body ng-app="anchorScrollOffsetExample">
<div class="fixed-header" ng-controller="headerCtrl">
<a href="" ng-click="gotoAnchor(x)" ng-repeat="x in [1,2,3,4,5]">
Go to inner-anchor {{x}}
</a>
</div>
<div id="anchor" class="anchor">
<div ng-repeat="x in [1,2,3,4,5]">
between
<div id="innerAnchor{{x}}" class="anchor" >Inner-Anchor {{x}} of 5</div>
</div>
</div>
</body>
style.css
.anchor {
border: 2px dashed DarkOrchid;
padding: 10px 10px 200px 10px;
max-height:500px;
overflow-y: auto;
}
script.js
angular.module('anchorScrollOffsetExample', [])
.run(['$anchorScroll', function($anchorScroll) {
$anchorScroll.yOffset = 500;
}])
.controller('headerCtrl', ['$anchorScroll', '$location', '$scope',
function ($anchorScroll, $location, $scope) {
$scope.gotoAnchor = function(x) {
var newHash = 'anchor' + x;
if ($location.hash() !== newHash) {
$location.hash('innerAnchor' + x);
} else {
$anchorScroll();
}
};
}
]);
http://plnkr.co/edit/yFj9fL3sOhDqjhMawI72?p=preview
Is there a proper way to achieve this in AngularJS without jQuery or additional libraries, keeping the "between" outside the target DIV?