<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
.indexes {
z-index: 999;
/* visibility: hidden; */
background-color: #fff;
position: fixed;
top: 50%;
left: 10%;
border: 1px solid skyblue;
border-radius: 10px;
}
.indexes li {
cursor: pointer;
padding: 10px 20px;
transition: .3s;
color: cornflowerblue;
}
.indexes li.active {
background-color: skyblue;
color: #fff;
}
.indexes li:last-child {
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
}
.indexes li:first-child {
border-top-left-radius: 10px;
border-top-right-radius: 10px;
}
.dome1 {
position: relative;
width: 1200px;
margin: 0 auto;
}
::-webkit-scrollbar {
border-radius: 10px;
background-color: skyblue;
width: 10px;
}
::-webkit-scrollbar-thumb {
transition: .3s;
border-radius: 10px;
background-color: slateblue;
}
</style>
<body>
<ul class="indexes">
<li class="active">直播</li>
<li>动画</li>
<li>动画</li>
<li>动画</li>
<li>动画</li>
</ul>
<div class="dome1">
<div style="height:800px;background-color: rgb(222, 155, 135);"></div>
<div style="height:600px;background-color: rgb(92, 207, 92);"></div>
<div style="height:500px;background-color: rgb(97, 245, 232);"></div>
<div style="height:600px;background-color: rgb(222, 135, 215);"></div>
<div style="height:800px;background-color: rgb(233, 37, 70);"></div>
</div>
<script>
let $ = e => {
return document.querySelector(e)
}
function li_(index) {
$('.indexes li.active').classList.remove('active')
document.querySelectorAll('.indexes li')[index].classList.add('active')
}
window.addEventListener('scroll', e => {
if ($('.dome1').offsetTop - document.documentElement.scrollTop <= -200) {
$('.indexes').style['visibility'] = "visible"
console.log(111)
} else {
$('.indexes').style['visibility'] = "hidden "
}
document.querySelectorAll('.dome1>div').forEach((item, index) => {
if (item.offsetTop - document.documentElement.scrollTop <= 300) {
li_(index)
}
})
})
document.querySelectorAll('.indexes li').forEach((item, idnex) => {
item.onclick = function() {
li_(idnex)
document.documentElement.scrollTop = document.querySelectorAll('.dome1>div')[idnex].offsetTop - 200
}
})
</script>
</body>
</html>
网友评论