Wie lege ich einen Eigenschaftswert für den aktiven Zustand eines anderen Elements in CSS fest?
P粉878510551
P粉878510551 2023-09-14 18:54:27
0
1
586

Ich entwickle derzeit einen JavaScript-freien Switch. Ein Problem, das ich habe, ist, dass ich nicht weiß, wie ich eine Eigenschaft eines Elements festlegen soll, während sich ein anderes Element im Status :aktiv befindet. Ich habe einiges ausprobiert, ohne Erfolg. Ich habe hier einen CodePen-Link. Außerdem gibt es hier einen Code:

body {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: gray;
  overflow: hidden;
  filter: brightness(100%);
}

.wall-switch {
  background-color: #ebebeb;
  width: 300px;
  height: 500px;
  box-shadow: inset -10px -10px 22px 0px rgba(255, 255, 255, 1), inset 10px 10px 22px 0px rgba(0, 0, 0, 0.30);
  display: flex;
  align-items: center;
  flex-direction: column;
  padding: 25px;
  border-radius: 4px;
}

.screw {
  padding: 7.5px;
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: inset 2.5px 2.5px 10px 0px rgba(255, 255, 255, 1), inset -2.5px -2.5px 10px 0px rgba(0, 0, 0, 0.30), 0px 0px 2px 1px rgba(0, 0, 0, 0.40);
}

.icon {
  transition-duration: 10s;
  transition-timing-function: linear;
}

.icon:active {
  transform: rotate(-3600deg);
}

.padding {
  padding: 55px;
}

.switch {
  width: 105px;
  height: 200px;
  box-shadow: 0px 0px 2px 1px rgba(0, 0, 0, 0.40), inset 0px 95px 20px 0px #ebebeb, inset 0px -95px 20px 0px rgba(0, 0, 0, 0.20);
  border-radius: 2.5px;
}

.switch:active {
  box-shadow: 0px 0px 2px 1px rgba(0, 0, 0, 0.40), inset 0px -95px 20px 0px #6b6b6b, inset 0px 95px 20px 0px rgba(0, 0, 0, 0.20);
}

.switch:active .wall-switch {
  background-color: #6b6b6b;
}
<div class="wall-switch">
  <div class="screw">
    <svg class="icon  icon--plus" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" fill="rgba(0, 0, 0, 0.45)">
        <path d="M24 10h-10v-10h-4v10h-10v4h10v10h4v-10h10z" stroke="url(#grad1)" />
    </svg>
  </div>
  <div class="padding"></div>
  <div class="switch"></div>
  <div class="padding"></div>
  <div class="screw">
    <svg class="icon  icon--plus" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" fill="rgba(0, 0, 0, 0.45)">
        <path d="M24 10h-10v-10h-4v10h-10v4h10v10h4v-10h10z" stroke="url(#grad1)" />
    </svg>
  </div>
</div>

Kennt jemand die Lösung? Ich arbeite schon eine Weile daran.

P粉878510551
P粉878510551

Antworte allen(1)
P粉262113569

我查了一下,原来这是CSS中的一个新特性。>字符用于升级(从子元素到父元素,这是新的)。

.wall-switch:has(> .switch:active){
  background-color: #6b6b6b;
}

参考:是否有CSS父选择器?

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!