如何为底部设置动画:底部值:自动

How to animate bottom: value to bottom: auto

我正在尝试创建一个隐藏在文档顶部屏幕之外的菜单。有一小部分显示可以悬停,这将使菜单的其余部分可见。尝试将底部设置为自动动画,但它不起作用。想知道是否有人知道如何或更好的方法来创建类似于我的 codepen 的屏幕外菜单。

http://codepen.io/anthony-dandrea/pen/EjqYqj

.hud 是给我带来动画问题的 class。

.hud {
  position: absolute;
  width: 100%;
  text-align: center;
  transition: all 1s;
  bottom: calc(100% - 39px);
}
.hud:hover {
  bottom: 80%;
  bottom: auto;
}
$('#click').click(function () {

    var windowHeight = $(window).height();
    var lineHeight = $('#line').height();
    var desiredBottom = 100;

    var newPosition = windowHeight - (lineHeight + desiredBottom);

    $('#line').animate({top:newPosition},1000,function () {
        $('#line').css({
            bottom: desiredBottom,
            bottom: 'auto'
        });
    });

});

这里jsfiddle

正如 Patrick Allen 在评论中提到的,您不能使用 CSS 从 "auto" 值 animate/transition 或 "auto" 值。对于您的情况,您可以将其替换为 transform: translate() ,就像下面的代码片段一样,并达到相同的效果。

下面是相关的 SCSS 代码及其作用:

  • transform: translateY(-100%) 将元素内容向上移动容器元素的确切高度。这会隐藏整个容器。
  • 添加了 top: 39px,这样 V 形图标仍然显示,只有内容被隐藏。
  • 悬停时 transform 通过执行 transform: translateY(0%) 无效。这会将元素放回原来的位置。
  • 但是由于 top: 39px 出现在未悬停状态,容器的位置会稍微偏移,可以通过在悬停时添加 top: 0px 来消除。
.hud {
  position: absolute;
  width: 100%;
  text-align: center;
  transition: all 1s;
  top: 39px;
  transform: translateY(-100%);
  &:hover {
     top: 0px;
     transform: translateY(0%);
  }
}

body {
  background: #121111;
}
.hud {
  position: absolute;
  color: red;
  width: 100%;
  text-align: center;
  -webkit-transition: all 1s;
  transition: all 1s;
  top: 39px;
  -webkit-transform: translateY(-100%);
  -ms-transform: translateY(-100%);
  transform: translateY(-100%);
}
.hud:hover {
  top: 0px;
  -webkit-transform: translateY(0%);
  -ms-transform: translateY(0%);
  transform: translateY(0%);
}
.pull-down {
  color: #e6e6e6;
  -webkit-transition: all 0.2s;
  transition: all 0.2s;
  cursor: pointer;
  height: 24px;
  margin-top: 15px;
  font-size: 1.5em;
}
.pull-down:hover {
  color: #fff;
}
.hud:hover .pull-down {
  color: #fff;
  -ms-transform: rotate(-180deg);
  -webkit-transform: rotate(-180deg);
  transform: rotate(-180deg);
}
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="hud">
  <div class="hud-internal">
    <p>foobar</p>
  </div>
  <i class="fa fa-chevron-down pull-down" data-hud-toggle></i>
</div>

top: 0; 赋给 .hud 元素,它将正常工作。这是代码笔:http://codepen.io/anon/pen/KpOKdE