【问题标题】:Cannot get an image in a keyframe to center horizontally无法使关键帧中的图像水平居中
【发布时间】:2016-01-27 23:42:34
【问题描述】:

我对我拥有的图像感到非常头疼。无论我做什么,它都不会水平居中。我尝试将其设置为绝对和相对。设置left为50%,加入transform: translate(-50%, -50%);,设置图片显示为block,左右边距为auto。

我还能做些什么来让这个东西居中?每当我添加transform: translate(-50%, -50%); 时,它就会居中,但关键帧的旋转完全不合时宜,而不是手在同一位置挥动,而是沿着页面向下移动。

sn-p 没有完全展示它在做什么。

.red {
  background-color: #0085A1;
  width: 100%;
  height: 100vh;
  position: relative;
  text-align: center;
}

.hand {
  width: auto;
  height: auto;
  position: absolute;
  display: inline-block;
  vertical-align: top;
  text-align: center;
  position: absolute;
  /*transform: translate(0, -50%);*/
  /*transform: translate(-50%, -50%);*/
  top: 50%;
  left: 50%;
  margin-right: -50%;
  -webkit-animation: wave 4s 1 normal forwards;
  animation: wave 4s 1 normal forwards;
}

img.hand {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

@keyframes wave {
  50% {
    -ms-transform: rotate(75deg);
    /* IE 9 */
    -webkit-transform: rotate(75deg);
    /* Chrome, Safari, Opera */
    transform: rotate(75deg);
  }
  90%,
  100% {
    opacity: 0
  }
}
<div class="red">
  <img class="hand" alt="HELLO">
</div>

【问题讨论】:

    标签: html css animation css-transitions


    【解决方案1】:

    同时添加translate() 和rotate()。

    jsfiddle

    .red {
      background-color: #0085A1;
      width: 100%;
      height: 100vh;
      position: relative;
    }
    .hand {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%) rotate(0deg);
      animation: wave 4s 1 normal forwards;
    }
    @keyframes wave {
      50% {
        transform: translate(-50%, -50%) rotate(75deg);
      }
      100% {
        opacity: 0
      }
    }
    <div class="red">
      <img src="http://i.imgur.com/ywCRl0A.png" class="hand" alt="HELLO">
    </div>

    【讨论】:

    • 太棒了!这很好用。我是否有任何灵活性可以从顶部向下移动手?附带问题:为什么这在我的手机上不起作用?图像是静态的,什么都不做。
    • 是的,你可能需要为规则添加前缀,为了简单起见,我没有使用任何前缀,这个工具很方便-autoprefixer.github.io
    猜你喜欢
    • 2015-06-21
    • 1970-01-01
    • 2019-02-10
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多