gpt4 book ai didi

javascript - 当页面到达滚动中的某个点时加载宽度转换?

转载 作者:太空宇宙 更新时间:2023-11-04 02:49:55 25 4
gpt4 key购买 nike

我试图在页面滚动到某个点时触发动画。这是我到目前为止所拥有的(Codepen version) :

$(window).scroll(function () {
var hT = $('#photoshop').offset().top,
hH = $('#photoshop').outerHeight(),
wH = $(window).height(),
wS = $(this).scrollTop();
console.log((hT - wH), wS);
if (wS > (hT + hH - wH)) {
// I need the CSS to happen here, so it happens when the page is scrolled to "photoshop". //
}
});
body {
background-color: black;
}

#photoshop {
font-family: 'Roboto', sans-serif;
color: #FF5444;
text-align: left;
background-color: transparent;
width: 20%;
margin-left: 24%;
margin-bottom: 3px;
padding: 2px;
margin-top: 10px;
padding-left: 3px;
font-size: 80%;
}
/* this is what I need to happen when the page is scrolled to id="photoshop"

#photoshop {
width: 40%;
background-color: #134;
transition: ease-in 400ms;
-moz-transition: ease-in 400ms;
-webkit-transition: ease-in 400ms;
transition-delay: 200ms;
}

*/
.percent {
display: inline;
color: #fff;
margin-right: 3px;
font-size: 80%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div id="photoshop">
<div class="percent">80%</div> photoshop
</div>
</body>

我试过通过 ID 函数获取元素,但它不会在我需要时加载 css。我对 JavaScript 了解不多,希望尽可能少地编写脚本。有没有办法在 if (wS > (hT + hH - wH)) { 行之后更改 CSS?

最佳答案

试试下面的,

您可以使用 jQuery addClass方法,

只需使用 css 创建一个新类,并在 div 在 viewport 中可见时使用 addClass 方法应用该类

$(window).scroll(function() {
var hT = $('#photoshop').offset().top,
hH = $('#photoshop').outerHeight(),
wH = $(window).height(),
wS = $(this).scrollTop();
console.log((hT - wH), wS);
if (wS > (hT + hH - wH)) {
$('#photoshop').addClass("photoshop_trans");
}
});
body {
background-color: black;
}
.dummy {
height: 500px;
}
#photoshop {
font-family: 'Roboto', sans-serif;
color: #FF5444;
text-align: left;
background-color: transparent;
width: 20%;
margin-left: 24%;
margin-bottom: 3px;
padding: 2px;
margin-top: 10px;
padding-left: 3px;
font-size: 80%;
}
/* this is what I need to happen when the page is scrolled to id="photoshop" */

#photoshop.photoshop_trans {
width: 40%;
background-color: #134;
transition: ease-in 400ms;
-moz-transition: ease-in 400ms;
-webkit-transition: ease-in 400ms;
transition-delay: 200ms;
}
.percent {
display: inline;
color: #fff;
margin-right: 3px;
font-size: 80%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
<div class="dummy"></div>
<div id="photoshop">
<div class="percent">80%</div>photoshop
</div>
</body>

关于javascript - 当页面到达滚动中的某个点时加载宽度转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33225394/

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