热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

垂直对齐到translateey(-50%)?-VerticallyalignwithtranslateY(-50%)givingajerk?

HowCanIfixthebelowcode..Ihaveusedthetechniqueoftransform:translateY(-50%)tomakeadiv

How Can I fix the below code.. I have used the technique of transform:translateY(-50%) to make a div vertically center. But When I use it with animation , it first takes top:50% then it translates giving a jerk.. I don't want the jerk to happen and the element should automatically come in center.

我怎样才能修改下面的代码。我使用了转换技术:translateY(-50%)来创建一个div垂直中心。但当我在动画中使用它时,它首先会占据顶部:50%然后它会翻译为“给一个混蛋”。我不希望这种情况发生,元素应该自动进入中心。

body,
html {
  height: 100%;
  background: #c9edff;
  text-align: center;
}

.element {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  font-family: arial;
  font-size: 20px;
  line-height: 1.8em;
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {
    -webkit-transform: scale(0);
  }
  to {
    -webkit-transform: scale(1)
  }
}

@keyframes zoom {
  from {
    transform: scale(0)
  }
  to {
    transform: scale(1)
  }
}
Vertical Align is Awesome!
But with animation its giving a jerk!
Please Fix

2 个解决方案

#1


6  

Your animation rule overwrites the translateY(-50%) with scale(), and when the animation is done, the previous rule gets applied again, hence it jumps.

您的动画规则使用scale()覆盖translateY(-50%),当动画完成时,前面的规则再次被应用,因此它会跳转。

If you add translateY(-50%) to the animation, it will work fine.

如果向动画中添加translateY(-50%),它就可以正常工作。

A side note, based on whether one put the translateY() before or after the scale(), it animates differently, as transform values gets applied from right to left

另外,根据是否将translateY()放在scale()之前或之后,它的动画效果是不同的,因为转换值是从右到左应用

body,
html {
  height: 100%;
  background: #c9edff;
  text-align: center;
}

.element {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  font-family: arial;
  font-size: 20px;
  line-height: 1.8em;
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {
    -webkit-transform: translateY(-50%) scale(0);
  }
  to {
    -webkit-transform: translateY(-50%) scale(1);
  }
}

@keyframes zoom {
  from {
    transform: translateY(-50%) scale(0); 
  }
  to {
    transform: translateY(-50%) scale(1);
  }
}
Vertical Align is Awesome!
But with animation its giving a jerk!
Please Fix

#2


0  

The problem here ist the line-height, but you can actually use calc to fix that.

这里的问题是行高,但实际上你可以用calc来解决这个问题。

transform: translateY(calc(- 50% + 1.8em));

转换:translateY(calc(- 50% + 1.8em));

body,
html {
  height: 100%;
  background: #c9edff;
  text-align: center;
}

.element {
  position: relative;
  top: 50%;
  transform: translateY(calc(- 50% + 1.8em));
  font-family: arial;
  font-size: 20px;
  line-height: 1.8em;
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {
    -webkit-transform: scale(0);
  }
  to {
    -webkit-transform: scale(1)
  }
}

@keyframes zoom {
  from {
    transform: scale(0)
  }
  to {
    transform: scale(1)
  }
}
Vertical Align is Awesome!
But with animation its giving a jerk!
Please Fix


推荐阅读
author-avatar
Just忽略我_559
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有