js
$(function () {
var flag = true;
var pos = $(".two").offset().top;
toggleTool();
$(window).scroll(function () {
toggleTool();
if (flag) {
$(".w").each(function (i, ele) {
if ($(document).scrollTop() >= $(ele).offset().top)
$("li").eq(i).addClass("current").siblings("").removeClass("current");
})
}
});
$("li").on("click", function () {
flag=false;
var current = $(".w").eq($(this).index()).offset().top
$("body,html").stop().animate({
"scrollTop": current
},function(){
flag=true;
})
$(this).addClass("current").siblings("").removeClass("current");
})
function toggleTool() {
if ($(document).scrollTop() >= pos) {
$(".nav").fadeIn();
} else {
$(".nav").fadeOut();
}
}
})
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="css/index.css">
<script src="js/jquery.min.js"></script>
<script src="js/index.js"></script>
</head>
<body>
<header>
<div class=" one"></div>
<div class="w two"></div>
<div class="w three"></div>
<div class="w four"></div>
<div class="w five"></div>
<div class="w nav">
<ul>
<li class="current">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</div>
</header>
</body>
</html>
效果就是有点丑
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020072420561559.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0d5Xzk1NDM=,size_16,color_FFFFFF,t_70)