Always return to the top of the screen by clicking on links of a SPA with Angular.Js

Asked

Viewed 1,467 times

0

I have an angular design, of which with the $routeProvider define my views and call the appropriate controllers, and use the <ng-view></ng-view> to present them on my website.

They are few pages, but they are pages with a scroll bar, and when clicking on a link I change from one view to another <a ng-href="/projeto/#/produto/{{produtoId}}>{{produtoNome}}</a>.

However, when it goes to the next page, the scrolling is at the same height, it does not go back to the top, I know it is because it is a SPA, it is not literally changing pages. But there is some easy way of angular, of whenever change route, scrolling the page always return to the top?

Thank you.

  • I think this is what you’re looking for https://docs.angularjs.org/api/ng/service/$anchorScroll

  • @Marcelobonifazio actually no, I will try to assemble an example of my case and already edit the question.

  • Take a look :) http://stackoverflow.com/a/27430246/3956218

  • @Marcelobonifazio Caraca! is exactly what I wanted! hahaha, I looked at the q theory that should do what I need! however I’m not getting to do hahaha, I need some angular dependency for this $rootScope. $on? vi que o autor da pergunta usa o angular-ui-route, eu uso apenas o angular-route. isso implica no evento $stateChangeSuccess? Obrigado :D

  • I think to use Location.href='#height'; it would be simpler in your case, I will edit Thiago’s answer using, see if it works... it’s okay to use native javascript functions within an angular $Scope, it works the same way :)

  • If you can create a minimal example with what you have, maybe to try to help you using the angular example, you can notice that it uses pure javascript to anchor the element

  • Post only the code when you go to the next page Aldo

  • @Marcelobonifazio I navigate between the views with the <a ng-href="/#/...rota"> with even a normal link.

Show 3 more comments

1 answer

1


Using only Angular, I found the ngView documentation, which recommends using:

<div class="ng-view" autoscroll="true"></div>

But if you want to use a little jQuery does the following, adds the Scroll Function to all <a> using .delegate() so jQuery threw even to <a> dynamically created by Angularjs :

$("body").delegate("a", "click", function() {
  $("html, body").animate({ scrollTop: 0 }, "slow");
});
#altura {
  height: 1000px;
  width: 100%;
  background: #EEE;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="altura"></div>
<a>Todos os Links</a>

And if you want to do using only pure JS:

var Anchors = document.getElementsByTagName("a");

for (var i = 0; i < Anchors.length ; i++) {
    Anchors[i].addEventListener("click", 
        function() {
            document.body.scrollTop = 0;
        }, 
        false);
}
#altura {
  height: 1000px;
  width: 100%;
  background: #EEE;
}
<div id="altura"></div>
<a>Todos os Links</a>

Another solution using Pure JS:

#altura {
  height: 1000px;
  width: 100%;
  background: #EEE;
}
<div id="altura"></div>
<a onclick="location.href='#altura'">Todos os Links</a>

  • With that you say that with Angular.js there is no way to do this? I thought about doing it the same way you showed it (but without using jquery because it doesn’t include dependency on my project, using javascript only). But I preferred to ask to know if there is any half native of the Angular that would do it. Thank you.

  • @Aldofernandesjunior Don’t you use jQuery? I’ll change it then! Look you want only with Angular and without using native JS, this?

  • @Thiagosantos I took the liberty of adding my example in your reply, if you don’t mind

  • @Marcelobonifazio I do not care, but he would have to add this onCLick in all elements, I don’t know if he wants this

  • You didn’t understand rs, if it put in the code, no matter where location.href='#altura' he goes to anchor :) That’s why I think maybe it was the best solution.

  • In this way that you spoke, I would have to add onclick even since, as angular use, I have some links that are loaded after rendering the DOM, so the links would not enter the addeventlistener. Or I’d put jQuery to do it the first way @.@

  • @Aldofernandesjunior the previous jQuery would not work with dynamically created items, but the current one yes, I made a small edition.

  • @Marcelobonifazio removed your suggestion because it would not work with dynamically created links, I’m wondering if I also remove my suggestion with JS.

  • Caraca Thiago santos! is such a simple thing, this autoscroll="true" worked exactly for what I needed! O.O, and look I gave a search D: I think I need to improve my google hahaha Skills Thanks a lot for the help! : D upvoted!

  • 1

    @Aldofernandesjunior I’m happy to help and, leaving a hint, it seems that search Skill is a natural thing, but it’s not, so it always gives that extra push in the search that you’re getting better. A hug!

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.