【问题标题】:Pure CSS3 animated slide-up caption on hover悬停时纯 CSS3 动画上滑字幕
【发布时间】:2016-08-17 22:50:53
【问题描述】:

背景非常简单,我希望在用户悬停/点击时从元素底部向上滑动一个小标题。见图1。

有点挖掘说这无法使用 CSS 完成,但我真的不明白为什么。几个小时后,我想我已经非常接近解决它了,但我无法跨过最后的障碍。

我的逻辑是,如果您的父元素有overflow: hidden,并且您绝对将标题放置在父元素的底部,您可以使用transition 属性为位置值设置动画,使其向上滑动。纯 CSS 宝贝!

您不能为高度设置动画 - 文本被压碎,元素必须作为块移动(尽管不一定呈现为 display:block)。

这就是我到目前为止https://jsfiddle.net/zufwavpn/ 的目标。 HTML,

<div class="item-wrapper">
  <div class="content">
    Hello I am content. All that matters for this method to work is that the item wrapper has a fixed size. In my working project, the width is set to a % value, and the height to rem.
  </div>
  <div class="popup-title">
    <span>A title for my content</span>
  </div>
</div>

还有 CSS(我已经在这里转换为 vanilla CSS),

.item-wrapper{
    height:22rem;
    position:relative;
    overflow:hidden;
    color:white;
    font-family:sans-serif;
  }

  .content{
    height:100%;
    background-color:red;
    padding:10px;
  }

  .popup-title{
    position:absolute;
    top:100%;
    bottom:0%;
    width:100%;
    transition: bottom 0.5s, top 0.5s;
    vertical-align: bottom;
  }

.popup-title span{
    display:block;
    margin:0;
    background-color: black;
}

.item-wrapper:hover .popup-title{
    bottom:0%;
    top:0%;
}

感觉接近的原因是在这个阶段,popup 基本可以工作,但是里面的内容应该和容器的底部对齐。本质上,这是将绝对定位元素的顶部和底部设置为“0”的古老技巧,但用于从容器下方为某些内容设置动画。

为什么要为顶部和底部属性设置动画?如果您只使用 'top' 值,您可以通过设置 top:100% 来隐藏该元素,但您不能为它设置动画,因此它会停留在父元素的底部。您需要将 top 的特定值设置为(父项的高度减去弹出内容的高度),并且弹出内容/父项可以是任何大小。您可以设置bottom:-100% - 这确实有效,您可以将动画设置为bottom:0%,并在父级底部弹出带有休息的弹出窗口。一切都很好,无需设置最高值。但是,这并不令人满意,您必须将滑块放置在父级下方并对其进行动画处理,由于各种原因与其他动画有关,会产生不合时宜的效果。

所以,这里我们将弹出元素定位在父元素的底部,因为顶部和底部值重合,所以没有高度,并且内容向下溢出。完美的。然后顶部的值动画起来,弹出元素现在有top:0; bottom:0,填充了父元素,如果我能让内容粘在底部就好了。

最后一点通常不会太难。我们有vertical-align 和flex 的整个世界,但它们似乎都会产生错误和错误并导致我陷入困境。有什么想法吗?在这一点上,我必须继续前进,只使用 javascript,但我觉得这是一个值得单独解决的问题。

【问题讨论】:

    标签: html css css-animations frontend


    【解决方案1】:

    .item-wrapper {
      height:22rem;
      position:relative;
      overflow:hidden;
      color:white;
      font-family:sans-serif;
    }
    
    .content {
      height:100%;
      background-color:red;
      padding:10px;
    }
    
    .popup-title {
      position:absolute;
      top:100%;
      width:100%;
      transition: transform 250ms;
      vertical-align: bottom;
    }
    .popup-title span {
      display:block;
      margin:0;
      background-color: black;
    }
    
    .item-wrapper:hover .popup-title {
      transform:translateY(-100%);
    }
    <div class="item-wrapper">
      <div class="content">
        Hello I am content. All that matters for this method to work is that the item wrapper has a fixed size. In my working project, the width is set to a % value, and the height to rem.
      </div>
      <div class="popup-title">
        <span>A title for my content</span>
      </div>
    </div>

    【讨论】:

    • 这是完美的,谢谢。我还注意到,在这种情况下,您可以完全不用弹出元素上的position:absolute; top:100%;,因为主要内容已经将它推到容器下方,从而使代码更加简单。
    【解决方案2】:

    调整了.popup-titletop:bottom: 属性,并将背景颜色移动到了div,而不是它的子跨度。这里不需要特定的高度。它应该是动态的。

    .item-wrapper{
        height:12rem;
        position:relative;
        overflow:hidden;
        color:white;
        font-family:sans-serif;
      }
      
      .content{
        height:100%;
        background-color:red;
        padding:10px;
      }
      
      .popup-title{
        position:absolute;
        bottom: -101%;
        width:100%;
        transition: bottom 0.5s, top 0.5s;
        vertical-align: bottom;
          background-color: black;
      }
    
    .popup-title span, .popup-title p{
          display:block;
          margin:0;
          padding: 10px;
    }
    
    .item-wrapper:hover .popup-title{
            bottom: 0;
    }
    <div class="item-wrapper">
      <div class="content">
        Hello I am content. All that matters for this method to work is that the item wrapper has a fixed size. In my working project, the width is set to a % value, and the height to rem.
      </div>
      <div class="popup-title">
        <span>A title for my content. Height of the content here is irrelevant.</span>
      </div>
    </div>
    <br />
    
    <div class="item-wrapper">
      <div class="content">
        Hello I am content. All that matters for this method to work is that the item wrapper has a fixed size. In my working project, the width is set to a % value, and the height to rem.
      </div>
      <div class="popup-title">
        <p>The popup div should expand as necessary. Even if there are multiple sentences or paragraphs.</p>
        <p>Just dont' make it taller than the wrapper div</p>
      </div>
    </div>

    【讨论】:

    • 与 Aziz 的回答一样,这没有正确设置动画(请参阅我的回复,但要点是弹出元素的起始位置太低)。我还提到我在问题中尝试过这个。答案似乎是改用 transform 属性。感谢您清理代码。
    【解决方案3】:

    只需将.popup-title 设为负bottom 位置(根本不需要top 值)并在悬停时将其转换为0

    .item-wrapper {
      height: 22rem;
      position: relative;
      overflow: hidden;
      color: white;
      font-family: sans-serif;
    }
    .content {
      height: 100%;
      background-color: red;
      padding: 10px;
    }
    .popup-title {
      position: absolute;
      bottom: -100%;
      width: 100%;
      transition: bottom 0.5s, top 0.5s;
      vertical-align: bottom;
    }
    .popup-title span {
      display: block;
      margin: 0;
      background-color: black;
    }
    .item-wrapper:hover .popup-title {
      bottom: 0;
    }
    <div class="item-wrapper">
      <div class="content">
        Hello I am content. All that matters for this method to work is that the item wrapper has a fixed size. In my working project, the width is set to a % value, and the height to rem.
      </div>
      <div class="popup-title">
        <span>A title for my content</span>
      </div>
    </div>

    【讨论】:

    • 我试过了,它可以工作,但是由于您将元素放置在父元素下方(父元素高度的 -100%),因此动画到 0% 的时间和速度无法预测.我可以使用过渡延迟,但缓和效果仍然很差。
    • 嗯,你知道弹窗的高度吗?你可以使用-100px之类的东西,否则你将不得不使用`transform: translateY(-100%);
    • 我不知道高度,我完全忘记了变换。我现在就试试。
    • @OlivierButler 检查 Stínek 的答案,他使用了这种技术,尽管他没有解释他的代码是做什么的。
    • 现在才看到,很好玩。不过谢谢你的建议。我仍然很好奇为什么垂直对齐孩子如此困难 - 我可以想象问题的根源在于将顶部和底部设置为 0 本质上不是推荐的调整元素大小的方法,所以它的孩子会表现得很奇怪,但这让我很恼火。
    猜你喜欢
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2015-02-09
    相关资源
    最近更新 更多