1
私は、header :: before擬似要素の背景色を動的に変更する方法を検討しています。しかし、別のクラスを作成せずに2回目の他のすべての属性を定義しなくても、バックグラウンドカラーを変更して、異なる属性の1つだけを切り替えることもできます。それは可能なのか、もしそうなら、どんな提案ですか?css - 動的に擬似要素の背景を変更する
HTML:
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<header></header>
</body>
<script src="code.js"></script>
</html>
CSS:
header {
position: absolute;
width: 100%;
height: 600px;
box-shadow: 0 0 10px black;
}
header::before {
content: "";
z-index: -1;
position: absolute;
width: inherit;
height: inherit;
background-color: red;
/*
few more attributes
*/
}
JS:
setInterval(function(){
var random = Math.floor((Math.random() * 5) + 1);
if(random == 1) console.log(1);// and also set header::before with background-color: blue;
else if(random == 2) console.log(2);// and also set header::before with background-color: green;
else console.log(3);// and also set header::before with background-color: red;
}, 2000);
答えのためのTyのようなビットは、私が探していただけで何:) –