使用CSS3旋转背景图像

Cof*_*fey 11 css rotation background-image css3 css-animations

我的背景图片有一个指向右侧的箭头.当用户单击按钮时,所选状态会将箭头更改为指向下方(使用图像精灵中的不同背景位置).

无论如何使用CSS3设置动画,所以一旦单击按钮并且jQuery为其指定一个"选定"类,它将从右到下以动画(仅90度)旋转?(最好使用带有指向右侧的箭头的单个图像/位置)

我不确定是否需要使用变换或关键动画帧.

小智 19

你可以用::after(或::before)pseudo-element来生成动画

div /*some irrelevant css */
{
    background:-webkit-linear-gradient(top,orange,orangered);
    background:-moz-linear-gradient(top,orange,orangered);
    float:left;padding:10px 20px;color:white;text-shadow:0 1px black;
    font-size:20px;font-family:sans-serif;border:1px orangered solid;
    border-radius:5px;cursor:pointer;
}

/* element to animate */
div::after               /* you will use for example "a::after" */
{
    content:' ?';        /* instead of content you could use a bgimage here */
    float:right;
    margin:0 0 0 10px;
    -moz-transition:0.5s all;
    -webkit-transition:0.5s all;
}

/* actual animation */
div:hover::after         /* you will use for example "a.selected::after" */
{
    -moz-transform:rotate(90deg);
    -webkit-transform:rotate(90deg);
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<div>Test button</div>
Run Code Online (Sandbox Code Playgroud)

在您的情况下,您将使用element.selected类而不是

jsfiddle demo:http://jsfiddle.net/p8kkf/

希望这可以帮助

  • ::之后更正确,但是:经过更好的支持,两者都指的是同一件事 (4认同)

Ted*_*ddy 10

这是我用来旋转背景图像的旋转css类:

.rotating {
  -webkit-animation: rotating-function 1.25s linear infinite;
     -moz-animation: rotating-function 1.25s linear infinite;
      -ms-animation: rotating-function 1.25s linear infinite;
       -o-animation: rotating-function 1.25s linear infinite;
          animation: rotating-function 1.25s linear infinite;
}

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

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

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

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

@keyframes rotating-function {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
Run Code Online (Sandbox Code Playgroud)