纯CSS加载动画在白色背景(非白色背景工程)

1bqhqjot  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(140)

我有一个纯CSS的全功能旋转加载指示器,在非白色背景下工作得很好(下面的例子中页面背景是灰色的)。默认情况下圆圈显然是白色的,所以如果我将页面背景设置为白色,什么也不会显示。我该如何修改CSS,使它也能在白色背景下工作?为了举例,让我们假设圆圈应该是绿色的。

<html>
    <head>
<style>
.loading {
  display: inline-block;
  position: relative;
  width: 80px;
  height: 80px;
}
.loading div {
  position: absolute;
  border: 4px solid #fff;
  opacity: 1;
  border-radius: 50%;
  animation: loading 1s cubic-bezier(0, 0.2, 0.8, 1) infinite;
}
.loading div:nth-child(2) {
  animation-delay: -0.5s;
}
@keyframes loading {
  0% {
    top: 36px;
    left: 36px;
    width: 0;
    height: 0;
    opacity: 0;
  }
  4.9% {
    top: 36px;
    left: 36px;
    width: 0;
    height: 0;
    opacity: 0;
  }
  5% {
    top: 36px;
    left: 36px;
    width: 0;
    height: 0;
    opacity: 1;
  }
  100% {
    top: 0px;
    left: 0px;
    width: 72px;
    height: 72px;
    opacity: 0;
  }
}
</style>
    </head>
    <body style="background-color: gray;">

        <p>beginning</p>
        <div class="loading">
            <div></div>
            <div></div>
        </div>
        <p>end</p>

    </body>
</html>
o7jaxewo

o7jaxewo1#

如果你添加了混合模式,那么你的白色圆圈将在任何背景上可见:

.loading {
  display: inline-block;
  position: relative;
  width: 80px;
  height: 80px;
}

/* For example only --> */ .loading:nth-of-type(1) { background-image: linear-gradient(#000 50%, #000 50%); }.loading:nth-of-type(2) { background-image: linear-gradient(#000 50%, #fff 50%); }.loading:nth-of-type(3) { background-image: linear-gradient(#fff 50%, #fff 50%); }.loading:nth-of-type(4) { background-image: linear-gradient(#fff 0%, #000 100%); }.loading:nth-of-type(5) { background-image: linear-gradient(45deg, #f00, #f000, #080), linear-gradient(-45deg, #fa0, #f000, #08a); }

.loading div {
  position: absolute;
  border: 4px solid #fff;
  opacity: 1;
  border-radius: 50%;
  
  mix-blend-mode: difference; /* or "exclusion" */
  
  animation: loading 1s cubic-bezier(0, 0.2, 0.8, 1) infinite;
}

.loading div:nth-child(2) {
  animation-delay: -0.5s;
}

@keyframes loading {
  0% {
    top: 36px;
    left: 36px;
    width: 0;
    height: 0;
    opacity: 0;
  }
  4.9% {
    top: 36px;
    left: 36px;
    width: 0;
    height: 0;
    opacity: 0;
  }
  5% {
    top: 36px;
    left: 36px;
    width: 0;
    height: 0;
    opacity: 1;
  }
  100% {
    top: 0px;
    left: 0px;
    width: 72px;
    height: 72px;
    opacity: 0;
  }
}
<body style="background-color: gray;">
  <p>beginning</p>

  <div class="loading"><div></div><div></div></div>
  <div class="loading"><div></div><div></div></div>
  <div class="loading"><div></div><div></div></div>
  <div class="loading"><div></div><div></div></div>
  <div class="loading"><div></div><div></div></div>

  <p>end</p>
</body>
k2fxgqgv

k2fxgqgv2#

来自@jme11:像这样改变边框颜色就可以了:

.loading div {
  border: 4px solid green;
}

相关问题