我目前正在开发一个无JavaScript的开关。我遇到的一个问题是,我不知道如何在一个元素处于:active状态时设置另一个元素的属性。我尝试了一些方法,但都没有成功。我在这里有一个CodePen链接。另外,这里是一些代码:
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>
有人知道解决方案吗?我已经在这上面工作了一段时间了。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我查了一下,原来这是CSS中的一个新特性。>字符用于升级(从子元素到父元素,这是新的)。
.wall-switch:has(> .switch:active){ background-color: #6b6b6b; }参考:是否有CSS父选择器?