I am just starting to learn JavaScript, so I am not yet familiar with all the commands involved. I have a code that includes a button which scrolls back to the top of the page, but I was wondering if there is a way to change the position of that button based on where the cursor is located. For example, if the cursor is on the right side of the site, I would like the button to appear on the right side as well. I don't expect you to provide me with the full code, just let me know what commands I need to use and I will try to implement it myself.
Any help would be greatly appreciated!
<button onclick="topFunction()" id="topB" title="Go to top">Top</button>
#topB {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
border: none;
outline: none;
background-color: gray;
color: white;
cursor: pointer;
padding: 15px;
border-radius: 10px;
font-size: 18px;
}
#topB:hover {
color:black;
background-color: red;
}
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
// After scrolling down "number"px, the button appears
if (document.body.scrollTop >= 10 || document.documentElement.scrollTop >=
10) {
document.getElementById("topB").style.display = "block";
} else {
document.getElementById("topB").style.display = "none";
}
}
function topFunction() {
// Scrolls to the top on click
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}