12

I have a background image that has an arrow that points to the right. When a user clicks on the button, the selected state changes the arrow to point down (using a different background position in my image sprite).

Is there anyway to animate this using CSS3 so once the button is clicked and jQuery assigns it a "selected" class, it will rotate in an animation (only 90 degrees) from the right to down? (preferably using the single image/position with the arrow that points to the right)

I'm unsure as to whether transform or key animation frames need to be used.

Cofey
  • 11,144
  • 16
  • 52
  • 74
  • See also: [How to rotate the background image in the container?](http://stackoverflow.com/q/5087420/1591669) – unor Feb 14 '15 at 00:51

2 Answers2

20

you could use the ::after (or ::before) pseudo-element, to generate the animation

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);
}

HTML:

<div>Test button</div>

in your case you will use element.selected class instead of

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

hope this helps

Siful Islam
  • 1,906
  • 3
  • 21
  • 31
10

Here is a rotating css class that I have used to spin a background image:

.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);
  }
}
Teddy
  • 18,357
  • 2
  • 30
  • 42
  • Thanks for the code. I haven't tried it out yet, but will this also rotate the arrow from the down to the right position when the selected class is removed? – Cofey Mar 02 '13 at 05:48
  • You will have to manually position the arrow after rotation. I like @wes's suggestion to use the `:after` pseudo element. – Teddy Mar 02 '13 at 23:35