写这篇文章的背景:
接手的项目中有一个页面,通过锚点实现了导航联动内容,但是没有实现内容滚动时联动导航
于是我在项目中实现这一功能之前,在本地模拟了相同的场景
以下代码可以直接复制到本地查看效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>导航与页面滚动的联动</title>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue"></script>
<style>
*{margin: 0;padding: 0;}
html,body{height: 100%;overflow: hidden;}
ul{list-style: none;}
a{text-decoration: none;color: #666;}
.clearfix:after{content: '';clear: both;display: block;}
.container{width: 1000px;margin: 0 auto;height: 100%}
.list li{width: 100px;height: 80px;line-height: 80px;float: left;}
.active a{color: #f00;}
.wrap{height: 300px;overflow-y: scroll;border: 1px solid #ccc;}
</style>
</head>
<body>
<div id="contain