javascript - 侧面板到顶部。还有其他解决办法吗?

标签 javascript html css

我正在尝试将按钮置于侧面板的顶部。但是我遇到了一个问题,因为我需要我的 SIDE 面板作用于滚动而不是整个 body 。例如有代码:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="side">
<button onclick="topFunction()" id="topBtn" title="Go to top">Top</button>

<div style="background-color:black;color:white;padding:30px">Scroll Down</div>
<div style="background-color:lightgrey;padding:30px 30px 2500px">This example demonstrates how to create a "scroll to top" button that becomes visible when the user starts to scroll the page.</div>

</div>
</body>
<script type="text/javascript" src="js.js"></script>
</html>

JS:

const side = document.getElementById('side');
const top = side.offsetTop;

if (top > 10){
    document.getElementById('topBtn').style.display = "block";
} else {
  document.getElementById('topBtn').style.display = "none";
}

最佳答案

function topFunction() {
	  document.body.scrollTop = 0;
	  document.documentElement.scrollTop = 0;
	}
#topBtn{
  position:fixed;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div id="side">
<button onclick="topFunction()" id="topBtn" title="Go to top">Top</button>

<div style="background-color:black;color:white;padding:30px">Scroll Down</div>
<div style="background-color:lightgrey;padding:30px 30px 2500px">This example demonstrates how to create a "scroll to top" button that becomes visible when the user starts to scroll the page.</div>

</div>
<script src="js.js"></script>
</body>
</html>

关于javascript - 侧面板到顶部。还有其他解决办法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54766268/

相关文章:

javascript - 为IE优化一个简单的double for循环

javascript - 使切换仅打开主动单击的按钮,而不是所有切换

html - div 在 firefox 中推出表格列的底部

javascript - 将 WMD 编辑器的预览 HTML 与服务器端 HTML 验证对齐(例如,没有嵌入的 JavaScript 代码)

javascript - javascript中的Angular2递归模板

javascript - 拉拉维尔 5.3 : Auto refresh div when data update with Ajax

javascript - 在格式化数字后将插入符设置回原来的位置

angular - PrimeNG Turbotable全部展开

java - 为 JavaFX Scenic View 设置的元素

javascript - 没有垂直滚动条的跨域iframe调整大小修复