2015-11-19 231 views
1

如果將鼠標懸停在元素上,則轉換可以工作,但不會在離開元素時轉換。我該如何解決它?僞元素轉換

這是我的代碼和我JSFiddle

.block{ 
 
    border: 2px solid grey; 
 
    border-radius: 4px; 
 
    height: 90px; 
 
    margin: 0 auto; 
 
    text-align: center; 
 
    width: 110px; 
 
    padding-top: 6px; 
 
} 
 

 
.fr:hover{ 
 
    -webkit-transition: all 0.4s ease 0s; 
 
    -moz-transition: all 0.4s ease 0s; 
 
    -ms-transition: all 0.4s ease 0s; 
 
    -o-transition: all 0.4s ease 0s; 
 
    transition: all 0.4s ease 0s; 
 
    position: relative; 
 
\t border-top: 2px solid rgba(150, 200, 200, 1); 
 
\t border-bottom: 2px solid rgba(0, 0, 0, 1); 
 
} 
 

 
.fr:before, .fr:after { 
 
    -webkit-transition: opacity 0.2s ease-in-out; 
 
    -moz-transition: opacity 0.2s ease-in-out; 
 
    -ms-transition: opacity 0.2s ease-in-out; 
 
    -o-transition: opacity 0.2s ease-in-out; 
 
    transition: opacity 0.2s ease-in-out; 
 
    content: ""; 
 
    position: absolute; 
 
    background-image: linear-gradient(to bottom, rgba(150, 200, 200, 1) 0%, rgba(10, 20, 20, 1) 80%), linear-gradient(to bottom, rgba(150, 200, 200, 1) 10%, rgba(10, 20, 20, 1) 100%); 
 
    opacity: 0; 
 
} 
 

 
.fr:hover:before, .fr:hover:after { 
 
    -webkit-transition: opacity 0.2s ease-in-out; 
 
    -moz-transition: opacity 0.2s ease-in-out; 
 
    -ms-transition: opacity 0.2s ease-in-out; 
 
    -o-transition: opacity 0.2s ease-in-out; 
 
    transition: opacity 0.2s ease-in; 
 
    content: ""; 
 
    position: absolute; 
 
    background-image: linear-gradient(to bottom, rgba(155, 200, 200, 1) 0%, rgba(10, 20, 20, 1) 80%), linear-gradient(to bottom, rgba(155, 200, 200, 1) 10%, rgba(10, 20, 20, 1) 100%); 
 
    top: 0px; bottom: 0px; width: 2px; 
 
    opacity: 1; 
 
} 
 

 
.fr:before { left: -2px; } 
 
.fr:after { right: -2px; }
<div class="block fr"></div>

+0

將'transition'屬性移出':hover'選擇器,並移動到'.block'或'.fr'。 – Sampson

+0

Hello Sampson,謝謝你的迴應。它適用於頂部和底部欄,但不適用於僞元素(左側和右側欄)。 – Rafael

+0

你能告訴我開始和結束狀態應該是什麼樣子嗎? – Sampson

回答

1

無數事情需要在這裏完成。首先,您需要隨時應用轉換到.block .fr元素(所以它適用於鼠標輸入和鼠標輸出)。現在你只:hover狀態期間應用的轉換:

.block { 
    /* Add `transition: all 0.4s ease 0s;` */ 
    /* Add `position: relative;` */ 
} 

.fr:hover { 
    /* Remove `position: relative;` */ 
    /* Remove `transition: all 0.4s ease 0s;` */ 
} 

這使得過度效果淡入,進出。僞元素仍然存在問題 - 它們會跳入狀態而不是跳轉狀態。這是由於在:hover狀態定位屬性(topbottom等)的存在,但不是在靜態狀態:

.fr:before, .fr:after { 
    /* Add `top: 0px; bottom: 0px; width: 2px;` */ 
} 

.fr:hover:before, .fr:hover:after { 
    /* Remove `transition: opacity 0.2s ease-in;` */ 
    /* Redundant: `content: "";` */ 
    /* Redundant: `position: absolute;` */ 
    /* Redundant: `background-image: ...; */ 
    /* Remove `top: 0px; bottom: 0px; width: 2px;` */ 
} 

當一切都說過和做過,這裏的(大致)你'left left:https://jsfiddle.net/vn5hdn45/3/

0

嘗試把懸停之前的元素上的轉換,因此轉換回其原始狀態。

.fr{ 
    -webkit-transition: all 0.4s ease 0s; 
    -moz-transition: all 0.4s ease 0s; 
    -ms-transition: all 0.4s ease 0s; 
    -o-transition: all 0.4s ease 0s; 
    transition: all 0.4s ease 0s; 
}