Adım 1) HTML Ekleyin:
Tıklandığında kullanıcıyı sayfanın en üstüne götürecek bir düğme oluşturun:
<button onclick="topFunction()" id="buton" title="Başa Dön">Yukarı</button>
Adım 2) CSS ekleyin:
Düğmenin stilini belirleyin:
#buton{
display: none; /* Hidden by default */
position: fixed; /* Fixed/sticky position */
bottom: 20px; /* Place the button at the bottom of the page */
right: 30px; /* Place the button 30px from the right */
z-index: 99; /* Make sure it does not overlap */
border: none; /* Remove borders */
outline: none; /* Remove outline */
background-color: red; /* Set a background color */
color: white; /* Text color */
cursor: pointer; /* Add a mouse pointer on hover */
padding: 15px; /* Some padding */
border-radius: 10px; /* Rounded corners */
font-size: 18px; /* Increase font size */
}#buton:hover {
background-color: #555; /* Add a dark-grey background on hover */
}
Adım 3) JavaScript ekleyin:
//Düğmeyi Al:
butonum = document.getElementById("buton");// Kullanıcı siteyi 20 piksel aşağı kaydırdığında, düğmeyi göster
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
butonum.style.display = "block";
} else {
butonum.style.display = "none";
}
}// Kullanıcı düğmeyi tıkladığında, dokümanın en üstüne gönder
function topFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}