2013-03-02 138 views
11

我有一個箭頭指向右側的背景圖像。當用戶點擊按鈕時,所選狀態會將箭頭更改爲指向下方(使用我的圖像精靈中的不同背景位置)。用CSS3旋轉背景圖像

無論如何要使用CSS3動畫,所以一旦按鈕被點擊和jQuery分配它一個「選定」的類,它會從右到下動畫旋轉(只有90度)? (最好使用帶有指向右側的箭頭的單個圖像/位置)

我不確定是否需要使用變換或關鍵動畫幀。

+0

參見:如何旋轉在容器中的背景圖片?](HTTP://計算器。 com/q/5087420/1591669) – unor 2015-02-14 00:51:15

回答

19

您可以使用::after(或::beforepseudo-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); 
} 

HTML :

<div>Test button</div> 

在你的情況下你將使用替代

的jsfiddle演示 element.selected類:http://jsfiddle.net/p8kkf/

希望這有助於

+0

有沒有區別:之後和::之後? – xorinzor 2013-09-06 13:22:03

+3

::後更正確,但:後更好的支持,都指同一件事 – 2013-09-06 21:56:34

+0

超級偉大;)謝謝你! – 2013-12-18 12:16:15

9

這裏是我已經習慣了旋轉背景圖像的旋轉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); 
    } 
} 
+0

感謝您的代碼。我還沒有嘗試過,但是當選定的類被刪除時,這也會將箭頭從下往上旋轉到正確的位置嗎? – Cofey 2013-03-02 05:48:54

+0

旋轉後,您將不得不手動放置箭頭。我喜歡@ wes建議使用':after'僞元素。 – Teddy 2013-03-02 23:35:08