如何在悬停时从上到下动画对齐?

How to animate alignment from top to bottom on hover?

我正在寻找一种使用 css 制作悬停过渡动画的方法。我希望保持它的纯净 css。如果没有,我将使用 jquery 作为备份。

这将是我的目标:

一个容器,内容为div。当悬停时它会动画/向上滑动。如图:

我试过类似下面的代码。问题是过渡不会为 auto 部分设置动画。内容具有可变高度。所以每次都不一样。 (每个网格项目)

    .my_container{
        position: relative;
        width: 100%;
        padding-top: 160%;
        overflow: hidden;
    }

    .my_container > .my_content{
        position: absolute;
        top: 0;
        bottom: auto;
        left: 0;
        right: 0;
    }


    .my_container > my_content:hover{
        top: auto;
        bottom: 0;
    }

    .my_container * {
        -webkit-transition: all .6s ease-in-out;
        -moz-transition: all .6s ease-in-out;
        -o-transition: all .6s ease-in-out;
        transition: all .6s ease-in-out;
    }

我考虑过 transform: translateY(); 但据我所知这只适用于百分比和像素。

目标是让它在悬停时从上到下对齐。

(输入这个让我想到了另一件事。这在移动设备上没用,对吧?:))

如果子元素和父元素之间存在已知关系,您就可以轻松应用翻译。

这是一个基本示例

.box {
  height:100px;
  width:50px;
  margin:50px;
  border:3px solid;
  position:relative;
}
.box:before {
  content:"";
  position:absolute;
  top:0;
  width:100%;
  height:143%;
  background:red;
  transition:1s all;
}
.box:hover::before {
  transform:translateY(-30%) 
  /* 143% is 100%
     100% is X% ---> X = 70% so we move by (100% - 70%)
  */
}
<div class="box">

</div>

你可以用一个CSS变量来表达:

.box {
  height:100px;
  width:50px;
  margin:50px;
  display:inline-block;
  border:3px solid;
  position:relative;
}
.box:before {
  content:"";
  position:absolute;
  top:0;
  width:100%;
  height:calc(var(--p)*1%);
  background:red;
  transition:1s all;
}
.box:hover::before {
  transform:translateY(calc((10000/var(--p))*1% - 100%)) 
}
<div class="box" style="--p:143;">

</div>

<div class="box" style="--p:170;">

</div>

<div class="box" style="--p:120;">

</div>

更新

如果是动态内容,您可以添加如下小的 JS 代码:

$('.box').each(function() {
  var h0 = $(this).height();
  var h1 = $(this).find('span').height();
  
  $(this).css('--t',(h0-h1));
})
.box {
  height: 100px;
  width: 50px;
  margin: 50px;
  display: inline-block;
  border: 3px solid;
  position: relative;
}

.box span {
  position: absolute;
  top: 0;
  width: 100%;
  background: red;
  transition: 1s all;
}

.box:hover span{
  transform: translateY(var(--t));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box">
  <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est ex, pretium tempus turpis vitae, </span>
</div>

<div class="box">
  <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>
</div>

<div class="box">
  <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est ex, pretium </span>
</div>

这是我的 jQuery 方法。它比较容器和内容的大小。如果它比容器大,它会将差异动画化,以便所有内容都可见。

var $ = jQuery;

$(document).ready(function () {

    $('.post_grid_item').hover(

        function() {

            $containter = $(this).find('.content_slide');
            $content = $(this).find('.content_slide > .vce-row-content');

            $content_height    = $content.outerHeight();
            $containter_height = $containter.outerHeight() ;

            // if content is bigger than container
            if( $content_height >  $containter_height ) {

                $content_hover_offset = $containter_height - $content_height;

                $content.animate({
                    top: $content_hover_offset + 'px',
                }, 'fast');
            }

        },function() {

            $containter = $(this).find('.content_slide');
            $content = $(this).find('.content_slide > .vce-row-content');

            $content.animate({
                top: '0',
            },'fast');
        }
    );


});

这会在添加特定移动设备条件时为我提供更多条件灵活性。

如果有人看到一些改进请告诉我。