【问题标题】:CSS animation expand width from 0 to 100 and hide from 0 to 100 [duplicate]CSS动画将宽度从0扩展到100并从0隐藏到100 [重复]
【发布时间】:2020-05-09 15:52:26
【问题描述】:

我试图弄清楚这个网页上的事情是如何完成的hover effect on projects。如您所见,悬停从右到左显示并从同一方向隐藏。我正在努力实现这种效果。这就是我到目前为止所做的:

HTML
<div class="text">Simple text</div>
<div class="img"></div>

SCSS
$square-color: #FF4136;

.text{
  position: absolute;
  width: 100px;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.img{
  position: absolute;
  left: 50%;
  z-index: -1;
  width: 75px;
  height: 100%;
  background-color: $square-color;
  // visibility: hidden;
  opacity: 0;

  &.fade-in{
    animation: .5s linear fadein forwards;
  }

  &.fade-out{
    animation: .5s linear fadeout forwards alternate;
  }

}

@keyframes fadein {
  from{
    opacity: 0;
    width: 0px;
  }
  to{
    opacity: 1;
    width: 75px;
  }
}

@keyframes fadeout{
  from{
    opacity: 1;
    width: 75px;
    transform-origin: left;
  }
  to{
    opacity: 0;
    width: 0px;
  }
}
JS
const txt = document.querySelector('.text');
const img = document.querySelector('.img');

txt.addEventListener('mouseover', function() {
  img.classList.remove('fade-out');
  img.classList.add('fade-in');
})

txt.addEventListener('mouseout', function(){
  img.classList.remove('fade-in');
  img.classList.add('fade-out');
})

感谢您的帮助和任何提示!

【问题讨论】:

标签: html css animation hover


【解决方案1】:

这是使用leftright属性

.underline {
  display: inline;
  position: relative;
  overflow: hidden;
}
.underline:after {
  content: "";
  position: absolute;
  z-index: -1;
  right: 0;
  width: 0;
  bottom: -5px;
  background: #000;
  height: 4px;
  transition-property: width;
  transition-duration: 0.3s;
  transition-timing-function: ease-out;
}
.underline:hover:after,
.underline:focus:after,
.underline:active:after {
  left: 0;
  right: auto;
  width: 100%;
}
<p>hello, <a href="#" class="underline">Please animate</a>
</p>

【讨论】:

  • 这正是我一直在寻找的!谢谢
猜你喜欢
  • 1970-01-01
  • 2019-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-12
  • 1970-01-01
相关资源
最近更新 更多