jQuery楼层效果

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery楼层效果</title>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
<script>
$(function () {
$(window).scroll(function () { //获取滚动条
var winH=$(window).height();//获取窗口可视区域的高度
var scrollTop=$(window).scrollTop();
if(scrollTop>=$("#header").height()){//当滚动的长度大于header的高度时,“楼梯”和“回到顶部” 淡入

$("#stairs").fadeIn();
$("#top").fadeIn();

// each() 遍历获取 找到所有的楼层 (i=索引,el=当前元素)
$(".floor").each(function (i,el) {
//获取每个楼层到浏览器可视区顶部的距离
var floor_top=$(el).offset().top;
if(winH+scrollTop-$(this).offset().top>=winH/2){//窗口可视区域的高度+被卷上去的高度-当前元素的高度>=窗口可视区域的高度的一半
$("#stairs li").eq(i).addClass("on").siblings().removeClass("on");
}
})
}else { //否则,当滚动的长度大于header的高度时,“楼梯”和“回到顶部” 淡出
$("#stairs").fadeOut();
$("#top").fadeOut();
}
})

//点击变高亮 //事件委托

$("#stairs").on("click","li",function () {

$(this).addClass("on").siblings().removeClass("on");
var index=$(this).index();
// console.log(index);
//点击回到对应楼层
var floor_scrolltop=$(".floor").eq(index).offset().top;
//获取对应索引的楼层,距离顶部的距离
$("html,body").stop().animate({"scrollTop": floor_scrolltop},500,function () {

})


})


//回到顶部
$("#top").click(function () {
$("html,body").animate({
"scrollTop":0
},500)
})

})

</script>
<style>
*{
margin: 0px;
padding:0px;
}
#header{
height: 200px;
background-color: lightskyblue;
font-size: 50px;
line-height: 200px;
text-align: center;

}
.floor {
height:1000px;
color: white;
font-size: 80px;
line-height: 1000px;
text-align: center;

}
#footer{
height: 600px;
color: white;
font-size: 50px;
line-height: 400px;
text-align: center;
background-color: #9c2f06;
}
ul{
list-style: none;
z-index: 110000;
position: absolute;
left: 10px;
top: 100px;
100px;
border: 1px solid white;
text-align: center;
background-color: gray;
opacity: 0.8;
display: none;
position: fixed;

}
li{

color: white;
border: 1px solid white;
100px;
height: 60px;
line-height: 50px;


}
#top{
height: 40px;
60px;
border: 1px solid black;
line-height: 40px;
background-color:gray;
font-size: 15px;
font-weight: bold;
color: white;
text-align: center;
z-index: 200;
border-radius: 5px;
position: fixed;
top: 900px;
left: 1830px;
display: none;

}
#stairs li:hover span{display: block;background-color: lightpink;height: 60px}
#stairs li:hover em{display: none;}

span{
display: none;
}

.on{background-color: red}
</style>
</head>
<body>
<ul >尾部</div>
</body>
</html>