2014-12-19 90 views
0

我想創建一個SVG懸停動畫效果使用CSS。SVG旋轉虛線圓形邊框上懸停

我想要做的是,當我將鼠標懸停在我的圖標上時,實心圓形容器將以虛線容器旋轉。見下圖。

http://prntscr.com/5ii2i7

檢查什麼我迄今所做的:http://jsfiddle.net/uhkwLuph/3/

到目前爲止,這裏是我的代碼。

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="200px" 
    height="200px" viewBox="0 0 200 200" style="enable-background:new 0 0 200 200;" xml:space="preserve" id="icon"> 

<style type="text/css">  
    .st0 
    { 
     fill:none; 
     stroke:#F2F2F2; 
     stroke-width:4; 
     stroke-miterlimit:10; 
    } 

    #icon .st0{ 
     -webkit-transition: .5s; 
    } 

    #icon:hover .st0{ 
     fill: #ffffff; 
     stroke: #1f8a4c; 
     cursor: pointer; 
    }  
</style> 


<g id="container"> 
    <circle class="st0" cx="101" cy="99" r="89.333"/> 
</g> 

<g id="icon-details"> 
    <path class="st0" d="M146.899,134.202c3.856,4.702,2.772,11.963-2.418,16.218l0,0c-5.192,4.258-12.523,3.896-16.38-0.806 
     l-30.004-36.594c-3.855-4.701-2.772-11.964,2.418-16.22l0,0c5.19-4.256,12.523-3.895,16.377,0.808L146.899,134.202z"/> 
    <circle class="st0" cx="77.843" cy="72.434" r="33.331"/> 
    <circle class="st0" cx="77.844" cy="72.434" r="22.343"/> 
</g> 

</svg> 

據我所知,它可以與獲得中風dasharray和中風偏移+ @keyframes(CSS),但任何人都可以點我怎麼才能實現呢?

這裏的JSFIDDLE

+0

我不是在這裏爲你做的,但我想推動你在正確的方向**如果**你告訴我你的嘗試。 – Nick 2014-12-19 08:22:56

+0

嗨尼克謝謝你的答覆。這是我到目前爲止所嘗試的。然而,我沒有得到我想要的:http://jsfiddle.net/uhkwLuph/3/試圖調整發現沒有運氣。 – 2014-12-19 08:30:40

+0

@Kimberly Wright看看我的答案是虛線邊框和單個元素。 – 2014-12-19 09:23:48

回答

1

小提琴:Click 現在你只需要與dashoffset/dasharray價值的發揮。 CSS:

body { background: #27ae60; } 
#container{ 

} 

#container:hover circle{ 
animation: dash 2s linear forwards; 
-webkit-animation: dash 2s linear forwards; 
} 



@keyframes dash { 
    0%{  stroke-dashoffset:0; 
    stroke-dasharray:0;} 
    100% { 
    stroke-dashoffset:10; 
    stroke-dasharray:180; 
    } 
} 
@-webkit-keyframes dash { 
    0%{  stroke-dashoffset:0; 
    stroke-dasharray:0;} 
    100% { 
    stroke-dashoffset:10; 
    stroke-dasharray:180; 
    } 
} 
+0

上述代碼不起作用。 – 2014-12-19 08:42:27

+0

@KimberlyWright什麼瀏覽器?它在Firefox中工作。 – Nick 2014-12-19 08:49:56

+0

我正在使用Chrome。 – 2014-12-19 08:51:41

0

UPDATE

enter image description here

:root { background: #27ae60; transition: background .3s ease} 
 

 
[class=loop]{ 
 
    position:relative; 
 
    margin: 240px auto 
 
} 
 
[class=loop], [class=circle]{ 
 
    width: 240px; 
 
    height: 240px 
 
} 
 
[class=loop]:before, [class=loop]:after{ 
 
    content: ''; 
 
    z-index: 1 
 
} 
 
[class=loop]:before, [class=loop]:after,[class=circle]{ 
 
    position: absolute; 
 
} 
 
[class=loop]:after,[class=circle]{ 
 
    border-radius: 50% 
 
} 
 
[class=loop]:before{ 
 
    width: 80px; 
 
    height: 20px; 
 
    border-radius: 10px; 
 
    border: 4px solid green; 
 
    transform: rotateZ(50deg); 
 
    top: 160px; 
 
    left: 114px 
 
} 
 
[class=loop]:after{ 
 
    top: 32px; 
 
    left: 32px; 
 
    width: 100px; 
 
    height: 100px; 
 
    border: 10px solid transparent; 
 
    box-shadow: inset 0 0 0 4px green,0 0 0 4px green 
 
} 
 
[class=circle]{ 
 
    border: 4px dashed green; 
 
    top: 0px; 
 
    left: 0px; 
 
    background: white; 
 
    z-index: 0; 
 
    background-clip: content-box 
 
} 
 
[class=loop]:hover [class=circle]{ 
 
    -webkit-animation: spin 1s infinite linear; 
 
    -moz-animation: spin 1s infinite linear; 
 
    animation: spin 1s infinite linear 
 
} 
 
@-webkit-keyframes spin { 
 
    to { transform: rotate(360deg); } 
 
} 
 
@-moz-keyframes spin { 
 
    to { transform: rotate(360deg); } 
 
} 
 
@keyframes spin { 
 
    to { transform: rotate(360deg); } 
 
}
<div class=loop> 
 
    <div class=circle></div> 
 
</div>

單元素的解決辦法是

:root { background: #27ae60; transition: background .3s ease} 
 
:root:hover { background: red } 
 
div{ 
 
    width: 80px; 
 
    height: 20px; 
 
    border-radius: 10px; 
 
    border: 4px solid white; 
 
    transform: rotateZ(45deg); 
 
    margin: 230px auto; 
 
    position: relative 
 
} 
 
div:before,div:after{ 
 
    content: ''; 
 
    position: absolute; 
 
    border-radius: 50% 
 
} 
 
div:before{ 
 
    width: 100px; 
 
    height: 100px; 
 
    border: 10px solid transparent; 
 
    top: -52px; 
 
    left: -124px; 
 
    box-shadow: inset 0 0 0 4px white,0 0 0 4px white 
 
} 
 
div:after{ 
 
    width: 250px; 
 
    height: 250px; 
 
    border: 4px dashed white; 
 
    top: -124px; 
 
    left: -154px 
 
} 
 
div:hover:after{ 
 
    -webkit-animation: spin 1s infinite linear; 
 
    -moz-animation: spin 1s infinite linear; 
 
    animation: spin 1s infinite linear; 
 
} 
 
@-webkit-keyframes spin { 
 
    to { transform: rotate(360deg); } 
 
} 
 
@-moz-keyframes spin { 
 
    to { transform: rotate(360deg); } 
 
} 
 
@keyframes spin { 
 
    to { transform: rotate(360deg); } 
 
}
<div/>

+0

感謝tambo。是否有可能不改變背景只是SVG本身的背景? – 2014-12-19 09:47:23

+0

@Kimberly Wright是的,請檢查更新或訪問這個小提琴http://jsfiddle.net/shhzrxpd/ – 2014-12-19 10:12:37