2017-06-02 88 views
0

我在下面的代碼中,當你將鼠標放在clid元素上時,它正在旋轉。 我希望父母和孩子在懸停時觸發旋轉。CSS Onmouseover旋轉子元素

https://jsfiddle.net/d4sbvmb0/

.parent { 
 
    width: 250px; 
 
    height: 250px; 
 
    background: silver; 
 
    padding: 25px; 
 
} 
 

 
.child { 
 
    background: red; 
 
    border-radius: 2% 50%; 
 
    height: 100px; 
 
    margin: 100px; 
 
    transition: all 0.5s; 
 
    transition-timing-function: ease-in-out; 
 
    width: 100px; 
 
} 
 

 
.child:hover { 
 
    transform: rotate(20deg); 
 
}
<div class="parent"> 
 
    <div class="child"></div> 
 
</div>

+0

你問當父盤旋旋轉的孩子呢?如果家長和孩子都被輪換,是否應該額外輪換? – hungerstar

+0

是否這樣? https://jsfiddle.net/d4sbvmb0/1/ –

+0

當鼠標懸停在孩子身上時,你需要JS來觸發父母的懸停。你可以用事件冒泡做到這一點 –

回答

0

如果你只需要父元素觸發孩子的旋轉只需修改包含變換從.child:hover.parent:hover .child選擇。對於後者,我們說,當.parent懸停時,請選擇.child並旋轉它。

.parent { 
 
    width: 250px; 
 
    height: 250px; 
 
    background: silver; 
 
    padding: 25px; 
 
} 
 

 
.child { 
 
    background: red; 
 
    border-radius: 2% 50%; 
 
    height: 100px; 
 
    margin: 100px; 
 
    transition: all 0.5s; 
 
    transition-timing-function: ease-in-out; 
 
    width: 100px; 
 
} 
 

 
.parent:hover .child { 
 
    transform: rotate(20deg); 
 
} 
 

 
/* Add me if you want additional rotation when both are hovered. */ 
 
/* 
 
.parent:hover .child:hover { 
 
    transform: rotate(40deg); 
 
} 
 
*/
<div class="parent"> 
 
    <div class="child"></div> 
 
</div>

注:transition-timing應該transition-timing-function

-1

試試這個

.parent:hover .child { 
    transform: rotate(20deg); 
} 
+0

請解釋你的代碼。 – brimstone