将所有背景更改为透明

时间:2018-11-13 13:26:58

标签: javascript sidenav

https://codepen.io/jayllopy/pen/bQgRPY

function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}

function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft= "0";
document.body.style.backgroundColor = "white";
}

我具有此链接,可将所有背景更改为白色不透明度0.4,但#main的背景色为红色

问:当sidenav打开时,我想将#main背景更改为rgba(0,0,0,0.4)

1 个答案:

答案 0 :(得分:0)

function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
    document.getElementById("main").style.marginLeft = "250px";
    document.getElementById("main").style.backgroundColor = 'rgba(0,0,0,0.4)';
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("main").style.marginLeft= "0";
    document.body.style.backgroundColor = "white";
    document.getElementById("main").style.backgroundColor = 'rgb(255,0,0)';
}

当您关闭它时,它会变回红色。