Hintergrund Farbe ändern beim scrollen

Otreck

New Member
#1
Hallo Leute,

ich will die Navigationsleiste beim Scrollen umfärben, jedoch bekomme ich es nicht hin, dass die Variable "B" angenommen wird. Was mache ich falsch?

window.addEventListener('scroll', function() {

let x = window.pageYOffset;
let b = x;
if (x > 700 )
{
b = 700;
}
b = b/700;

document.getElementsByClassName("nav")[0].style.backgroundColor = "rgba(255, 255, 255,'+ b')";
console.log(b);
});
 

JR Cologne

Well-Known Member
c-b Experte
#2
Javascript:
window.addEventListener('scroll', function () {
    let yOffset = window.pageYOffset;
    let alpha = yOffset;

    if (yOffset > 700 ) {
        alpha = 700;
    }

    alpha = alpha / 700;

    document.getElementsByClassName("nav")[0].style.backgroundColor = "rgba(255, 255, 255," + alpha + ")";
    console.log(alpha);
});
 
Oben