【问题标题】:transition effect on element A while hovering element B悬停元素 B 时对元素 A 的过渡效果
【发布时间】:2021-11-02 06:22:32
【问题描述】:

我正在努力在 .blue div 上进行转换,同时悬停在父级 - .red div 上。

我想要实现的是一个缓慢的过渡,比如从显示 1 秒:无,到阻塞。

样式表在 SCSS 中并且是嵌套的。有人可以帮帮我吗?

Codepen fiddle

HTML:

<div class='app'>
  <div class='red'>
    <div class="blue"></div>
  </div>
</div>

SCSS:

.app {
  heigh: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;

  .red {
    position: relative;
    background-color: red;
    height: 30rem;
    width: 30rem;
    &:hover {
      .blue {
        display: block;
      }
    }
    .blue {
      display: none;
      height: 10rem;
      width: 10rem;
      position: absolute;
      background-color: blue;
      // transition: 1s; DOESNT WORK
    }
  }
}


【问题讨论】:

    标签: html css sass


    【解决方案1】:
    .app {
      height: 100vh;
      width: 100vw;
      display: flex;
      justify-content: center;
      align-items: center;
    
      .red {
        position: relative;
        background-color: red;
        height: 30rem;
        width: 30rem;
        &:hover {
          .blue {
            opacity: 1;
          }
        }
        .blue {
          opacity: 0; //add opacity
          height: 10rem;
          width: 10rem;
          position: absolute;
          background-color: blue;
          transition: 1s;
        }
      }
    }
    
    

    【讨论】:

    • 您错过了答案中的重要部分。它不适用于 display 的原因是因为transition 具有一组您可以使用它的属性。查看list of animated properties
    • 另外,opacity 不一定是最佳实践,因为您可能不希望元素存在于 DOM 中。您应该使用 max-width 代替它,它仍然不会从 DOM 中删除元素,但您不能再与它交互,因为它没有可点击的空间。
    • @kmp 过渡不适用于显示,因此您需要使用其他 css 属性 alognwith opacity。如果要求在 dom 中没有元素进行交互,那么 scale(0) 将起作用或将高度设置为 0。
    • ...the reason it doesn't work with display is because transition has a set of properties that you can use it on.下次可能会仔细阅读我的评论
    • @kmp 您需要在不透明的情况下发布您的答案,而只是“转换属性集”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 2012-06-19
    • 2013-07-12
    • 2014-01-15
    • 2013-06-17
    • 2022-11-30
    相关资源
    最近更新 更多