简体   繁体   English

旋转时CSS3居中图像

[英]CSS3 Centering Image while Rotating

So the basic problem I'm having is that I want to have a rotating vortex which is positioned in the middle of their respective divs. 所以我遇到的基本问题是我想有一个位于各自div中间的旋转涡旋。 When I try to have a transform:translate outside of the animation, it doesn't get activated, and if I try to call it within the animation, the image rotates around the top left corner. 当我尝试在动画外部进行transform:translate时,它不会被激活,并且如果我尝试在动画中调用它,图像将在左上角旋转。 I can't seem to figure out how to both center the image vertically and horizontally, and have it rotate at the same time. 我似乎无法弄清楚如何使图像垂直和水平居中,并使其同时旋转。

Here is the code I am working with: 这是我正在使用的代码:

div img
{
    position: absolute;
    display:block;
    top: 50%;
    left: 50%;

    -webkit-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    -o-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);

    margin: auto;
    width:auto;
    height: auto;
    max-height: 70%;
    max-width: 70%;




    -webkit-animation: rotate 3s linear infinite 0s;
    -moz-animation: rotateup 3s linear infinite 0s;
    -o-animation: rotate 3s linear infinite 0s;
    -ms-animation: rotate 3s linear infinite 0s;
    animation: rotate 3s linear infinite 0s
}

@-webkit-keyframes rotate {
 from {
     -webkit-transform: rotate(0deg);
}
to {
      -webkit-transform: rotate(360deg);                                 
}

and here is a fiddle demonstrating my problem. 这是一个小提琴,展示了我的问题。 http://jsfiddle.net/4j7T4/ http://jsfiddle.net/4j7T4/

Thanks for all of the help! 感谢您的所有帮助!

Here is an updated fiddle . 这是更新的小提琴

Since the width of the div is 30px and the height is 30px, you place a margin-left and margin-top of -15px (or negative half of the width/height) 由于div的宽度为30像素,高度为30像素,因此请在左边距左边距和顶部左边距设置-15px(或宽度/高度的负一半)

{
  top: 50%;
  left: 50%;
  margin-top: -15px;
  margin-left: -15px;
}

Another example with just a square http://jsfiddle.net/tb5Bg/ 另一个仅用正方形的示例http://jsfiddle.net/tb5Bg/

I would spin the div and not the image maybe. 我会旋转div而不是图像。 Also, I would center it differently. 另外,我将以不同的方式居中。 Try this: Are you using sass or autoprefixr? 试试这个:您使用的是sass还是autoprefixr? If not, you should check it out. 如果没有,您应该检查一下。 I'm using a block element modifier for the .image-w which is good for responsive images and stuff, and a little @mxin for reuse on absolutely positioned center stuff. 我正在为.image-w使用块元素修饰符,这对响应式图像和内容@mxin用,而@mxin有一点用于在绝对定位的中心内容上重用。 The margin auto does the trick as long as you specify height and width of what you want to center. 只要您指定要居中位置的高度和宽度,边距自动功能就可以完成。 Here is a codepen. 这是一个codepen。

HTML HTML

<div class="image-w spinner"><img src="http://placehold.it/400x400" alt="spinner" /></div>

SCSS SCSS

.image-w {
  //
  img {
    display: block;
    width: 100%;
    height: auto;
  }
}

@mixin absolute-center {
  position: absolute;
  top: 0; right: 0;
  bottom: 0; left: 0;
  margin: auto;
}

.spinner {
  width: 10em;
  height: 10em;
  @include absolute-center;
  border-radius: 50%;
  overflow: hidden;
  animation: rotate 3s linear infinite 0s
}

@keyframes rotate {
  from {
    -webkit-transform: rotate(0deg);
  }
  to {
    -webkit-transform: rotate(360deg);
  }
  animation: rotate 3s linear infinite 0s
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM