我尝试使用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);
}

<div class="blobs">
<div class="blob"></div>
<div class="blob"></div>
</div>
&#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);
}
.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);
}
&#13;
<div class="blobs">
<div class="blob"></div>
<div class="blob"></div>
</div>
<div class="original-color"></div>
&#13;
答案 0 :(得分: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);
}
<div class="blobs">
<div class="blob"></div>
<div class="blob"></div>
</div>
添加了另一种方式来获取您的请求。这很难设置,但可以在Edge上使用,其中过滤器可用,但混合模式不可用。
此代码段涉及使用之前的两个设置,并为每个设置使用不同的原色。 (您可以使用原始设置获得原色)。
要获得特定颜色,您可以为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);
}
<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>
另一次尝试,这次使用更复杂的过滤器。
使用色调旋转实现颜色
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);
}
<div class="blobs">
<div class="blob"></div>
<div class="blob"></div>
</div>
另一次尝试,这次是一个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);
}
}
<div class="test"></div>
尝试获得跨浏览器的解决方案。 添加了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');
}
}
}
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;
}
<div class="blobs" id="blobs">
<div class="blob"></div>
<div class="blob"></div>
</div>
<button onclick="check()">Check</button>
答案 1 :(得分:3)
对于这个答案,我只需要对过滤属性进行更改
contrast(15) contrast(.5) sepia(1) brightness(1.85) hue-rotate(319deg) saturate(3.45)
所以现在我们有一个浅灰色的背景和黑色的斑点
列表中的最后三个属性对于获得正确的颜色至关重要。你的目标是rgb(255,113,93)。应用此行后,颜色为rgb(255,115,94)
您可以在this Fiddle
中看到它有用作为旁注。您还可以堆叠变换,这意味着如果您想要使blob居中,则不必使用负边距,而是使用translate(-50%, -50%)
技巧。然后当您将鼠标悬停在它们上方时,您可以简单地堆叠变换,如translate(-50%, -50%) translateX(-70px)