题一:页面返回顶部
要求: 点击按钮之后可以由快到慢地回到顶部,处于顶部位置时按钮消 失,按钮位于页面的右下方。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
*{
scroll-behavior: smooth;
}
.head{
background-color: rgb(65, 14, 219);
height: 5000px;
width: 1000px;
margin: 0 auto;
color: white;
}
.nav{
background-color: rgb(15, 219, 28);
height: 2000px;
width: 1000px;
margin: 0 auto;
color: white;
}
.body{
background-color: rgb(25, 119, 182);
height: 4000px;
width: 1000px;
margin: 0 auto;
}
.btn{
position: relative;
left: 1600px;
}
</style>
<body>
<div class="head">h7456</div>
<div class="nav">j456</div>
<div class="body">85468495</div>
<button class="btn">返回顶部</button>
<script>
const btn = document.querySelector('.btn')
btn.addEventListener('click', function () {
document.documentElement.scrollTop = 0
})
</script>
</body>
</html>
JS题目一