<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
ul{
list-style: none;
}
a{
text-decoration: none;
}
html,body{
width: 100%;
height: 100%;
}
.container{
width: 1200px;
height: 100%;
margin: 0 auto;
}
.container div{
width: 100%;
height: 100%;
text-align: center;
color: red;
font-size: 30px;
border: 1px solid red;
}
.aside{
position: fixed;
width: 40px;
right: 20px;
top: 50%;
transform: translateY(-50%);
font-size: 16px;
font-weight: 700;
text-align: center;
}
.aside li{
height: 50px;
border-bottom: 1px solid #ddd;
}
.aside li a{
color: pink;
}
.aside li.current{
background: coral;
}
</style>
</head>
<body>
<div class='container' id='box'>
<div class="current">爱逛好街</div>
<div>好店主播</div>
<div>品质特色</div>
<div>猜你喜欢</div>
</div>
<ul class="aside" id='aside'>
<li class="current">
<a href="javascript:;">爱逛好街</a>
</li>
<li>
<a href="javascript:;">好店主播</a>
</li>
<li>
<a href="javascript:;">品质特色</a>
</li>
<li>
<a href="javascript:;">猜你喜欢</a>
</li>
</ul>
<script>
window.onload = function() {
function Animation(obj, json, Callback) {
clearInterval(obj.timer);
obj.timer = setInterval(function() {
var cur = 0;
var flag = true;
for (var attr in json) {
switch (attr) {
case 'opacity':
cur = Math.round(parseFloat(getStyle(obj, attr)) * 100)
break;
case 'scrollTop':
cur = obj[attr];
break;
default:
cur = parseInt(getStyle(obj, attr));
break;
}
speed = (json[attr] - cur) / 10;
speed = json[attr] > cur ? Math.ceil(speed) : Math.floor(speed)
if(json[attr] !== cur) {
flag = false
}
switch (attr) {
case 'opacity':
obj.style[attr] = `alpha(opacity:${cur + speed})`;
obj.style[attr] = (cur + speed) / 100;
break;
case 'scrollTop':
obj.scrollTop = cur + speed;
break;
default:
obj.style[attr] = cur + speed + 'px'
break;
}
}
if( flag) {
clearInterval(obj.timer);
if(Callback){
Callback()
}
return;
}
}, 10)
}
function getStyle(obj, attr) {
if(obj.currentStyle){
return obj.currentStyle[attr]
}else{
return getComputedStyle(obj, null)[attr]
}
}
let box = document.getElementById('box');
let allBoxs = box.children
let aside = document.getElementById('aside');
let lis = aside.children;
isClick = false
for(var j=0; j<lis.length; j++){
lis[j].index = j
lis[j].onclick = function() {
isClick = true;
for(var i=0; i<lis.length; i++){
lis[i].className = ''
}
this.className = 'current'
Animation(document.documentElement, {
scrollTop: this.index * document.body.clientHeight
}, function(){
isClick = false;
})
}
}
window.onscroll = function(){
if(!isClick){
var docScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
for(var i = 0; i<lis.length; i++){
if(docScrollTop > allBoxs[i].offsetTop){
for(var j=0; j<lis.length; j++){
lis[j].className = ''
}
lis[i].className = 'current';
}
}
}
}
}
window.onbeforeunload = function(){
document.documentElement.scrollTop = 0;
document.body.scrollTop = 0;
}
</script>
</body>
</html>