"position: fixed" 在 parent 有 "transform" CSS 属性 时不工作
"position: fixed" not woking when parent has the "transform" CSS property
在我的项目中,我的屏幕应该从屏幕右侧开始 ease-in,因此我使用了 transform: translateX(100%)
,然后将其更改为 transform: translateX(0%)
。它工作正常我能够实现 ease-in 效果但是在那个屏幕上我有一个动作按钮,它有 css 属性 of Position: Fixed;Bottom: 0px;
但这不起作用我的意思是它没有粘住在屏幕底部。
这是我的 JSfiddle:https://jsfiddle.net/sureshpattu/a1seze4x/
Html:
<header>
Heading
</header>
<div class="page__popup page__popup--ease-in-right page__action-btn--visible" style="height: 382px;">
<div class="container">
</div>
<button class="js-action-btn">
SELECT ROOMS
</button>
</div>
Css:
header {
background: #fff;
height: 60px;
position: fixed;
top: 0;
left: 0;
right: 0;
width: 100%;
z-index: 10;
box-shadow: 2px 2px 10px #000;
}
.container {
height: 382px;
}
.page__popup {
position: absolute;
top: 100vh;
z-index: 8;
display: block;
overflow-y: scroll;
max-height: 100vh;
width: 100%;
height: 0;
background: #ffffff;
.js-action-btn {
position: relative;
bottom: -50px;
transition: all 0.25s ease-in-out;
}
//Themes
&--ease-in-bottom {
&.visible {
transition: height 0.25s ease-in-out;
top: 54px;
right: 0;
bottom: 0;
left: 0;
}
}
&--ease-in-right {
transform: translateX(100%);
height: 100vh;
top: 60px;
&.visible {
transition: transform 0.25s ease-in-out;
transform: translateX(0);
}
}
}
.page__action-btn--visible {
.js-action-btn {
background: red;
width: 100%;
height: 30px;
position: fixed;
bottom: 0;
z-index: 10;
box-shadow: 0 7px 4px 10px rgba(0, 0, 0, .12);
}
}
这不是错误。
查看规范:The Transform Rendering Model
Specifying a value other than ‘none’ for the ‘transform’ property
establishes a new local coordinate system at the element that it is
applied to.
所以根据规范:具有固定定位的元素将相对于具有变换的元素 - 而不是视口
作为解决方法,您可以:
1) 使用转换(例如在 left
属性 上)而不是转换 (translateX
)
2) 从使用转换的容器中删除 position:fixed
按钮
在我的项目中,我的屏幕应该从屏幕右侧开始 ease-in,因此我使用了 transform: translateX(100%)
,然后将其更改为 transform: translateX(0%)
。它工作正常我能够实现 ease-in 效果但是在那个屏幕上我有一个动作按钮,它有 css 属性 of Position: Fixed;Bottom: 0px;
但这不起作用我的意思是它没有粘住在屏幕底部。
这是我的 JSfiddle:https://jsfiddle.net/sureshpattu/a1seze4x/
Html:
<header>
Heading
</header>
<div class="page__popup page__popup--ease-in-right page__action-btn--visible" style="height: 382px;">
<div class="container">
</div>
<button class="js-action-btn">
SELECT ROOMS
</button>
</div>
Css:
header {
background: #fff;
height: 60px;
position: fixed;
top: 0;
left: 0;
right: 0;
width: 100%;
z-index: 10;
box-shadow: 2px 2px 10px #000;
}
.container {
height: 382px;
}
.page__popup {
position: absolute;
top: 100vh;
z-index: 8;
display: block;
overflow-y: scroll;
max-height: 100vh;
width: 100%;
height: 0;
background: #ffffff;
.js-action-btn {
position: relative;
bottom: -50px;
transition: all 0.25s ease-in-out;
}
//Themes
&--ease-in-bottom {
&.visible {
transition: height 0.25s ease-in-out;
top: 54px;
right: 0;
bottom: 0;
left: 0;
}
}
&--ease-in-right {
transform: translateX(100%);
height: 100vh;
top: 60px;
&.visible {
transition: transform 0.25s ease-in-out;
transform: translateX(0);
}
}
}
.page__action-btn--visible {
.js-action-btn {
background: red;
width: 100%;
height: 30px;
position: fixed;
bottom: 0;
z-index: 10;
box-shadow: 0 7px 4px 10px rgba(0, 0, 0, .12);
}
}
这不是错误。
查看规范:The Transform Rendering Model
Specifying a value other than ‘none’ for the ‘transform’ property establishes a new local coordinate system at the element that it is applied to.
所以根据规范:具有固定定位的元素将相对于具有变换的元素 - 而不是视口
作为解决方法,您可以:
1) 使用转换(例如在 left
属性 上)而不是转换 (translateX
)
2) 从使用转换的容器中删除 position:fixed
按钮