Im trying to pause the display of a child element when it's parent is hovered over.
Html:
<span>
<div>This Is The Child</div>
Some Text in the span
</span>
Css:
span {
position: relative;
}
span div {
display: none;
width: 0px;
opacity: 0;
transition: width 5s;
-webkit-transition: width 5s;
transition: opacity 5s;
-webkit-transition: opacity 5s;
}
span:hover div {
display: inline-block;
width: 150px;
opacity: 1;
}
As of right now, when the span is hovered, the div has no delay before it is shown. How would I go about fixing it so there is a pause?
Fiddle here: http://jsfiddle.net/SReject/vmvdK/
A few notes:
I originally tried to transition the display, but as Edward pointed out, that isn't possible, and have sense tried the above which, also, isn't working
SOLVED
It would appear that any "display" property in the "transition to" styling will stop any transition animations from happening. To work around this. I set the width of the child to be displayed to 0px and have it be completely transparent. Then in the "transition to" styling, I set the correct width, and make the div solid:
Html:
<span>
<div>This Is The Child</div>
Some Text in the span
</span>
Css:
span {
position: relative;
}
span div {
position: absolute;
width: 0px;
opacity: 0;
transition: opacity 5s;
-webkit-transition: opacity 5s;
}
span:hover div {
width: 150px;
opacity: 1;
}
Fiddle here: http://jsfiddle.net/SReject/vmvdK/
According to this article on CSS transitions, which is referenced by the MDN page on CSS transitions, the display property is not one that can be transitioned:
There are several properties or values you’ll want to transition, but which are both unspecced and unsupported at the time of writing:
background-image, including gradients
...
display between none and anything else
So applying the transition: display 5s; property to your div has no effect.
EDIT:
Based on your updated code, you can achieve the effect you want with opacity and width as long as you don't specify the display property. Simply remove the line
display: none;
from the span div section, and the pop-up menu will use the transitions you specified when you hover over it.
Since the transition from display:none; to display:inline-block can't be animated, this property is probably changed only at the end of the transition - so the opacity animates while the div is still invisible.
Have you tried using -webkit-transition-delay: ;? If not, this might be something you are looking for?
Did some changes in the code:
span div {
position: absolute;
left: 5px;
top: 5px;
border: 1px solid #000;
background-color: #888;
width: 0px;
opacity: 0;
cursor: pointer;
}
span:hover div {
display: inline-block;
-webkit-transition-delay: 2s;
width: 150px;
opacity: 1;
}
And here's a demo
Related
Is there a way to smooth the transition during a div background change? Ideally I'd like to do this within the css, and not use any js.
In my css I have:
.examplediv {
background: url(img_img.png);
}
.examplediv:hover{
background: url(brighter_img.png);
}
It's doing what I'd like it to do (changing pictures), but if there was a way to make the two backgrounds "dissolve" into one another, I'd start frothing at the mouth with gratitude.
Note: The effect I'm going for is essentially an opacity change, so if it's easier to code a dissolve with :opacity, I'm all ears!
Tanks!~
It's definitely possible using just CSS. See this Fiddle for an example: https://jsfiddle.net/ffqdmcws/
HTML:
<div class="crossfade">
<div class="static"></div>
<div class="hover"></div>
</div>
CSS:
.crossfade {
position: relative;
width: 300px;
height: 200px;
}
.static, .hover {
position: absolute;
width: 100%;
height: 100%;
transition: opacity 1s ease;
}
.static {
background: url('http://lorempixel.com/300/200/food');
opacity: 1;
}
.hover {
background: url('http://lorempixel.com/300/200/cats');
opacity: 0;
}
.crossfade:hover > .static {
opacity: 0;
}
.crossfade:hover > .hover {
opacity: 1;
}
In this case I've got a container div using the crossfade class, and a couple of other divs inside that, using classes static and hover.
The static class contains the background to be shown initially, and the hover class contains the background to fade to on hover. The initial opacities are 1 for the static class and 0 for the hover class, so you only see the div with class static.
Then, if you hook up the hover action on the container div using .crossfade:hover, in order to set opacity: 0; for static and opacity: 1; for hover, that hides static and shows hover, when you hover over the container div.
Finally, to make the backgrounds overlap use absolute positioning of the two internal divs, so they're on top of each other at all times. Additionally, for the true crossfade effect you need the transition: opacity 1s ease; rule, which says you want the opacity to transition over a period of 1 second instead of switching instantly. Both the divs changing opacity from 1->0 and from 0->1 give you the crossfade effect of the background images.
You can do it with pseudo elements which are absolutely positioned. One is visible by default and another one on hover.
.examplediv {
height: 600px;
position: relative;
}
.examplediv:before, .examplediv:after {
content: '';
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
z-index: -1;
transition: opacity 0.5s ease-out;
}
.examplediv:before {
background: url(https://pixabay.com/static/uploads/photo/2015/07/19/17/38/flower-851725_960_720.jpg)
}
.examplediv:after {
background: url(https://pixabay.com/static/uploads/photo/2015/12/07/23/56/pink-flower-1081646_960_720.jpg);
opacity: 0;
}
.examplediv:hover:before {
opacity: 0;
}
.examplediv:hover:after {
opacity: 1;
}
<div class="examplediv">
</div>
JSFIDDLE
I want whenever I scale - adjacent elements to move accordingly. How can I do that? Whenever I do scale it goes on top of adjacent element.
jsbin
In opposite to that if I change width value it works as I wanted, yet I can't use width in transitions.
HTML:
<input>
<div class="foo">
CSS:
input{
display: inline-block;
transition: all 1s ease;
transform-origin:left;
}
input:focus{
transform: scaleX(2)
}
.foo{
display: inline-block;
width: 200px;
height: 20px;
background-color: red;
}
Remember to set both an initial and a destination value for your transitions, like so:
input{
display: inline-block;
vertical-align: top;
transition: width 1s ease;
transform-origin:left;
width: 100px;
}
input:focus{
width: 200px
}
JSBin illustrating this here
html:
<div id='test'><span></span></div>
CSS:
#test:hover span:before{content:'I want this to make the div expand in ease'}
#test, #test span:before, #test span{-webkit-transition: all 0.2s ease-in;-moz-transition: all 0.2s ease-in;-o-transition: all 0.2s ease-in;-ms-transition: all 0.2s ease-in;transition: all 0.2s ease-in}
I'm wondering if this can be achieved my CSS only: when mouseover #test, some text should be added into span, and the outer div should be expanded smoothly in ease.
The above HTML+CSS doesn't work, the DIV would expand immediately.
Here's one way to do it: http://jsfiddle.net/m49tdabh/. Note: I would recommend adding content in between span tags instead of using pseudo-elements.
HTML:
<div id='test'>
<span></span>
</div>
CSS:
#test {
outline: 1px dotted gray;
}
#test span {
display: table;
}
#test span:before {
content: "Hidden message displayed on hover";
display: inline-block;
width: 0%;
overflow: hidden;
white-space: nowrap;
border: 1px solid transparent;
transition: width 0.3s linear;
}
#test:hover span:before {
width: 100%;
border-color: red;
}
The reason your transitions aren't working is because there is nothing to transition in this scenario.
Computed values don't work for CSS transitions, as a transition needs both an initial and a destination state. So something like
#test span:before {
content: '';
max-height: 0;
max-width: 0;
overflow: hidden;
display: inline-block; }
#test:hover span:before {
content: 'I want this to make the div expand in ease';
max-height: 500px;
max-width: 500px;
transition: all 0.2s ease-in;
}
will work as the max height provides both an initial and destination value. That is what becomes animated in this scenario, not the simple adding of content as the CSS is agnostic of what content in present in the element – despite what you might think from seeing the content attribute in use for pseudo elements.
How I can change the background-color on a:hover using a transition from the bottom to the top with a duration of 0.3s?
<ul>
<li><a></a></li>
<li><a></a></li>
<li><a></a></li>
</ul>
Is that possible?
Thanks
There is no way to (generically) apply a transition direction in CSS. However, we can work around that limitation by using a pseudo element (or other method like how this example uses gradients).
By using a pseudo element, which initially has a visible height of 0, we can transition the height from and to a desired direction when the link is hovered. It's best to use transform: scale for performance reasons, which means that we need to set our transform-origin to bottom center in this case to make sure it goes from bottom to top.
This approach is probably the most general, working with non-solid backgrounds and such, but does require a pseudo element or child element.
The CSS for that approach would be:
li {
background: red;
}
a {
position: relative;
z-index: 1;
}
a::after {
content: "";
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
transform: scaleY(0);
transform-origin: bottom center;
background: blue;
z-index: -1;
transition: transform 0.3s;
}
a:hover::after {
transform: scaleY(1);
}
Demo
At the moment i am working on a header with a slider animation (css3 only):
http://jimmytenbrink.nl/slider/
Everything is working fine except sometimes the slider is bugging if you go from the center to the right. It seems that i need to stop the animation for a few miliseconds to complete. However i searched everywhere on the internet but i cant seem to get it to work.
Anyone here has experience with it who can help me out?
HTML
<header>
<div><span>slide 1</span></div>
<div><span>slide 2</span></div>
<div><span>slide 3</span></div>
<div><span>slide 4</span></div>
<div><span>slide 5</span></div>
<div><span>slide 6</span></div>
<div><span>slide 7</span></div>
<div><span>slide 8</span></div>
</header>
CSS
header {
margin-top: 10px;
width: 800px;
overflow: hidden;
height: 500px;
}
header div {
background-color: #000;
width: 43.8px;
height: 200px;
position: relative;
float: left;
-webkit-transition: width .3s;
transition: width .3s;
overflow: hidden;
-webkit-animation-timing-function: linear;
-webkit-animation-iteration-count: 1;
-webkit-animation-fill-mode: forwards;
margin-right: 2px;
}
header div:first-child {
margin-left: 0px;
}
header div:last-child {
margin-right: 0px;
}
header div:hover span {
left: 50px;
opacity: 1;
}
header div img {
position: relative;
left: -240px;
-webkit-transition: all .3s;
transition: all .3s;
-webkit-filter: grayscale(1);
overflow:hidden;
}
header div span {
-webkit-transition: left .3s;
transition: left .3s;
position: absolute;
bottom: 30px;
color: white;
left: -350px;
opacity: 0;
width: 450px;
font-family:'Fugaz One', cursive;
text-transform: uppercase;
font-size: 24px;
color: #fff;
text-shadow: 0px 0px 10px #f1f1f1;
filter: dropshadow(color=#f1f1f1, offx=0, offy=0);
}
header:hover > div {
width: 43.8px;
}
header:hover > div:hover {
width: 150px;
}
Here is a JSFiddle
So the question is, how can i set a stop on the animation for a few miliseconds so the animation can finish before it gets triggered again?
Hope my question is clear!
(thanks for the edit)
One might call my answer a workaround. Maybe it is but according to my comment on ExtPro's answer - it is still completely pure CSS.
I decided to use display: table-cell since the table cell's width is distributed equally.
So, the CSS might look like this:
HINT: This is only a bunch of necessary CSS. All the code is in the jsFiddle
header {
width: 368px;
display: table;
overflow: hidden;
}
header > div {
width: 44px;
height: 200px;
position: relative;
-webkit-transition: width .3s;
transition: width .3s;
display: table-cell;
overflow: hidden;
}
header > div:hover {
width: 151px;
}
Fiddle
As you can see, we don't have to determine the width of all not-hovered divs. Actually, the problem came from that very CSS rule:
/* DON'T USE THIS RULE - IT'S THE RULE WHICH WAS BAD */
header:hover > div {
width: 43.8px;
}
You were changing the width of the divs on header:hover, so when the transition didn't manage to do its job in time, you came out with mouse pointing to the header but to non of the divs.
If I understand what you mean by 'bugging', what is happening is if you move the mouse quickly to the right, it traverses the currently open div and is left in an area which when that div collapses, does not contain (e.g. the mouse is not hovered over) the next one in order to expand it- namely the hover event of the following div(s) is/are not firing thus they do not expand. There wont be a CSS fix for this Im afraid as its browser related, you may want to replace with jQuery/JS.