How to make a "back to top" button on a page? [duplicate]

1

How can I make a button return to the top on an html page?

Jquery, javascript, what would be best?

    
asked by anonymous 28.10.2016 / 15:45

2 answers

8

The best way will depend on your needs.

In addition to the form presented by Mauro, you can use only html pointing the href of the link to the page stretch that is at the top:

<span id='topo'></span>

<p>
  bla bla <br> bla bla <br> bla bla <br> 
  bla bla <br> bla bla <br> bla bla <br>
  bla bla <br> bla bla <br> bla bla <br>
  bla bla <br> bla bla <br> bla bla <br>
  bla bla <br> bla bla <br> bla bla <br>
  bla bla <br> bla bla <br> bla bla <br>
  bla bla <br> bla bla <br> bla bla <br>
  bla bla <br> bla bla <br> bla bla <br>
  bla bla <br> bla bla <br> bla bla <br>
  bla bla <br> bla bla <br> bla bla <br>
  bla bla <br> bla bla <br> bla bla <br>
  bla bla <br> bla bla <br> bla bla <br>
  bla bla <br> bla bla <br> bla bla <br>
</p>

<a href='#topo'>Voltar ao topo</a>
    
28.10.2016 / 16:31
6

Using Jquery

//scroll normal
$('html,body').scrollTop(0);

//scroll suave
$('html, body').animate({scrollTop:0}, 'slow'); //slow, medium, fast

Using Javascript

var scrollTop = function() {
    window.scrollTo(0, 0);
};
    
28.10.2016 / 15:47