在不同事件中对同一元素应用多个CSS变换

5

我有一组链接标签,它们在页面加载时带有translate3D效果。这个效果非常完美。但是我需要链接标签在悬停时缩放,而直接缩放无法实现。


有没有办法只使用CSS来实现呢?以下是代码:

.linkblock {
  margin: 20% 0;
}

.hlink {
  width: 12%;
  height: 60px;
  opacity: 0;
  padding: 0 10px;
  background: rgba(0, 0, 0, 0.3);
  display: inline-block;
  text-align: center;
  color: rgba(0, 0, 0, 0.6);
  transition: all 0.5s ease;
}

.hlink:hover {
  transform: translate(0px, -20px);
  color: red;
  background: rgba(0, 0, 0, 0.6)
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translate3d(0, 100%, 0);
  }
  to {
    opacity: 1;
    transform: translate3d(0, 0, 0);
  }
}

.fadeInUp {
  animation: fadeInUp 0.3s ease-in both;
}

.linkblock a:nth-child(1) {
  animation-delay: 1.0s;
}

.linkblock a:nth-child(2) {
  animation-delay: 1.1s;
}

.linkblock a:nth-child(3) {
  animation-delay: 1.2s;
}

.linkblock a:nth-child(4) {
  animation-delay: 1.3s;
}

.linkblock a:nth-child(5) {
  animation-delay: 1.4s;
}

.linkblock a:nth-child(6) {
  animation-delay: 1.5s;
}

.linkblock a:nth-child(7) {
  animation-delay: 1.6s;
}

.linkblock a:nth-child(8) {
  animation-delay: 1.7s;
}
<div class="linkblock">
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>

</div>


与您的问题相关的代码应直接放入问题中,而不是仅仅倾倒在外部网站上。请相应地进行编辑。 - misorude
3个回答

2
问题在于使用了both,这使得您可以保留动画的最后状态,因此动画内部的变换将覆盖悬停上的变换,而悬停则永远不会被激活。
您可以将动画分为两个,只使用opacitybothforwards,这样您就可以在动画完成后进行过渡。

.linkblock {
  margin: 20% 0;
}

.hlink {
  width: 12%;
  height: 60px;
  padding: 0 10px;
  background: rgba(0, 0, 0, 0.3);
  display: inline-block;
  text-align: center;
  color: rgba(0, 0, 0, 0.6);
  transition: all 0.5s ease;
  opacity:0;
}

.hlink:hover {
  transform: translate(0px, -20px) scale(1.2);
  color: red;
  background: rgba(0, 0, 0, 0.6)
}

@keyframes fadeInUp {
  from {
    transform: translate3d(0, 100%, 0);
  }
}
@keyframes show {
  to {
    opacity:1;
  }
}

.fadeInUp {
  animation: fadeInUp 0.3s ease-in,
              show 0.3s ease-in forwards;
}

.linkblock a:nth-child(1) {
  animation-delay: 1.0s;
}

.linkblock a:nth-child(2) {
  animation-delay: 1.1s;
}

.linkblock a:nth-child(3) {
  animation-delay: 1.2s;
}

.linkblock a:nth-child(4) {
  animation-delay: 1.3s;
}

.linkblock a:nth-child(5) {
  animation-delay: 1.4s;
}

.linkblock a:nth-child(6) {
  animation-delay: 1.5s;
}

.linkblock a:nth-child(7) {
  animation-delay: 1.6s;
}

.linkblock a:nth-child(8) {
  animation-delay: 1.7s;
}
<div class="linkblock">
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>

</div>


这个方法在IE和Edge上不起作用。还需要其他什么吗?@Temani - Tom

1

Please try this:

.linkblock{
   margin:20% 0;
  }
.hlink{
  width:12%;
  height:60px;
  opacity:0;
  padding:0 10px;
  background:rgba(0,0,0,0.3);
  display:inline-block;
  text-align: center;
  color:rgba(0,0,0,0.6);
  transition:all 0.5s ease;
  transform:translate3d(0,0,0); /* add it */
}
.hlink:hover{
  transform:translate3d(0,0,0) scale(1.2); /* add it */
  color:red;
  background:rgba(0,0,0,0.6)
}
  
@keyframes fadeInUp {
  from{
    opacity:0;
    transform:translate3d(0,100%,0);
  }
  to{
    opacity:1;
    /* transform:translate3d(0,0,0); */ /* remove it */
  }
}
.fadeInUp{
  animation:fadeInUp 0.3s ease-in both;
}
.linkblock a:nth-child(1){animation-delay:1.0s;}
.linkblock a:nth-child(2){animation-delay:1.1s;}
.linkblock a:nth-child(3){animation-delay:1.2s;}
.linkblock a:nth-child(4){animation-delay:1.3s;}
.linkblock a:nth-child(5){animation-delay:1.4s;}
.linkblock a:nth-child(6){animation-delay:1.5s;}
.linkblock a:nth-child(7){animation-delay:1.6s;}
.linkblock a:nth-child(8){animation-delay:1.7s;}
<div class="linkblock">
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
  <a href="#" class="fadeInUp hlink">fsdfsdf</a>
</div>


我们没有过渡。 - Temani Afif
谢谢帮助。这确实有效,但是过渡效果丢失了。上面提供的“向前”选项可以解决这个问题。 - Tom
@Tom,没问题。 - Irina Potapova

0

您想使用max-height属性进行动画效果。

将最大高度设置为50px,当鼠标悬停时, 将属性的最大高度设置为原始高度

这里是一个可用的fiddle

最终的CSS文件:

.linkblock{
   margin:20% 0;
  }
  .hlink{
    width:12%;
    height: 60px;
    max-height: 60px; // add this
    opacity:0;
    padding:0 10px;
    background:rgba(0,0,0,0.3);
    display:inline-block;
    text-align: center;
    color:rgba(0,0,0,0.6);
    transition:all 0.5s ease;
  }
  .hlink:hover{
    max-height: 50px; // add this
    color:red;
    background:rgba(0,0,0,0.6)
  }

@keyframes fadeInUp {
    from{
      opacity:0;
      transform:translate3d(0,100%,0);
    }
    to{
      opacity:1;
      transform:translate3d(0,0,0);
    }
  }
  .fadeInUp{
    animation:fadeInUp 0.3s ease-in both;
  }
  .linkblock a:nth-child(1){animation-delay:1.0s;}
  .linkblock a:nth-child(2){animation-delay:1.1s;}
  .linkblock a:nth-child(3){animation-delay:1.2s;}
  .linkblock a:nth-child(4){animation-delay:1.3s;}
  .linkblock a:nth-child(5){animation-delay:1.4s;}
  .linkblock a:nth-child(6){animation-delay:1.5s;}
  .linkblock a:nth-child(7){animation-delay:1.6s;}
  .linkblock a:nth-child(8){animation-delay:1.7s;}

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