【问题标题】:Position Animated Text to the Bottom Left of Div将动画文本定位到 Div 的左下角
【发布时间】:2022-01-26 16:03:17
【问题描述】:

有人可以帮我把这个动画文本放在我的 div 的左下角吗?如果我需要改变实现动画文本的方式,我会全力以赴。我从另一个 Stack Overflow 线程中得到了我正在使用的示例。我正在使用 Wordpress 和 Elementor。

我的网站的链接是: http://stalone.flywheelsites.com/

我目前使用的代码如下:

.lyrics-container {
    margin: 75% 0% 0% 0%;
}
.lyrics{
  position: absolute;
  font-size: 72px;
  font-weight: bold;
  line-height: 87%;
}
.lyrics:nth-child(1){
  animation-name: fade;
  animation-fill-mode: both;
  animation-iteration-count: infinite;
  animation-duration: 5s;
  animation-direction: alternate-reverse;  
}


.lyrics:nth-child(2){
  animation-name: fade;
  animation-fill-mode: both;
  animation-iteration-count: infinite;
  animation-duration: 5s;
  animation-direction: alternate;
}

@keyframes fade{
    0%,50% {
      opacity: 0;
}
    100%{
      opacity: 1;
  }
}
  <div class="logo">Logo</div>
  <div class="lyrics-container">
  <p class="lyrics">I'm trying,<br>not to swim<br>into the deep,</p>
  <p class="lyrics">I will never<br>love again.</p>
  </div>

注意:出于演示目的,我做了一个快速的保证金修改,我知道这是不可行的,这就是我在这里寻求帮助的原因。

提前致谢!

【问题讨论】:

  • 在您的网站中,这个 div 被包裹在另一个 div 中

标签: html css wordpress elementor


【解决方案1】:

你需要一个绝对定位元素的相对包装器。这是一种常见的做法。您可能需要给包装器一定的高度。例如 100vh 或任何您需要的。

我只是添加了最低限度。所以你将不得不处理其余的样式。

.wrapper {
  position: relative;
 }

.lyrics-container {
    position: absolute;
    left: 0;
    bottom: 0;
    margin: 75% 0% 0% 0%;
}
.lyrics{
  position: absolute;
  font-size: 72px;
  font-weight: bold;
  line-height: 87%;
}
.lyrics:nth-child(1){
  animation-name: fade;
  animation-fill-mode: both;
  animation-iteration-count: infinite;
  animation-duration: 5s;
  animation-direction: alternate-reverse;  
}


.lyrics:nth-child(2){
  animation-name: fade;
  animation-fill-mode: both;
  animation-iteration-count: infinite;
  animation-duration: 5s;
  animation-direction: alternate;
}

@keyframes fade{
    0%,50% {
      opacity: 0;
}
    100%{
      opacity: 1;
  }
}
  <div class="wrapper" >
     <div class="logo">Logo</div>
     <div class="lyrics-container">
        <p class="lyrics">I'm trying,<br>not to swim<br>into the deep,</p>
        <p class="lyrics">I will never<br>love again.</p>
     </div>
  </div>

【讨论】:

  • 这行得通,我只需要在 .lyrics-container div 中添加 100% 的宽度,以便换行符符合定义。我只是希望除了使用 75% 的边距来强制定位在 div 的左下角之外,还有其他方法。这是实现这一目标的最佳方法,还是我可以做其他更好的事情?我不喜欢的主要事情是不同的视口并没有真正将其始终定位在左下角。我找到了这个网站:generalcondition.com我喜欢他们的做法,但我不知道他们是如何做到的。
【解决方案2】:

您可以使用position: absolute; 将其与底部对齐,而不会影响其余组件。更多关于positions here

.lyrics-container {
  width: 100%;
  position: absolute;
  bottom: 0;
}

.lyrics {
  position: absolute;
  font-size: 72px;
  font-weight: bold;
  line-height: 87%;
}

.lyrics:nth-child(1) {
  animation-name: fade;
  animation-fill-mode: both;
  animation-iteration-count: infinite;
  animation-duration: 5s;
  animation-direction: alternate-reverse;
}

.lyrics:nth-child(2) {
  animation-name: fade;
  animation-fill-mode: both;
  animation-iteration-count: infinite;
  animation-duration: 5s;
  animation-direction: alternate;
}

@keyframes fade {
  0%,
  50% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
<div class="logo">Logo</div>
<div class="lyrics-container">
  <p class="lyrics">I'm trying,<br>not to swim<br>into the deep,</p>
  <p class="lyrics">I will never<br>love again.</p>
</div>

【讨论】:

  • 在真实网站的其他元素中像这样使用它可能会导致意想不到的结果,因为没有声明为相对的父级。
  • 非常正确,我注意到了。我确实想使用底部:0;对齐而不是 75% 的边距,因为我觉得这有点不靠谱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
相关资源
最近更新 更多