CSS transitions blocking each other - css

Following example explains my problem:
Link: http://jsfiddle.net/sTFT3/1/
HTML
<div class="parent">
<img src="http://farm8.staticflickr.com/7292/9388249554_18f230a0ce_z.jpg" class="image"/>
<img src="http://farm3.staticflickr.com/2814/9389265805_1fd4040203_z.jpg"/>
<span class="text">Lorem Ipsum</span>
</div>
CSS
.parent {
position: relative
}
.parent .text {
position: absolute;
top: 0;
left: 0;
background: #ccc;
width: 80%;
max-height: 0;
overflow: hidden;
-webkit-transition : max-height 1s linear 0s;
-moz-transition : max-height 1s linear 0s;
-ms-transition : max-height 1s linear 0s;
-o-transition : max-height 1s linear 0s;
transition : max-height 1s linear 0s;
}
.image {
position: absolute;
opacity: 0;
-webkit-transition : opacity 1s ease-in 0s;
-moz-transition : opacity 1s ease-in 0s;
-ms-transition : opacity 1s ease-in 0s;
-o-transition : opacity 1s ease-in 0s;
transition : opacity 1s ease-in 0s;
}
.parent:hover .text {
max-height: 600px;
}
.parent:hover .image {
opacity: 1;
}
I need the animations to start together, but they are waiting for each other. Couldn't find anyone else with that kind of problem.

They start at the same time, but because you have a max-height of 600px's on the top div it finishes faster. Change this to a lower value and they will animate at the same time and duration
http://jsfiddle.net/sTFT3/2/
The problem is that if you set max-height of 600 while the div is only 20 px's big, it will increase the animation drasticly because it will animate to 600px's height in 1 second instead of 20px's.

Related

Using CSS transition on ::before pseudo element

.posts .img-hover:before {
content: '';
display: block;
opacity: 0;
-webkit-transition: opacity 1.2s ease;
-moz-transition: opacity 1.2s ease;
-ms-transition: opacity 1.2s ease;
-o-transition: opacity 1.2s ease;
transition: opacity 1.2s ease-out;
}
.posts .img-hover:hover:before {
content: '';
display: block;
background: url("img/Texture1.png");
width: 320px;
/* image width */
height: 220px;
/* image height */
position: absolute;
top: 13px;
right: 2px;
opacity: 1;
}
<div class="posts">
<a href="#">
<h2 class="postname">
Travel Flashback #1 </h2>
</a>
<a class="img-hover" href="#">
<img width="960" height="720" src="http://.." class="img-hover" alt="" />
</a>
</div>
I have one problem with this code. As you see I want transition over pseudo element ::before, which has bkg img.
When I hover on, transition works smoothly, but when I leave mouse, bkg img goes away immediately without transition.
Can you please suggest something?
On the hover you probably only want the css related to the transition, not the actual styles for the pseudo element. Try this
.posts .img-hover:before {
content: '';
display: block;
background: url("img/Texture1.png");
width: 320px; /* image width */
height: 220px; /* image height */
position: absolute;
top: 13px;
right: 2px;
opacity: 0;
-webkit-transition: opacity 1.2s ease;
-moz-transition: opacity 1.2s ease;
-ms-transition: opacity 1.2s ease;
-o-transition: opacity 1.2s ease;
transition: opacity 1.2s ease-out;
}
.posts .img-hover:hover:before{
opacity: 1;
}
For others browsing through this forum, I came to this thread with exact same problem, I tried to switch transition focus from
opacity 0.35s ease-in-out
to:
all 0.35s ease-in-out
and issue was resolved.
My browser is Chromium version 80.0.3987.162, Debian Linux 10.4
My issue was actually that the transition did not work at all. The element appears and disappears instantly. For those with a similar problem and came here, I believe CSS ignores the on-hover transition for an empty element even if the content will be added on hover and the reason it doesn't transition when you hover off is because the content is removed immediately.
Instead of
elem:before{
opacity:0;
transition: opacity 1.2s ease-out;
}
elem:hover:before {
opacity:1;
content:'something';
}
move content to elem:before
elem:before{
opacity:0;
content:'something';
transition: opacity 1.2s ease-out;
}
elem:hover:before {
opacity:1;
}
If you want the content only on hover but you want to transition another property (like width) and opacity can't be used, content: ''; should work on hover but remember to keep the property even when you hover off.
To answer OP's question and why the solution by ynter works it's because the background disappears once they hover off. Keep the background in the :before element.

css - make the transition works just for one way

I have the following code in css:
.item {
opacity: 0;
transition: opacity 1s linear;
-webkit-transition: opacity 1s linear;
-moz-transition: opacity 1s linear;
}
.item.seen {
opacity: 1;
}
when I add the class seen to an .item, the opacity of the item turn from 0 to 1 with transition.
but when I remove the class seen from an .item the opacity transition (from 1 to 0) also runs.
is there any way to make the transition run when .seen is added but not when it is removed?
.item {
opacity: 0;
}
.item.seen {
opacity: 1;
-webkit-transition: opacity 1s linear;
-moz-transition: opacity 1s linear;
transition: opacity 1s linear;
}
fiddle: http://jsfiddle.net/qcxt3evn/ (with opacity set to 0.2 just for the purpose of seeing the clickable element)
Also, don't forget to place the standard property after the vendor-prefixed (the latter might be non-compliant to the specification)
In this case, the class which contains the transition never change.
If you setup your transition in the class which toggle, the transition won't be effective on the remove.
.item {
opacity: 0;
}
.item.seen {
opacity: 1;
-webkit-transition: opacity 1s linear;
-moz-transition: opacity 1s linear;
transition: opacity 1s linear;
}
DEMO
To go beyond, as also well explained in the article Ordering CSS3 Properties from Chris Coyier, you should be careful about the order of you prefixes, with the very good example attached to the article:
http://codepen.io/css-tricks/pen/pqgKH
#wrongway { background: #ccc; padding: 30px;
border-radius: 30px 10px;
-webkit-border-radius: 30px 10px;
}
#rightway { background: #ccc; padding: 30px;
-webkit-border-radius: 30px 10px;
border-radius: 30px 10px;
}

Is it possible in CSS to transition through a third color when using a hover transition?

I have an element that is red in resting state, and green when the user hovers their cursor over it. I have it set to ease the transition for 0.4s.
Instead of having the colour transition straight from red to green, I'd like it to pass through yellow at the midway point. So when the user mouses over it, it goes from red to yellow to green in one smooth transition. Is this possible?
This is my current code.
.element {
background-color: red;
-webkit-transition: all 0.4s ease;
transition: all 0.4s ease;
}
.element:hover {
background-color: green;
}
You can use the CSS #keyframes animation syntax.
#keyframes animate-color {
0% { color: red; }
50% { color: yellow; }
100% { color: green; }
}
element:hover {
animation: animate-color 0.4s forwards;
}
Change the 0.4s value to control how fast the animation runs.
Here's an example for Chrome using -webkit-animation and #-webkit-keyframes:
https://jsfiddle.net/ahm2u8z2/1/
Make sure you cover all browser possibilities as the syntax is different for Chrome, Firefox, Internet Explorer and Opera.
https://css-tricks.com/snippets/css/keyframe-animation-syntax/
Here's more information for configuring your animations in CSS3, you can control things such as animation-delay, animation-direction, and many more.
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_animations
Alteratively, if you're not up to using #keyframes (although I don't see why not), you can use pseudo elements to act as the middle color. All you need to do is control the delay of the transitions using transition-delay:
.element {
width: 100px;
height: 100px;
background-color: red;
-webkit-transition: all 0.4s ease;
transition: all 0.4s ease;
position: relative;
-webkit-transition-delay: 0.4s;
transition-delay: 0.4s;
}
.element:before {
position: absolute;
width: 100%;
height: 100%;
content: "";
background: green;
-webkit-transition: all 0.4s ease;
transition: all 0.4s ease;
opacity: 0;
-webkit-transition-delay: 0s;
transition-delay: 0s;
}
.element:hover:before {
opacity: 1;
-webkit-transition-delay: 0.4s;
transition-delay: 0.4s;
}
.element:hover {
background-color: yellow;
-webkit-transition-delay: 0s;
transition-delay: 0s;
}
<div class="element"></div>
you could use keyframes for this:
.element {
background-color: red;
height: 300px;
width: 300px;
}
.element:hover {
-webkit-animation: changeColor 0.4s forwards;
animation: changeColor 0.4s forwards;
}
#-webkit-keyframes changeColor{
0%{background: red;}
50%{background:yellow}
100%{background:green}
}
#keyframes changeColor{
0%{background: red;}
50%{background:yellow}
100%{background:green}
}
<div class="element"></div>
This works by adding the keyframe sequence when the element is hovered, and not during the actual element's creation (so the keyframes only work during the hovered stage).
The forwards declaration is used so that the animation will 'pause' on the '100%' keyframe, rather than looping back and 'finishing where it started'. I.e. the first keyframe.
Please note: Other prefixes will need to be included see here for more info.

control the order in which property transitions occur?

If a create some simple rules with a transition:
.foo {
opacity: 1;
position: absolute;
top: 0;
left: 0;
transition: opacity .3s ease;
}
.foo.is-hidden {
opacity: 0;
top: -9999;
left: -9999;
}
i am dynamically adding and removing the is-hidden class with js.
<div class="foo"> ----> <div class="foo is-hidden">
when i do this, I would like the opacity transition to happen before the absolute position flips it off the screen.
can this be done with just transition? or do i somehow leverage a keyframe animation? I have not done such a thing before?
You can use transition-delay in conjunction with transition:
.foo {
transition: opacity 1s ease, top 1s, left 1s;
transition-delay: 0s, 1s, 1s;
}
In my Fiddle, I set opacity to 0.5 so you can see the effect:
http://jsfiddle.net/5knxvkc0/

css3 transition - custom style

Transition should work only width and height only
logo-small {
top:0 ;
width:198px;
height:198px;
position:absolute;
-webkit-transition: all 2s ease-in-out 0s;
-moz-transition: all 2s ease-in-out 0s;
transition: all 2s ease-in-out 0s;
}
logo-small:hover {
top:100px ;
width:298px;
height:298px;
}
Any idea's?
You've specified the transition should affect all properties. try
#logo-small:hover {
transition-property: width, height;
top:100px ;
width:298px;
height:298px;
}
or
#logo-small {
top:0 ;
width:198px;
height:198px;
position:absolute;
-webkit-transition: height 2s ease-in-out 0s, width 2s ease-in-out 0s;
-moz-transition: height 2s ease-in-out 0s, width 2s ease-in-out 0s;
transition: height 2s ease-in-out 0s, width 2s ease-in-out 0s;
}
edit: added back old answer, fixed
I hope you are getting this bug on Moz Firefox only:
Here is the bug: https://bugzilla.mozilla.org/show_bug.cgi?id=821976
create different css class for adding effect and apply this class on hover by javascript
.newClass {
top:100px ;
width:298px;
height:298px;
}
$('.logo-small').on('hover', function(){
$(this).addClass('newClass');
});
And don't forget to remove on mouse out.

Resources