Contoh butang skrol balik ke atas
HTML view
<button onclick="topFunction()" id="myKeatas" title="Ke atas">Atas</button>
<style type="text/css">
#myKeatas {
/* sifat gaya */
display: none;
position: fixed;
bottom: 10px;
right: 20px;
z-index: 99;
font-size: 16px;
font-weight: bold;
color: #a95f14;
outline: none;
background: rgb(207, 226, 243, 0.7);
cursor: pointer;
padding: 16px;
border: 1px solid #ffffff;
border-radius: 4px;
}
#myKeatas:hover {
background-color: rgb(252, 229, 205, 0.7);
}
</style>
<script>
var mybutton = document.getElementById("myKeatas");
// Butang muncul apabila diskrol turun 800px dari atas
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 800 || document.documentElement.scrollTop > 800) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
function topFunction() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
</script>
<style type="text/css">
#myKeatas {
/* sifat gaya */
display: none;
position: fixed;
bottom: 10px;
right: 20px;
z-index: 99;
font-size: 16px;
font-weight: bold;
color: #a95f14;
outline: none;
background: rgb(207, 226, 243, 0.7);
cursor: pointer;
padding: 16px;
border: 1px solid #ffffff;
border-radius: 4px;
}
#myKeatas:hover {
background-color: rgb(252, 229, 205, 0.7);
}
</style>
<script>
var mybutton = document.getElementById("myKeatas");
// Butang muncul apabila diskrol turun 800px dari atas
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 800 || document.documentElement.scrollTop > 800) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
function topFunction() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
</script>
Demo: CSS dan skrip 'butang skrol balik ke atas' yg serupa digunakan di halaman blog ini - beza warna.
Ulasan