转换伪元素和父元素时 z-index 不起作用
z-index not working when transforming both psuedo element and parent
目前我正在尝试在 X 轴和 Y 轴上平移一个父元素 10px,同时在其他方向上平移一个 ::after
等量(这是为了模拟伪元素无处移动的体验).我预计这是相当微不足道的,但是, ::after
不想在转换时留在它的父级后面。
我假设创建一个新的堆栈上下文会起作用,而且我以前从未 运行 解决过这个问题(有多年的 CSS 经验)。
body {
background-color: #FFF;
}
button {
padding: .75rem 1rem;
background-color: #eee;
border: none;
font-weight: 500;
font-size: 1rem;
position: relative;
transition: ease all .15s;
}
button::after {
content: '';
position: absolute;
z-index: -1;
height: 100%;
width: 100%;
background-color: #000;
top: 0;
left: 0;
transition: ease all .15s;
}
button:hover {
transform: translate(-10px, -10px);
}
button:hover::after {
transform: translate(10px, 10px);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Fun Button</title>
</head>
<body>
<button type="button">Fun Button!</button>
</body>
</html>
解决这个问题的一个简单方法是考虑使用另一个伪元素来创建灰色背景,并且让它们最初都属于同一个堆叠上下文(通过向按钮添加 z-index
)这样你就不会添加 transform
时没有任何问题
body {
background-color: #FFF;
}
button {
padding: .75rem 1rem;
border: none;
font-weight: 500;
font-size: 1rem;
position: relative;
z-index:0;
transition: ease all .15s;
}
button::after,
button::before{
content: '';
position: absolute;
z-index: -2;
height: 100%;
width: 100%;
background-color: #000;
top: 0;
left: 0;
transition: ease all .15s;
}
button::before{
z-index:-1;
background-color: #eee;
}
button:hover {
transform: translate(-10px, -10px);
}
button:hover::after {
transform: translate(10px, 10px);
}
<button type="button">Fun Button!</button>
目前我正在尝试在 X 轴和 Y 轴上平移一个父元素 10px,同时在其他方向上平移一个 ::after
等量(这是为了模拟伪元素无处移动的体验).我预计这是相当微不足道的,但是, ::after
不想在转换时留在它的父级后面。
我假设创建一个新的堆栈上下文会起作用,而且我以前从未 运行 解决过这个问题(有多年的 CSS 经验)。
body {
background-color: #FFF;
}
button {
padding: .75rem 1rem;
background-color: #eee;
border: none;
font-weight: 500;
font-size: 1rem;
position: relative;
transition: ease all .15s;
}
button::after {
content: '';
position: absolute;
z-index: -1;
height: 100%;
width: 100%;
background-color: #000;
top: 0;
left: 0;
transition: ease all .15s;
}
button:hover {
transform: translate(-10px, -10px);
}
button:hover::after {
transform: translate(10px, 10px);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Fun Button</title>
</head>
<body>
<button type="button">Fun Button!</button>
</body>
</html>
解决这个问题的一个简单方法是考虑使用另一个伪元素来创建灰色背景,并且让它们最初都属于同一个堆叠上下文(通过向按钮添加 z-index
)这样你就不会添加 transform
body {
background-color: #FFF;
}
button {
padding: .75rem 1rem;
border: none;
font-weight: 500;
font-size: 1rem;
position: relative;
z-index:0;
transition: ease all .15s;
}
button::after,
button::before{
content: '';
position: absolute;
z-index: -2;
height: 100%;
width: 100%;
background-color: #000;
top: 0;
left: 0;
transition: ease all .15s;
}
button::before{
z-index:-1;
background-color: #eee;
}
button:hover {
transform: translate(-10px, -10px);
}
button:hover::after {
transform: translate(10px, 10px);
}
<button type="button">Fun Button!</button>