我目前正在一个网站上工作,遇到了这种奇怪的行为.我不确定这是一个错误或如何处理它所以我要求你们帮忙.
所以我有这个标题屏幕“动画”,其标题以全屏页面为中心,当您向下滚动时,它变小并保持在页面顶部.这是一个具有预期行为的工作示例,我从中剥离了所有不必要的代码以使其最小化:
$(window).scroll( () => {
"use strict";
let windowH = $(window).height();
let windowS = $(window).scrollTop();
let header = $("#header").height();
if (windowS < windowH-header) {
$("#title").css("transform","scale("+(2-(windowS/($(document).outerHeight()-windowH))*2.7)+")");
$("#header").css("transform","translateY(0)");
$("#inside,#content").css({
"position": "static","margin-top": 0
});
} else {
$("#inside").css({
"position": "fixed","margin-top": -windowH+header
});
$("#content").css("margin-top",windowH);
}
$("#header").css("position",windowS > (windowH-header)/2 ? "fixed" :"static");
});
.fixed {
position: fixed!important;
}
.wrapper {
width: 100%;
text-align: center;
}
.wrapper:before {
display: table;
content: " ";
}
.wrapper:after {
clear: both;
}
#inside {
width: 100%;
height: 100vh;
background-color: lightcoral;
display: flex;
align-items: center;
justify-content: center;
}
#header {
height: 90px;
top: 0;
position: sticky;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.5s;
}
#title {
width: 100%;
color: #fff;
transform: scale(2);
}
#content {
height: 1000px;
background-color: lightblue;
}
接下来是完全相同的片段,但有一个补充:我应用了一个过滤器,就我而言,纯粹是化妆品:过滤器:亮度(1.3);.
正如您在下面看到的那样,当您在“动画”中间滚动时,标题就会消失.当你检查元素时,它仍然具有它的所有属性,但不知怎的,它已经消失了.这在Firefox和Chrome中是一样的,我不知道为什么.如果有人可以发布应用了过滤器的工作片段并解释为什么它之前没有用,我会非常感激.
$(window).scroll( () => {
"use strict";
let windowH = $(window).height();
let windowS = $(window).scrollTop();
let header = $("#header").height();
if (windowS < windowH-header) {
$("#title").css("transform",windowS > (windowH-header)/2 ? "fixed" :"static");
});
.fixed {
position: fixed!important;
}
.wrapper {
width: 100%;
text-align: center;
}
.wrapper:before {
display: table;
content: " ";
}
.wrapper:after {
clear: both;
}
#inside {
width: 100%;
height: 100vh;
background-color: lightcoral;
filter: brightness(1.3); /*<<<<<<<<<<<<<<<<*/
display: flex;
align-items: center;
justify-content: center;
}
#header {
height: 90px;
top: 0;
position: sticky;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.5s;
}
#title {
width: 100%;
color: #fff;
transform: scale(2);
}
#content {
height: 1000px;
background-color: lightblue;
}
最佳答案
如果我们参考the specification,我们可以阅读:
A value other than none for the filter property results in the
creation of a containing block for absolute and fixed positioned
descendants unless the element it applies to is a document root
element in the current browsing context. The list of functions are
applied in the order provided.
这意味着您的position:fixed元素将相对于filtred容器定位,而不再是视口.换句话说,它仍然是固定的,但在其新的包含块(过滤容器)内
这是一个简化版本来说明问题:
.container {
display: inline-block;
width: 200px;
height: 200vh;
border: 1px solid;
}
.container>div {
position: fixed;
width: 100px;
height: 100px;
background: red;
color: #fff;
}
要解决此问题,请尝试将过滤器移动到固定元素而不是其容器:
.container {
display: inline-block;
width: 200px;
height: 200vh;
border: 1px solid;
}
.container>div {
position: fixed;
width: 100px;
height: 100px;
background: red;
color: #fff;
filter: grayscale(1);
}