I'm attempting to use the solution here to scroll a container to a certain position. Here's my version:
var wrapper = angular.element(document.getElementById('myWrapper'))[0];
var container = wrapper.querySelector('#myContainer');
var anchor = wrapper.querySelector('#myAnchorWithinTheContainer');
scrollContainerToAnchor(container, anchor);
...
function scrollContainerToAnchor(container, anchor) {
var element = angular.element(anchor);
angular.element(container).animate({scrollTop: element.offset().top}, "slow");
}
However, angular.element
returns an array, so I don't see how that answer can work... but even if I correct it to the following:
function scrollContainerToAnchor(container, anchor) {
var element = angular.element(anchor);
angular.element(container)[0].animate({scrollTop: element[0].offset().top}, "slow");
}
the browser will still complain that "element[0].offset is not a function". So, I attempted to use getBoundingClientRect()
instead:
function scrollContainerToAnchor(container, anchor) {
var element = angular.element(anchor);
angular.element(container)[0].animate({scrollTop: element[0].getBoundingClientRect().top}, "slow");
}
but then the browser gives me "Failed to execute 'animate' on 'Element': The provided double value is non-finite" (in my case, the "non-finite" value that it's complaining about is 3282.9375(?)).
Anyway, the fact that the above-linked answer has (as of today) 10 upvotes and no complaints in the comments suggests that I am missing something, not that the answer is incorrect... So, what am I missing?
If there's a better way to scroll a div without using jQuery and without scrolling the whole page in addition to the div (I've already looked at $anchorScroll
, but it scrolls both the window and the div), I'm up for other suggestions/techniques.