在圆形路径上实现盒阴影/文字阴影的动画效果?

8

我试图使用CSS动画来创建一个光源效果,在物体上投下阴影并在其周围做圆形运动。我已经创建了以下片段,展示了到目前为止的进展。

现在看起来有点接近,但是因为我只有四个关键帧,所以它像是光源沿着一个正方形路径移动。我想让它看起来像是沿着一个圆形路径移动。

我能想到的唯一解决方案是添加更多的关键帧并创建一个(为简单起见)十二边形路径,但是否有更简单的解决方案?是否有一种类型的时间函数可以用于平滑路径?或者我可以使用某种Sass函数来自动计算中间关键帧吗?

需要注意的是,一旦我用box-shadow使其正常工作,我还想将相同的方法应用于text-shadow。

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100vh;
}

.circle {
  width: 200px;
  height: 200px;
  border-radius: 100%;
  background-color: teal;
  box-shadow: 50px 50px 5px darkgrey;
  animation: orbit-shadow 5s linear infinite;
}

@keyframes orbit-shadow {
  0% {
    box-shadow: 50px 50px 5px darkgrey;
  }
  25% {
    box-shadow: -50px 50px 5px darkgrey;
  }
  50% {
    box-shadow: -50px -50px 5px darkgrey;
  }
  75% {
    box-shadow: 50px -50px 5px darkgrey;
  }
  1000% {
    box-shadow: 50px 50px 5px darkgrey;
  }
}
<div class="container">
  <div class="circle"></div>
</div>

1个回答

11

在此需要考虑旋转问题。使用伪元素以避免对主元素进行旋转:

您需要考虑旋转问题。使用伪元素来避免旋转主元素:

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  position:relative;
  z-index:0;
}

.circle {
  width: 200px;
  height: 200px;
  margin:50px;
  border-radius: 100%;
  background-color: teal;
  position:relative;
}
.circle::before {
  content:"";
  position:absolute;
  z-index:-1;
  top:0;
  left:0;
  right:0;
  bottom:0;
  border-radius:inherit;
  box-shadow: 50px 50px 5px darkgrey;
  animation: orbit-shadow 5s linear infinite;
}

@keyframes orbit-shadow {
  100% {
    transform:rotate(360deg);
  }
}

body{
 margin:0;
}
<div class="container">
  <div class="circle"></div>
</div>

如果您没有任何内容,您可以简单地旋转该元素:

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.circle {
  width: 200px;
  height: 200px;
  border-radius: 100%;
  background-color: teal;
  box-shadow: 50px 50px 5px darkgrey;
  animation: orbit-shadow 5s linear infinite;
}

@keyframes orbit-shadow {
  100% {
    transform:rotate(360deg);
  }
}

body{
 margin:0;
}
<div class="container">
  <div class="circle"></div>
</div>

另一个想法:

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  position:relative;
  z-index:0;
}

.circle {
  width: 200px;
  height: 200px;
  margin:50px;
  border-radius: 100%;
  background-color: teal;
  position:relative;
}
.circle::before {
  content:"";
  position:absolute;
  z-index:-1;
  top:0;
  left:0;
  right:0;
  bottom:0;
  border-radius:inherit;
  background:darkgrey;
  filter:blur(5px);
  animation: orbit-shadow 5s linear infinite;
}

@keyframes orbit-shadow {
  0% {
    transform:rotate(0deg)   translate(50px);
  }
  100% {
    transform:rotate(360deg) translate(50px);
  }
}

body{
 margin:0;
}
<div class="container">
  <div class="circle"></div>
</div>

您也可以使用稍微不同的动画,以避免旋转文本,对text-shadow进行相同的操作:

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.circle {
  position:relative;
  font-size:40px;
  font-weight:bold;
}
.circle::before,
.circle::after{
  content:attr(data-text);
  position:relative;
  z-index:1;
}

.circle::before {
  position:absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
  color:transparent;
  text-shadow:0 0 5px darkgrey;
  animation: orbit-shadow 5s linear infinite;
}
/* the 50px is your offset */
@keyframes orbit-shadow {
  0% {
    transform:rotate(0deg)   translate(50px) rotate(0deg);
  }
  100% {
    transform:rotate(360deg) translate(50px) rotate(-360deg);
  }
}
body{
 margin:0;
}
<div class="container">
  <div class="circle" data-text="some text"></div>
</div>


1
@itsViney 我正在更新一个文本阴影解决方案。 - Temani Afif
所有这些例子都会影响滚动条,但我不知道是否有解决方案。不过,我怀疑这不会被用在以前不存在滚动条的地方。不过,它可能会导致内容上下移动,使其变得相当恼人。然而,我不知道是否有解决方案... :/ 但它看起来很酷! - Ismael Miguel
1
@IsmaelMiguel,您可以增加主元素的边距,以确保旋转的元素将在该区域内旋转,并避免内容的任何移位。 - Temani Afif
1
@IsmaelMiguel 我认为不是因为这与box-sizing无关。Margin更合适,因为在所有情况下,旋转都会使元素超出范围,除非您将padding应用于父元素。 - Temani Afif
@oldboy 我认为你可以在图片附近放置一个隐藏文本。爬虫仍然可以在HTML代码中看到它(不是一个SEO专家,无法确认这一点)。 - Temani Afif
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接