【问题标题】:Slide in text on banner css animation在横幅 css 动画上滑动文本
【发布时间】:2020-12-06 20:09:33
【问题描述】:

我想在横幅上从左到右translateX 文字。我参考了这个网站上的动画https://pereiraodell.com/

他们使用 jQuery/javaScript 来制作动画。我想使用 CSS 获得相同的效果。

我正在尝试使用 CSS 复制相同的效果,但我的效果不如参考中给出的平滑。

在引用的站点中,文字动画速度逐渐变慢,不透明度设置完美。我无法在我的代码中产生相同的效果。也许我错过了关键帧中的一些断点。 fiddle

我想在悬停时显示这个动画,这样当用户悬停时,文本应该回到动画中的原始位置。

.banner{
  width:500px;
  height:300px;
  background-color:green;
  border-radius:4px;
  overflow:hidden;
  padding-left:20px
}

.content{
  width:200px
}

.title{
  transform:translateX(-1000px);
  font-size:20px;
  color:#fff;
  padding-top:10px
}
.description{
  transform:translateX(-1000px);
  font-size:20px;
  color:#fff;
  padding-top:10px
}

.banner:hover .title{
  animation: slideInRight 1s;
  animation-fill-mode:forwards
}

.banner:hover .description{
  animation: slideInRight 1s 80ms;
  animation-fill-mode:forwards
}

@keyframes slideInRight {
  0% {
    transform: translateX(-100px);
    opacity: 0;
  }
  50%{
    opacity: 0.2;
  }
  80%{
    opacity: 0.6;
  }
  100% {
    transform: translateX(0);
    opacity: 1
  }
}
<div class="banner">
<div class="content">
<div class="title">
  I am Title
</div>
<div class="description">
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum
</div>
</div>
</div>

【问题讨论】:

  • 那么在悬停之后,当文本出现时,您希望文本保持原样还是消失?
  • @Code_Ninja 我想在用户悬停之前一直发短信

标签: javascript jquery html css


【解决方案1】:

检查小提琴,希望这能解决你的问题:)

小提琴:http://jsfiddle.net/5vax1uce/68/

.banner {
  width: 500px;
  height: 300px;
  background-color: green;
  border-radius: 4px;
  overflow: hidden;
  padding-left: 20px
}

.content {
  width: 200px;
}

.title {  
  font-size: 20px;
  color: #fff;
  padding-top: 10px;
  opacity: 0;
  transform: translateX(-100px);
  transition: all .8s linear;
}

.description {
  font-size: 20px;
  color: #fff;
  padding-top: 10px;
  opacity: 0;
  transform: translateX(-100px);
  transition: all .8s linear;
}

.banner:hover .title {
  opacity: 1;
  transform: translateX(0);
}

.banner:hover .description {
  opacity: 1;
  transform: translateX(0);
}
<div class="banner">
<div class="content">
<div class="title">
  I am Title
</div>
<div class="description">
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum
</div>
</div>
</div>

【讨论】:

    【解决方案2】:

    第一个动画回答

    .banner {
      width: 500px;
      height: 300px;
      background-color: green;
      border-radius: 4px;
      overflow: hidden;
      padding-left: 20px
    }
    
    .content {
      width: 200px
    }
    
    .title {
      transform: translateX(-1000px);
      font-size: 20px;
      color: #fff;
      padding-top: 10px;
      animation: slideOutLeft 1s 80ms;
      animation-fill-mode: forwards
    }
    
    .description {
      transform: translateX(-1000px);
      font-size: 20px;
      color: #fff;
      padding-top: 10px;
      animation: slideOutLeft 1s 80ms;
      animation-fill-mode: forwards;
    }
    
    .banner:hover .title {
      animation: slideInRight 1s;
      animation-fill-mode: forwards
    }
    
    .banner:hover .description {
      animation: slideInRight 1s 80ms;
      animation-fill-mode: forwards
    }
    
    @keyframes slideInRight {
      0% {
        transform: translateX(-100px);
        opacity: 0;
      }
      50% {
        opacity: 0.2;
      }
      80% {
        opacity: 0.6;
      }
      100% {
        transform: translateX(0);
        opacity: 1
      }
    }
    
    @keyframes slideOutLeft {
      0% {
        transform: translateX(0);
        opacity: 1;
      }
      50% {
        opacity: 0.8;
      }
      80% {
        opacity: 0.6;
      }
      100% {
        transform: translateX(-100px);
        opacity: 0
      }
    }
    <div class="banner">
      <div class="content">
        <div class="title">
          I am Title
        </div>
        <div class="description">
          Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum
        </div>
      </div>
    </div>

    第二个带有过渡的答案给出更好的结果

    .banner {
      width: 500px;
      height: 300px;
      background-color: green;
      border-radius: 4px;
      overflow: hidden;
      padding-left: 20px
    }
    
    .content {
      width: 200px
    }
    
    .title {
      transform: translateX(-105%);
      opacity: 0;
      font-size: 20px;
      color: #fff;
      padding-top: 10px;
      transition: all 1s ease-in;
    }
    
    .description {
      transform: translateX(-105%);
      opacity: 0;
      font-size: 20px;
      color: #fff;
      padding-top: 10px;
      transition: all 1s ease-in;
    }
    
    .banner:hover .title,
    .banner:hover .description {
      transform: translateX(0);
      opacity: 1;
      transition: all 1s ease-out;
    }
    <div class="banner">
      <div class="content">
        <div class="title">
          I am Title
        </div>
        <div class="description">
          Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum
        </div>
      </div>
    </div>

    【讨论】:

      【解决方案3】:

      编辑: 这是我一直在谈论的修复,我在路径.banner .content .title.banner .content .description 上应用了动画。我已经编辑了你的小提琴。我默认将.content 设为display:none。当mouseenter 事件被触发时,.content 更改为display:block,然后应用动画。我希望它有所帮助。我已经更新了小提琴的链接。

      $(".banner").mouseenter(function(){
      	$(".content").css("display","block");
      });
      .banner{
        width:500px;
        height:300px;
        background-color:green;
        border-radius:4px;
        overflow:hidden;
        padding-left:20px
      }
      
      .content{
        width:200px;
        display:none;
      }
      
      .title{
        transform:translateX(-1000px);
        font-size:20px;
        color:#fff;
        padding-top:10px
      }
      .description{
        transform:translateX(-1000px);
        font-size:20px;
        color:#fff;
        padding-top:10px
      }
      
      .banner .content .title{
        animation: slideInRight 1s;
        animation-fill-mode:forwards;
      }
      .banner .content .description{
        animation: slideInRight 1s;
        animation-fill-mode:forwards;
      }
      
      @keyframes slideInRight {
        0% {
          transform: translateX(-100px);
          opacity: 0;
        }
        50%{
          opacity: 0.2;
        }
        80%{
          opacity: 0.6;
        }
        100% {
          transform: translateX(0);
          opacity: 1
        }
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
      <div class="banner">
        <div class="content">
          <div class="title">
            I am Title
          </div>
          <div class="description">
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum
          </div>
        </div>
      </div>

      这是您的fiddle 的链接。

      【讨论】:

      • 动画元素之间必须有延迟。这就是我故意添加延迟的原因
      • 好吧,让我试试,我还是 CSS 动画的新手。
      • @Carlos 我可以使用 jQuery 来解决文本停留在原处的情况吗?
      • 我希望看到 CSS 修复,但是,您可以使用 jQuery
      • 我将尝试在.banner:hover .content .title.banner:hover .content .description 上应用动画,并将.content 默认设置为display:none。如果横幅悬停,则.content 将变为display:block。因为当我们悬停时,更改会影响该元素,直到指针位于该元素上,之后必须重置它们。 @卡洛斯
      【解决方案4】:

      直到在 css 脚本中我确实改变了 @keyframe slideInRight {} 的位置,它才起作用,我在调用它之前把它放在了它然后它对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-07
        • 1970-01-01
        • 2015-07-13
        • 1970-01-01
        • 2017-06-30
        • 1970-01-01
        • 2013-01-29
        相关资源
        最近更新 更多