首先看一下简单搭建的样式:
上面头部,左侧菜单,右侧是内容区域,内容区域可以超出后,形成滚动条。滚轮滑动到下方出现滚动条。
页面出现的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
html,
body {
height: 100%;
width: 100%;
}
.box1 {
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
}
.top {
height: 90px;
border: 2px solid #ff0;
}
.bottom {
display: flex;
height: calc(100% - 90px);
}
.left {
width: 200px;
border: 2px solid #ccc;
}
.right {
flex: 1;
border: 2px solid #333;
overflow: hidden;
overflow-y: auto;
}
.rightChildP {
height: 50px;
line-height: 50px;
}
</style>
</head>
<body>
<div class="box1">
<div class="top">top</div>
<div class="bottom">
<div class="left">left</div>
<div class="right">
<div class="rightChild">
<div id="header">我在顶部</div>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
</div>
</div>
</div>
</div>
</body>
</html>
1.利用a标签跳转锚点
<div class="rightChild">
<div id="header">我在顶部</div>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<div><a href="#header">返回顶部</a></div>
</div>
结果:点击后立即回到顶部,浏览器url发生变化
2.利用js,做动画效果 scrollTo
<div class="right">
<div class="rightChild">
<div id="header">我在顶部</div>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<p class="rightChildP">rightChild</p>
<div><a href="#header">返回顶部</a></div>
</div>
</div>
js
<script>
function go() {
let right = document.getElementsByClassName('right')[0]
const t = right.scrollTop
console.log(t, 'tt')
if (t > 0) {
window.requestAnimationFrame(go);
right.scrollTo(0, t - t / 8);
}
}
</script>