Zee*_*atz 20 css css3 css-filters
我试图用CSS创建粘性效果(没有svg).一切顺利,但我的父容器有一个contrast filter,我不能在我的子元素上使用颜色(对比度过滤器改变颜色).
是否有人知道只使用CSS制作此效果的基本方法,或者反转对比度滤镜以在子元素上获得正确的颜色?
我的代码:
body{
background: #fff;
}
.blobs {
position:absolute;
top:0;
left:0;
bottom:0;
right:0;
background: #fff;
width:400px;
height:200px;
margin:auto;
filter:contrast(20);
-webkit-filter:contrast(20);
}
.blob {
background:black;
width:100px;
height:100px;
position:absolute;
left:50%;
top:50%;
margin-top:-50px;
margin-left:-50px;
border-radius:100%;
transition: cubic-bezier(0.82, 0.1, 0.24, 0.99) 2.5s;
-webkit-transition: cubic-bezier(0.82, 0.1, 0.24, 0.99) 2.5s;
box-shadow: 0 0 30px 10px black;
}
.blobs:hover .blob:first-child {
transform:translateX(-70px);
}
.blobs:hover .blob:last-child {
transform:translateX(70px);
}Run Code Online (Sandbox Code Playgroud)
<div class="blobs">
<div class="blob"></div>
<div class="blob"></div>
</div>Run Code Online (Sandbox Code Playgroud)
当我为子元素着色时:
body{
background: #fff;
}
.blobs {
position:absolute;
top:0;
left:0;
bottom:0;
right:0;
background: #fff;
width:400px;
height:200px;
margin:auto;
filter:contrast(20);
-webkit-filter:contrast(20);
}
.blob {
background: rgb(255, 113, 93);
width:100px;
height:100px;
position:absolute;
left:50%;
top:50%;
margin-top:-50px;
margin-left:-50px;
border-radius:100%;
transition: cubic-bezier(0.82, 0.1, 0.24, 0.99) 2.5s;
-webkit-transition: cubic-bezier(0.82, 0.1, 0.24, 0.99) 2.5s;
box-shadow: 0 0 30px 10px rgb(255, 113, 93);
}
.blobs:hover .blob:first-child {
transform:translateX(-70px);
}
.blobs:hover .blob:last-child {
transform:translateX(70px);
}
.original-color {
height: 100px;
background: rgb(255, 113, 93);
}Run Code Online (Sandbox Code Playgroud)
<div class="blobs">
<div class="blob"></div>
<div class="blob"></div>
</div>
<div class="original-color"></div>Run Code Online (Sandbox Code Playgroud)
val*_*als 13
我已经采取了你的效果.在容器上,我设置了一个覆盖它的伪元素,无论你想要什么颜色.
使用混合混合模式,我可以在容器为黑色的地方设置此颜色,保留剩余的白色:
(顺便说一下,效果非常好!)
body{
background: #fff;
}
.blobs {
position:absolute;
top:0;
left:0;
bottom:0;
right:0;
background: #fff;
width:400px;
height:200px;
margin:auto;
filter:contrast(20);
-webkit-filter:contrast(20);
}
.blobs:after {
content: "";
position: absolute;
top:0;
left:0;
bottom:0;
right:0;
background-color: coral;
mix-blend-mode: lighten;
}
.blob {
background:black;
width:100px;
height:100px;
position:absolute;
left:50%;
top:50%;
margin-top:-50px;
margin-left:-50px;
border-radius:100%;
transition: cubic-bezier(0.82, 0.1, 0.24, 0.99) 1.5s;
box-shadow: 0 0 30px 10px black;
}
.blobs:hover .blob:first-child {
transform:translateX(-70px);
}
.blobs:hover .blob:last-child {
transform:translateX(70px);
}Run Code Online (Sandbox Code Playgroud)
<div class="blobs">
<div class="blob"></div>
<div class="blob"></div>
</div>Run Code Online (Sandbox Code Playgroud)
添加了另一种获取请求的方式.这很难设置,但可以在Edge上使用,其中过滤器可用但混合模式不可用.
此代码段涉及使用先前设置中的2个,并为每个设置使用不同的原色.(您可以使用原始设置获得原色).
要获得特定颜色,您可以为2个设置设置不同的alpha,并以某种方式可以实现您想要的任何颜色(即使过程并不容易)
body{
background: #fff;
}
.blobs {
position:absolute;
top:0;
left:0;
bottom:0;
right:0;
background: #fff;
width:400px;
height:200px;
margin:auto;
filter:contrast(20);
-webkit-filter:contrast(20);
opacity: 0.99;
}
.blob {
width:100px;
height:100px;
position:absolute;
left:50%;
top:50%;
margin-top:-50px;
margin-left:-50px;
border-radius:100%;
transition: cubic-bezier(0.82, 0.1, 0.24, 0.99) 2.5s;
}
.blobs:hover .blob:first-child,
.blobs:hover ~ .blobs .blob:first-child {
transform:translateX(-70px);
}
.blobs:hover .blob:last-child,
.blobs:hover ~ .blobs .blob:last-child {
transform:translateX(70px);
}
.blobs:nth-child(1) {
opacity: 0.57;
}
.blobs:nth-child(1) .blob {
background: red;
box-shadow: 0 0 30px 10px red;
}
.blobs:nth-child(2) {
pointer-events: none;
opacity: 0.08;
}
.blobs:nth-child(2) .blob {
background: yellow;
box-shadow: 0 0 30px 10px yellow;
}
.test {
width: 100px;
height: 100px;
position: absolute;
left: 0px;
background-color: rgb(255, 113, 93);
}Run Code Online (Sandbox Code Playgroud)
<div class="blobs">
<div class="blob"></div>
<div class="blob"></div>
</div>
<div class="blobs">
<div class="blob"></div>
<div class="blob"></div>
</div>
<div class="test"></div>Run Code Online (Sandbox Code Playgroud)
另一个尝试,这次使用更复杂的过滤器.
颜色通过色调旋转实现
body {
background: #fff;
}
.blobs {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: #fff;
width: 400px;
height: 200px;
margin: auto;
-webkit-filter: contrast(20) hue-rotate(-25deg);
filter: contrast(20) hue-rotate(-25deg);
}
.blob {
background: red;
width: 100px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
margin-top: -50px;
margin-left: -50px;
border-radius: 100%;
transition: cubic-bezier(0.82, 0.1, 0.24, 0.99) 1.5s;
box-shadow: 0 0 30px 10px red;
}
.blobs:hover .blob:first-child {
transform: translateX(-70px);
}
.blobs:hover .blob:last-child {
transform: translateX(70px);
}Run Code Online (Sandbox Code Playgroud)
<div class="blobs">
<div class="blob"></div>
<div class="blob"></div>
</div>Run Code Online (Sandbox Code Playgroud)
另一个尝试,这次是一个div ....
.test {
border: 1px solid black;
width: 600px;
height: 400px;
background-color: white;
background-image: radial-gradient(circle, red 100px, transparent 140px), radial-gradient(circle, red 100px, transparent 140px);
background-position: -150px 0px, 150px 0px;
background-repeat: no-repeat;
-webkit-filter: contrast(20) hue-rotate(35deg);
filter: contrast(20) hue-rotate(35deg);
transition: background-position 2s;
animation: crazy 13s infinite steps(12);
}
.test:hover {
background-position: 0px 0px, 0px 0px;
}
@keyframes crazy {
from {
filter: contrast(20) hue-rotate(0deg);
}
to {
filter: contrast(20) hue-rotate(360deg);
}
}Run Code Online (Sandbox Code Playgroud)
<div class="test"></div>Run Code Online (Sandbox Code Playgroud)
试图获得跨浏览器的解决方案.添加了javascript以有效地检查混合模式.
只需单击按钮即可运行该功能.
function check () {
if('CSS' in window && 'supports' in window.CSS) {
var support = window.CSS.supports('mix-blend-mode','lighten');
if ( ! support) {
var element = document.getElementById('blobs');
element.classList.add('compat');
}
}
}Run Code Online (Sandbox Code Playgroud)
body{
background: #fff;
}
.blobs {
position:absolute;
top:0;
left:0;
bottom:0;
right:0;
background: #fff;
width:400px;
height:200px;
margin:auto;
filter:contrast(20);
-webkit-filter:contrast(20);
}
.blobs:after {
content: "";
position: absolute;
top:0;
left:0;
bottom:0;
right:0;
background-color: coral;
mix-blend-mode: lighten;
}
.blob {
background:black;
box-shadow: 0 0 30px 10px black;
width:100px;
height:100px;
position:absolute;
left:50%;
top:50%;
margin-top:-50px;
margin-left:-50px;
border-radius:100%;
transition: cubic-bezier(0.82, 0.1, 0.24, 0.99) 1.5s;
}
.blobs:hover .blob:first-child {
transform:translateX(-70px);
}
.blobs:hover .blob:last-child {
transform:translateX(70px);
}
/* compatibility */
.blobs.compat {
-webkit-filter:contrast(20) hue-rotate(-25deg);
filter:contrast(20) hue-rotate(-25deg);
}
.blobs.compat:after {
content: none;
}
.compat .blob {
background: red;
box-shadow: 0 0 30px 10px red;
}Run Code Online (Sandbox Code Playgroud)
<div class="blobs" id="blobs">
<div class="blob"></div>
<div class="blob"></div>
</div>
<button onclick="check()">Check</button>Run Code Online (Sandbox Code Playgroud)