AngularJS – scroll to a specific element using anchorscroll

Sometimes, we require to scroll on specific element or div on angularjs application like scroll for go on specific comment, scroll on specific table row it. However we can do it using anchorscroll in angularjs application.

anchorscroll() will help to scroll on specific “anchor” div id. anchorscroll() is provided by angularjs. We can use it simply. We don’t require to write login on code jquery, because it is easy to do using anchorscroll(). $anchorScroll will use with $location variable hash.

I gave you full example of how to scroll on specific element on angularjs page, We can use simple as bellow code:


$scope.gotoDiv = function(x) {
var newHash = 'anchor' + x;
if ($location.hash() !== newHash) {
$location.hash('anchor' + x);
} else {
$anchorScroll();
}
};

Now we are going to show full example of $anchorScroll, you can also see demo for this example. You can copy bellow code and run in your machine, So let’s see bellow code:

index.html




  Angular scroll to a specific element
  
  
  
  



 
Div {{x}} of 5 Div

Leave a Reply

Your email address will not be published. Required fields are marked *