【问题标题】:Transitions when switching from top to bottom position从顶部位置切换到底部位置时的转换
【发布时间】:2015-06-19 05:14:19
【问题描述】:

如果有更好的方法来做我要问的问题,请告诉我,但到目前为止这是我能想到的最好的方法

我想要一组 div,其中包含一个滑动 div。滑动 div 将包含从最新帖子中提取的内容。所以它们可能并不总是完全相同的高度。

起始位置是显示标题,然后在父级悬停时显示整个 div。

我只使用底部位置时遇到的问题是,当屏幕变得太薄时,不仅会显示标题。使用top,我确实失去了一些头衔,但我愿意牺牲它。

因此,我决定同时使用顶部和底部,只需翻转 auto 所在的位置即可显示完整的 div。 (我不希望滑动 div 与包含 div 的高度相同)

但是,当我这样做时,过渡不起作用。我尝试在过渡中使用 top、bottom 和 all,但结果都是一样的 - 没有过渡。

有人可以解释一下 a) 为什么这不起作用 b) 如果不使用 jQuery,什么可以使它工作。

HTML:

<div class="innerLeftPosts">
    <div class="mainPostHome">
        <div class="postSlideCover">
            <h3>Hello this is a test</h3>
            <p>This is some test content that would go in here and take up some space</p>
        </div>
    </div>
    <div class="secondaryPostHome">
        <div class="postSlideCover">
            <h3>Hello this is a test</h3>
            <p>This is some test content that would go in here and take up some space</p>
        </div>
    </div>
</div>

CSS:

.postSlideCover{
width:calc(100% - 20px);
height:auto;
position:absolute;
    transition:all 0.4s ease-out;
    -webkit-transition:all 0.4s ease-out;
}
.mainPostHome .postSlideCover{
    top:83%;
    bottom:auto;
}

.mainPostHome:hover .postSlideCover{
    top:auto;
    bottom:0;
}

完整的视觉示例:https://jsfiddle.net/60nxpfs8/

【问题讨论】:

  • 您无法转换到自动或从自动转换。
  • 我假设是因为 css 不知道如何计算要移动多少?
  • 如果是这样的话,我假设 jQuery 不仅更好,而且是唯一的选择。
  • Jquery 无法从 auto 本地转换为 AFAIK,尽管已经为它编写了一些插件。但我看到你现在有了答案。
  • 我会选择与自动驾驶不同的路线。可能是经过计算的。

标签: css


【解决方案1】:

这里:

.postSlideCover{
    width:calc(100% - 20px);
    height:auto;
    position:absolute;
    transition:all 0.4s ease-out;
    -webkit-transition:all 0.4s ease-out;
    bottom: 0;
}
.mainPostHome .postSlideCover{
    transform: translateY(60%);
}

.mainPostHome:hover .postSlideCover{
    transform: translateY(0);
}

使用 JSFiddle

我们可以使用transform属性translateY来使用元素的高度作为我们移动它的度量(100%与元素高度的100%相关)。这样做的另一个好处是能够使用硬件加速(而不是软件)来制作动画。

【讨论】:

  • 这会启动完全隐藏的 div,这不是我想要的。我希望标题仍然显示。
  • @BrantBarton – 检查更新。使用百分比,直到显示的数量是您想要的。
  • 这是对我来说应该更明显的实例之一。不过感谢您的帮助
猜你喜欢
  • 2020-02-03
  • 2018-10-08
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
相关资源
最近更新 更多