回到顶部

116 阅读1分钟
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>回到顶部</title>
	<style type="text/css">
		body {
			box-sizing: border-box;
			margin: 0;
			padding: 0;
		}

		.contaniner {
			position: relative;
			display: grid;
			place-items:  center;
		}

		.btn {
			bottom: 10px;
			right: 10px;
			position: fixed;
			border: 0;
			outline: 2px solid lightgreen;
		}
	</style>
</head>
<body>
	<div class="contaniner">
		<div class="btn" onclick="goTop()">
			<button>回到顶部</button>
		</div>
		<h1>下滑菜单</h1>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>
		<p>1</p>

		<script type="text/javascript">
			function goTop() {
			    window.scrollTo({
    				top: 0,
    				behavior: "smooth"
			    });
			}
		</script>
	</div>
</body>
</html>