0
Good afternoon, I am doing a project and created a button to return to the beginning of the page, but this does not appear when requested. I have seen videos lessons, numerous tutorials and nothing. I would like help. My code is as follows:
/* BOTÃO PARA VOLTAR AO INÍCIO */
$(document).ready (function(){
var divBotao=$('.botaoVoltar');
var linkBotao=$('.botaoVoltar-Link');
/* Mostrar botão*/
$(window).scroll(function(){
if($(this).scrollTop()>70){
divBotao.fadeIn();
}else{
divBotao.fadeOut();
}
});
/* Clicar e voltar ao topo*/
divBotao.click(function(){
$('html,body').animate({scrollTop:0},800);
return false;
});
});
.botaoVoltar{
position:fixed;
float:right;
margin-left:900px;
margin-top:400px;
width:100px;
height:50px;
background-color:red;
border-radius:5px;
display:none;
}
.botaoVoltar-Link{
text-align:center;
display:block;
line-height:50px;
color: black;
font-weight:bold;
text-decoration:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="botaoVoltar">
<a class="botaoVoltar-Link " href="">Voltar a topo</a>
</div>
I imported the jQuery library. Thanks in advance.
Your script works, what is missing is you put some height on the body, put the height of the body in 200vh that will generate scoll in the window and the button will appear
– hugocsl
I did, but it didn’t solve
– Crislaine
I posted the answer look what I left there described, will help you
– hugocsl
I modified the margin part and put left and bottom in place. The problem was the link to the page, as both jQuery and Avascript were external. But the problem was that first I called Avascript instead of jQuery first, so the button disappeared and did not appear anymore. Thanks for the help.
– Crislaine