【发布时间】:2021-03-19 18:23:32
【问题描述】:
我正在尝试实现一个转到顶部的锚链接。最初它的位置固定在右下角。它的可见性是隐藏的。
网页一打开,我滚动,锚链接就可见了。
预期行为:当我单击锚链接时,它应该会转到顶部并将其可见性属性更改回隐藏。
真实行为:当我点击锚链接时,它会转到顶部但不隐藏。
#go-top-anchor {
font-size: 1em;
position: fixed;
right: 2%;
bottom: 5%;
background-color: tomato;
padding: 8px 8px;
border-radius: 2px;
visibility: hidden;
}
<html>
<head>
<title>Title</title>
</head>
<body onscroll="anchor_visible()">
<div id="top" style="height:300px;">
<p>You're at the top div</p>
</div>
<div style="height:300px;">
<p>You're at the bottom div</p>
</div>
<a id="go-top-anchor" onclick="hide_near_top()">Go-to-top</a>
<script>
function anchor_visible() {
document.getElementById("go-top-anchor").style.visibility = "visible";
}
</script>
<script>
function hide_near_top() {
location.href = "#top";
document.getElementById("go-top-anchor").style.visibility = "hidden";
}
</script>
</body>
</html>
【问题讨论】:
标签: javascript html css