[英]Z-index doesn't work with absolute positions
I have created a custom toggle switch component that looks like this:我创建了一个自定义切换开关组件,如下所示:
<template>
<div>
<label class="switch">
<input
type="checkbox"
:checked="value"
@change="changeSwitch"
>
<span class="slider round" />
<img
:src="require('../../assets/img/sun.svg')"
class="toggle-img"
alt="moon"
width="18"
height="18"
>
<img
:src="require('../../assets/img/moon.svg')"
class="toggle-img right"
alt="moon"
width="18"
height="18"
>
</label>
</div>
</template>
Styles of this component:此组件的 Styles:
@use "../variables" as v;
@import "../mixins";
.switch {
position: relative;
display: inline-block;
@include size(34px, 60px);
input {
display: none;
}
.toggle-img {
z-index: 5;
@include positions($top: 0, $bottom: 0);
@include margin(auto, 0, auto, 5px);
position: absolute;
&.right {
@include positions($right: 0);
@include margin(7px, 5px, auto, 0);
}
}
.slider {
z-index: 4;
@include positions(0, 0, 0, 0);
position: absolute;
cursor: pointer;
background-color: var(--color-header-bg-pale);
-webkit-transition: 0.4s;
transition: 0.4s;
&:before {
@include size(26px, 26px);
@include positions($left: 4px, $bottom: 4px);
position: absolute;
content: "";
background-color: var(--color-font-color);
-webkit-transition: 0.4s;
transition: 0.4s;
}
&.round {
border-radius: 34px;
&:before {
border-radius: 50%;
}
}
}
input:checked + .slider:before {
-ms-transform: translateX(26px);
transform: translateX(26px);
}
}
What I want to do, is to make icons be under slider, but no matter how I tried to do that playing around with z-index
it doesn't work and I can't get why.我想要做的是让图标位于 slider 下,但无论我如何尝试使用z-index
进行操作,它都不起作用,我不明白为什么。
I was trying to set z-index
on every part like parent and child, also, I was trying to remove all other z-index
in project, but it still makes no any impact.我试图在每个部分(如父母和孩子)上设置z-index
,此外,我试图删除项目中的所有其他z-index
,但它仍然没有任何影响。
Here is how it looks like:这是它的样子:
Do you try to set right image z-index to lowest when the input checked?当输入检查时,您是否尝试将正确的图像 z-index 设置为最低? If not you should try this如果不是,你应该试试这个
input:checked + .right:before {
z-index: -99;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.