gpt4 book ai didi

javascript - 根据滚动条位置更改元素的不透明度

转载 作者:行者123 更新时间:2023-12-02 22:31:21 25 4
gpt4 key购买 nike

我试图根据滚动条的位置更改元素的不透明度,但该函数总会在第一个“else if”之后停止,因此元素“C”、“D”、“不会发生任何事情E"即使该函数仍然检测滚动条位置

$(document).ready(function() {   
$(window).scroll(function(event) {
var scroll = $(window).scrollTop();

if (scroll < 600) {
document.getElementById("A").style.opacity = "1";
document.getElementById("B").style.opacity = "0.5";
document.getElementById("C").style.opacity = "0.5";
document.getElementById("D").style.opacity = "0.5";
}
else if (600 <= scroll < 1500) {
document.getElementById("A").style.opacity = "0.5";
document.getElementById("B").style.opacity = "1";
document.getElementById("C").style.opacity = "0.5";
document.getElementById("D").style.opacity = "0.5";
}
else if (1500 <= scroll < 2600) {
document.getElementById("A").style.opacity = "0.5";
document.getElementById("B").style.opacity = "0.5";
document.getElementById("C").style.opacity = "1";
document.getElementById("D").style.opacity = "0.5";
}
else if (2600 <= scroll < 3200) {
document.getElementById("A").style.opacity = "0.5";
document.getElementById("B").style.opacity = "0.5";
document.getElementById("C").style.opacity = "0.5";
document.getElementById("D").style.opacity = "1";

最佳答案

你不能做 x < a < y 它必须是 x < a && a < y...

$(document).ready(function() {   
$(window).scroll(function(event) {
var scroll = $(window).scrollTop();

if (scroll < 600) {
document.getElementById("A").style.opacity = "1";
document.getElementById("B").style.opacity = "0.5";
document.getElementById("C").style.opacity = "0.5";
document.getElementById("D").style.opacity = "0.5";
}
else if (600 <= scroll && scroll < 1500) {
document.getElementById("A").style.opacity = "0.5";
document.getElementById("B").style.opacity = "1";
document.getElementById("C").style.opacity = "0.5";
document.getElementById("D").style.opacity = "0.5";
}
else if (1500 <= scroll && scroll < 2600) {
document.getElementById("A").style.opacity = "0.5";
document.getElementById("B").style.opacity = "0.5";
document.getElementById("C").style.opacity = "1";
document.getElementById("D").style.opacity = "0.5";
}
else if (2600 <= scroll && scroll < 3200) {
document.getElementById("A").style.opacity = "0.5";
document.getElementById("B").style.opacity = "0.5";
document.getElementById("C").style.opacity = "0.5";
document.getElementById("D").style.opacity = "1";

关于javascript - 根据滚动条位置更改元素的不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58890525/

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