<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.whole{
width: 100%;
height: 10000px;
}
.nav{
position: fixed;
right: 0;
bottom: 0;
display: none;
}
</style>
</head>
<body>
<div class="whole">
<div class="nav">
<button>回到顶部</button>
</div>
</div>
<script>
const nav=document.querySelector('.nav')
window.addEventListener('scroll',function(){
const n=document.documentElement.scrollTop
if(n>=300){
nav.style.display='block'
}
else{
nav.style.display='none'
}
})
nav.addEventListener('click',function(){
document.documentElement.scrollTop=0
})
</script>
</body>
</html>
屏幕录制 2025-01-24 142504