gpt4 book ai didi

javascript - 基于视口(viewport)高度的 CSS

转载 作者:行者123 更新时间:2023-11-30 16:17:29 25 4
gpt4 key购买 nike

Goldman Sachs 有一个非常酷的网页 here .这个页面让我感兴趣的是,当你向下滚动时,你会看到一个标题,根据你所在的部分,它有更多的方 block 从白色变成蓝色。我想知道(因为我似乎无法在代码中找到答案),他们究竟是如何使滚动条看起来突然出现的(双关语不是故意的),以及方 block 如何根据位置从白色变为蓝色你在页面上。

最佳答案

最常见的方法是使用 javascript 检测滚动条的位置。我在这个演示中使用了 JQuery 库。

这是一些代码(仅用于说明目的!)

$(window).scroll(function (event) {
var numOfButtons = 4;
var scroll = $(window).scrollTop();
var heightContainer = $(".container").height();
console.log('scrollPos', scroll);

if(scroll > heightContainer/ numOfButtons){
$(".header .button:nth-child(2)").addClass('act');
}else{
$(".header .button:nth-child(2)").removeClass('act');
}

if(scroll > (heightContainer/ numOfButtons)*2){
$(".header .button:nth-child(3)").addClass('act');
}else{
$(".header .button:nth-child(3)").removeClass('act');
}

if(scroll > (heightContainer/ numOfButtons)*3){
$(".header .button:nth-child(4)").addClass('act');
}else{
$(".header .button:nth-child(4)").removeClass('act');
}


});
.header{
height:50px;
background-color:blue;
color:white;
position:fixed;
top:0;
left:0;
width:100%

}
.button{
display:inline-block;
width:10px;
height:10px;
background-color:white;
border-radius:20px;
}
.button.act{
background-color:red;
}
h1{
margin-top:60px;
}
.container{
height:4000px;
background:url("http://www.planwallpaper.com/static/images/518164-backgrounds.jpg");
}
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<h1>Scroll demo</h1>
<div class="header">
<div class="button act"></div>
<div class="button"></div>
<div class="button"></div>
<div class="button"></div>
</div>
<div class="container"><div id="mydiv"></div>
</div>
</body>

</html>

enter link description here

关于javascript - 基于视口(viewport)高度的 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35270658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com