Rafael
Rafael

Reputation: 35

Pseudo elements transition

If you hover the element, the transition works, but not when you leave the element. How can I fix it?

This is my code and my 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; 
	border-top: 2px solid rgba(150, 200, 200, 1); 
	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> 

Upvotes: 1

Views: 222

Answers (2)

Sampson
Sampson

Reputation: 268324

Numerous things needs to be done here. First, you need to apply transitions to the .block .fr element at all times (so it works on mouse-in, and mouse-out). Right now you're only applying transitions during the :hover state:

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

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

This allows the over-effect to fade in, and out. There is still an issue with the pseudo elements - they snap in and out of states, rather than transition. This is due to the presence of positioning properties (top, bottom, etc) on the :hover state, but not on the static state:

.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;` */
}

When all is said and done, here's (roughly) what you're left with: https://jsfiddle.net/vn5hdn45/3/

Upvotes: 1

Mohammad Batroukh
Mohammad Batroukh

Reputation: 13

Try putting the transition on the element before the hover so it transitions back into its original state.

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

Upvotes: 0

Related Questions