1
I created a basic script to display/hide a return button at the top of the page:
$(document).ready(function(){
// Add smooth scrolling to all links
$("a").on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
//Nav BG and TopBTN function
(function scrollFn(){
if($(document).scrollTop() >= 1){
$("nav").css({"background-color":"rgba(0,0,0,.6)"});
$(".up-scroll").addClass("display");
setTimeout(function(){
$(".up-scroll").addClass("active");
},100);
};
$(document).scroll(function(){
if($(this).scrollTop() >= 1){
$("nav").css({"background-color":"rgba(0,0,0,.6)"});
$(".up-scroll").addClass("display");
setTimeout(function(){
$(".up-scroll").addClass("active");
},100);
}else{
$("nav").css({"background-color":"rgba(0,0,0,0)"});
$(".up-scroll").removeClass("active");
};
});
})();
});
div{
height:3000px
}
.up-scroll{
background-color:transparent;
border: 3px solid rgba(0,0,0,1);
color:#000;
padding:6px;
width:40px;
height:40px;
border-radius:50%;
position:fixed;
bottom:20px;
right:20px;
z-index:50;
text-align:center;
opacity:0;
display:none;
transition:opacity .4s ease-in-out;
}
.up-scroll.display{
display:block;
}
.up-scroll.display.active{
opacity:1;
}
<div>
</div>
<a href="#home" class="up-scroll"><span class="sr-only">Voltar ao topo</span><i class="fas fa-arrow-up"></i></a>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
In this case, in this snippet the button will not work, when clicking, however, my problem is in making the button be hidden when the user returns to position 0 in the scroll of the page.
- No error pointed on console
not possible duplicate, if you read the question you will understand
– Murilo Melo