I would like to create a button to upload to the beginning, but I would not like it to always be visible, only when the page arrives in a certain part
I would like to create a button to upload to the beginning, but I would not like it to always be visible, only when the page arrives in a certain part
Here is an option made with jQuery
When btn is $(this).scrollTop() > 160)
it appears, when it is less than 160 from the top it hides. I left the comments in the code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.go-top {
position: fixed;
bottom: 4.5rem;
right: 3rem;
display: none;
border: 60px solid transparent;
border-bottom-color: black;
z-index: 1100;
cursor: pointer;
}
</style>
</head>
<body>
<div style="height:1500px; width:100px"></div>
<div class="go-top"></div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function() {
// esconde e mostra o btn
$(window).scroll(function() {
if ($(this).scrollTop() > 160) { // distancia que tem que rolar antes de aparecer
$('.go-top').fadeIn(250);
} else {
$('.go-top').fadeOut(250);
}
});
// manda pro topo
$('.go-top').click(function(event) {
event.preventDefault();
$('html, body').animate({scrollTop: 0}, 1200);
});
});
</script>
</body>
</html>