【发布时间】:2018-04-12 11:43:50
【问题描述】:
我还是 javascript 新手,所以很遗憾我还不知道其中的正确命令。 所以我有这段代码,带有一个可以滚动回顶部的按钮,我想知道是否有办法根据光标的位置更改该按钮的位置?例如,如果光标位于站点的右侧,则按钮将显示在右侧。 代码很长我不指望你自己写,如果你能告诉我需要使用哪些命令就足够了,那我自己试试吧
感谢任何帮助!
<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;
}
【问题讨论】:
-
对于初学者:w3schools.com/js/js_events.asp。您可以根据事件隐藏和显示不同div上的按钮
标签: html css button position cursor