Okay, so if you could go to;
http://jsfiddle.net/aled2305/UzM7U/4/
you will see a blue circle, when you take your mouse over a red square will appear to the right. Now that all works how I want, but I would like the red box to stay when the user then takes their mouse over it.
Now if you take your mouse over where the red square shows, it will show because of
.down:hover
{
opacity:100;
}
So is there a way to get the red square to stay when a mouse is over it, but only when it is activated by hovering over the blue circle.
Thanks in advance
Aled
UPDATE
Sorry forgot to say I would like the red square to hide once the mouse has been taken off.
Thanks
My demo will fade-in the square upon hovering the circle. From there, when you hover over the square, it will stay opaque. After you move off the circle or square, the square will fade-out.
The trick to getting this to work is setting 2 different transitions for the opacity, height, and width properties of the square, one for hover ON and one for hover OFF, as well as adding a delay attribute to the transition. The reason for transitioning height and width is that it will prevent you from being able to hover over the square without first hovering over the circle.
Here are the default settings of the square: opacity: 0, height: 0, and width: 0.
For the hover ON transition, you want opacity to fade-in over 1 second, but to be able to see that, the height and width values need to be 40px prior to the fade-in transition. To make that happen, you need to set a delay of 0 seconds on the height and width transitions. This way, the square is immediately at its max dimensions, which allows the fade-in transition to be seen.
The hover OFF transition will revert back to the default settings. What you want to have happen is for the opacity to ease-out over 1 second while at the same time keeping the values of height and width at 40px. Otherwise, height and width would instantly revert back 0 and you would not be able to see the fade-out transition. To make that happen you need to set a delay of 1 second on the height and width transitions. In doing that, the opacity eases out over 1 second and because of the 1 second delay on height and width, at that point, height and width will revert back 0.
See the jsFiddle demo
HTML
<div id="gravatar">
<div id="circle"></div>
<div id="square"></div>
</div>
CSS
#gravatar
{
float: left;
}
#circle
{
background-color: blue;
float: left;
height: 40px;
width: 40px;
border-radius: 20px;
}
#square
{
background-color: red;
float: left;
margin-left: 10px;
height: 0;
width: 0;
opacity: 0;
/* hover OFF */
-webkit-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
-moz-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
-o-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
-ms-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
}
#square:hover,
#circle:hover + #square
{
height: 40px;
width: 40px;
opacity: 1;
/* hover ON */
-webkit-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
-moz-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
-o-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
-ms-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
}
EDIT
The OP left a comment stating that adding contents to the square prevents the transitions from working correctly. I corrected it by adding overflow: hidden to the square.
I also added other styles to the CSS to account for the anchors the OP added.
See the jsFiddle demo
HTML
<div id="gravatar">
<div id="circle"></div>
<div id="square">
Profile Details
Account Details
</div>
</div>
CSS
#gravatar
{
float: left;
}
#circle
{
background-color: blue;
float: left;
height: 40px;
width: 40px;
border-radius: 20px;
}
#square
{
background-color: #2D3538;
float:left;
overflow: hidden;
margin-left: 10px;
height: 0;
width: 0;
opacity: 0;
/* hover OFF */
-webkit-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
-moz-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
-o-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
-ms-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
}
#square > a
{
display: block;
font: 15px Verdana;
color: #FFF;
text-decoration: none;
height: 15px;
line-height: 15px;
margin: 10px;
}
#square > a:last-child
{
margin-top: 0;
}
#square > a:hover
{
text-decoration: underline;
}
#square:hover,
#circle:hover + #square
{
height: 60px;
width: 135px;
opacity: 1;
/* hover ON */
-webkit-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
-moz-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
-o-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
-ms-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
}
Here's a Fiddle Using JS that follows the following logic:
Red Box shows when hovering on Blue Circle
Red Box hides when mouse leaves Reds
You can get that effect by adding a little JQuery and modifying your CSS:
JQuery:
$(".gravatar").hover(
function () {
$(".down").addClass('hoverDown');
}
);
$(".down").mouseleave(
function () {
$(".down").removeClass('hoverDown');
}
);
Here's the CSS:
.gravatar {
background-color:blue;
float: left;
margin-top: 2px;
margin-right: 10px;
margin-left: 2px;
border-radius: 20px;
width: 40px;
height: 40px;
}
.down
{
float:left;
width: 40px;
height: 40px;
background-color:Red;
opacity:0;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
.hoverDown
{
opacity:1;
}
well for mouse in and mouse out action you can use the mousenter mouseleave jquery function
$(".gravatar").mouseenter(
function () {
$(".down").addClass('hoverDown');
}
);
$(".gravatar").mouseleave(
function () {
$(".down").removeClass('hoverDown');
}
);
working fiddle:
http://jsfiddle.net/UzM7U/9/
Related
I am using the following CSS to animate features of a div. .shrink gets added to .header through Java
.brand, .brand:visited, .brand:hover {
display: block;
position: relative;
height: 100px; width: 100px;
margin-top: 25px;
background: url('img/logo.png') no-repeat center center;
background-size: contain;
border: 1px solid #fff;
border-radius: 50%;
-webkit-transition: height 0.35s ease, width 0.35s ease, margin 0.35s ease, border-color 0.35s ease;
-moz-transition: height 0.35s ease, width 0.35s ease, margin 0.35s ease, border-color 0.35s ease;
-ms-transition: height 0.35s ease, width 0.35s ease, margin 0.35s ease, border-color 0.35s ease;
-o-transition: height 0.35s ease, width 0.35s ease, margin 0.35s ease, border-color 0.35s ease;
transition: height 0.35s ease, width 0.35s ease, margin 0.35s ease, border-color 0.35s ease;
}
header.shrink .brand {
margin: 0; padding: 0;
height: 80px; width: 80px;
border-color: transparent;
}
I want to put a 0.35s delay on JUST the border-color transition. Not sure the proper notation so that it wont affect all values.
ALSO, is there a way to only have the delay applied in one direction? Meaning that I would like the delay to be applied when the border shows up, but no delay when it goes transparent.
Question 1 - How to add a delay of 0.35s only to border-color property transition?
It is very simple. Just add a delay in the last part of the comma separated values that is provided to the transition property (that is, the one for border-color) alone. In the shorthand when two time values are provided, the first would be considered as the duration and the second as the delay.
transition: height 0.35s ease,
width 0.35s ease,
margin 0.35s ease,
border-color 0.35s 0.35s ease; /* notice how the delay is added here alone */
Question 2 - How to add a delay only when border shows up (on hover)?
Again very simple, add two transition settings - one for the default selector and one for the :hover selector. In the one that is within :hover selector, add the delay because it applies when the border shows up and in the transition within the default selector do not provide any delay.
.brand {
display: block;
position: relative;
margin: 0;
padding: 0;
height: 80px;
width: 80px;
background: url('http://lorempixel.com/100/100') no-repeat center center;
background-size: contain;
border: 1px solid transparent;
border-radius: 50%;
transition: height 0.35s ease, width 0.35s ease, margin 0.35s ease, border-color 0.35s ease;
}
.brand:hover {
height: 100px;
width: 100px;
margin-top: 25px;
border: 1px solid #f00;
transition: height 0.35s ease, width 0.35s ease, margin 0.35s ease, border-color 0.35s 0.35s ease;
}
<div class='brand'></div>
I have two boxes. When clicking a button, the left box is supposed to get smaller and the right box bigger. I am aiming at having a smooth transition. When the right box gets bigger, I want a margin right to be included.
I use CSS3 transition effect. How can I achieve that for the right box the width and margin right transitions happen simultaneously and correctly?
JS Fiddle:
http://jsfiddle.net/bmzw80py/4/
My code:
HTML:
<div class="container">
<div class="box-left"></div>
<div class="box-right"></div>
</div>
<button id="animate">Animate</button>
CSS:
.container {
width: 100%;
height: 200px;
padding: 40px 0 0 60px;
}
.box-left {
float: left;
width: 60%;
height: 100px;
background: blue;
}
.box-left-smaller {
-webkit-transition: width 1s ease-in-out;
-moz-transition: width 1s ease-in-out;
-o-transition: width 1s ease-in-out;
transition: width 1s ease-in-out;
width: 355px;
}
.box-right {
float: right;
width: 30%;
height: 100px;
background: orange;
}
.box-right-bigger {
-webkit-transition: width 1s ease-in-out;
-moz-transition: width 1s ease-in-out;
-o-transition: width 1s ease-in-out;
transition: width 1s ease-in-out;
-webkit-transition: margin 1s ease-in-out;
-moz-transition: margin 1s ease-in-out;
-o-transition: margin 1s ease-in-out;
transition: margin 1s ease-in-out;
width: 62%;
margin-right: 80px;
}
JS:
$('#animate').click(function() {
$('.box-left').addClass('box-left-smaller');
$('.box-right').addClass('box-right-bigger');
});
There's no need to trigger two different transitions: you might just change the width and the left margin of the left box by applying one class only e.g.
http://jsfiddle.net/4qwrLtuw/1/
CSS (all)
.container {
width: 100%;
height: 200px;
padding: 40px 0 0 0;
}
.box-right {
overflow: hidden;
height: 100px;
background: orange;
}
.box-left {
float: left;
width: 60%;
margin-right: 2%;
height: 100px;
background: blue;
}
.box-left-smaller {
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
width: 30%;
margin-right: 80px;
}
Result
You need transition margin first then width
.box-right-bigger {
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
width: 62%;
margin-right: 80px;
}
from .box-right-bigger class
Fiddle
Use 1 translation for both animations by using all instead of 2 declaration (one for width, one for margin):
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
In your case, the first transition declaration (width) is overriden by the transition for margin...
FIDDLE : http://jsfiddle.net/bmzw80py/11/
Well, you might start out transitioning one or two properties, but then decide to add some others that you want transitioned. So, if the other transition-related values are the same, then it would be much easier to just have the “all” keyword in there from the start, so you don’t have to specify each property in a comma-separated list.
$('#animate').click(function() {
$('.box-left').addClass('box-left-smaller');
$('.box-right').addClass('box-right-bigger');
});
.container {
width: 100%;
height: 200px;
padding: 40px 0 0 60px;
}
.box-left {
float: left;
width: 60%;
height: 100px;
background: blue;
}
.box-left-smaller {
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
width: 30%;
}
.box-right {
float: right;
width: 30%;
height: 100px;
background: orange;
}
.box-right-bigger {
width: 62%;
margin-right: 80px;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="box-left"></div>
<div class="box-right"></div>
</div>
<button id="animate">Animate</button>
Here,you find demo
https://jsfiddle.net/DhwaniSanghvi/mr1feb5f/
<!doctype html>
<html>
<head>
<style type="text/css">
#i {
height: 20px;
background-color: #999;
/*return animation*/
-webkit-transition: height 0.3s ease-in 0s, background-color 0.4s ease-in 0.3s;
}
#i:hover {
height: 300px;
background-color: #F00;
/*begin animation*/
-webkit-transition: height 0.3s ease-in 0s, background-color 0.4s ease-in 0.3s;
}
</style>
</head>
<body>
<div class="c" id="i" >Hover Me</div>
</body>
</html>
See above. How do I get the box 'hover me' to end its bck-color to red, on the /return animation/, using only CSS. It understandably returns to #999, but I dont want that.
Basically: Start box 20px, #999 >> expand 300px >> Contract 20px, #red
Also is there a way to pass a value to a css property when using transitions like this:
-webkit-transition: background-color:red 0.4s ease-in 0.3s
Thanks for your help. Just trying to understand the very basics.
You can do something very close (but maybe not enough) using CSS animations.
Set an animation (not transition) on the element, and pause it. Set it's fill mode to forwards. On hover set the animation to running. When the animation ends, the end state remains.
The caveat - if somebody stops hovering before the animation is done, it will remain in it's current position until hovered again.
#i {
height: 20px;
background-color: #999;
animation: animation 0.4s ease-in 0.3s;
animation-fill-mode: forwards;
animation-play-state: paused;
}
#i:hover {
animation-play-state: running;
}
#keyframes animation {
0 {
height: 20px;
background-color: #999;
}
50% {
height: 300px;
}
100% {
height: 20px;
background-color: red;
}
}
<div class="c" id="i">Hover Me</div>
I think we don't have anything like this right now, you probably use JavaScript to do this kind of things.
I had to use a combination of jquery add and remove classes. wishing it could have been pure css though.sigh
Hopefully this simple code helps someone. If anyone has something better esp with pure css... id be happy to learn. #obi Cheers and thanks to all.
<body>
<div class="c" id="i" >Hover Me</div>
<style>
.c {height: 20px; background-color: #999;}
/*----begin animation-----------------------------------*/
#i:hover {
height: 300px;
background-color: #0F0;
-webkit-transition: height 0.3s ease-in 0s, background-color 0.4s ease-in 0.3s;
-moz-transition: height 0.3s ease-in 0s, background-color 0.4s ease-in 0.3s;
-ms-transition: height 0.3s ease-in 0s, background-color 0.4s ease-in 0.3s;
-o-transition: height 0.3s ease-in 0s, background-color 0.4s ease-in 0.3s;
transition: height 0.3s ease-in 0s, background-color 0.4s ease-in 0.3s;
}
/*----return animation-----------------------------------*/
.c_returnAnime {
height: 100px;
background-color: #F00;
-webkit-transition: height 0.3s ease-in 0s, background-color 0.4s ease-in 0.3s;
-moz-transition: height 0.3s ease-in 0s, background-color 0.4s ease-in 0.3s;
-ms-transition: height 0.3s ease-in 0s, background-color 0.4s ease-in 0.3s;
-o-transition: height 0.3s ease-in 0s, background-color 0.4s ease-in 0.3s;
transition: height 0.3s ease-in 0s, background-color 0.4s ease-in 0.3s;
}
</style>
<script>
$('#i').hover(
//always add class before removing other or errors
function(){ $(this).addClass('c_returnAnime') },
function(){ $(this).removeClass('c') }
)
</script>
</body>
</html>
I've got a dropdown menu that uses lists to achieve it. The sub menu has a height of 0 and then the hight changes when the user hovers over it.
The limit of the animation is that I can't set the max-height as auto so I've set it to a value that it unlikley that the sub menu will ever reach.
Since the tranistion time is based on the max-height is is very fast so I've slowed it down to be a suitable speed but what I'd like is to have it disappear a lot faster when someone un-hovers or even have it disppear immediately. Is there a way to do this?
.menu ul ul{
float: left;
padding: 0;
position: absolute;
text-align: left;
width: 274px;
z-index: 1000;
max-height: 0;
overflow: hidden;
-webkit-transition: max-height 1s ease-in;
-moz-transition: max-height 1s ease-in;
-o-transition: max-height 1s ease-in;
-ms-transition: max-height 1s ease-in;
transition: max-height 1s ease-in;
}
.menu ul li:hover ul, .menu li.over ul {
max-height: 999px;
}
I'd like to stick to CSS but I'm willing to use JavaScript.
Try this :
For the basic class ( not the :hover ), just define the transition duration you want for when the list will disapear.
On hover, define a new transition duration ( the duration that the list will take to appear ).
Quick exemple here : http://codepen.io/AxelCardinaels/pen/wBQZbm
HTML :
<div class="text">Some texte</div>
CSS :
.text{
background:blue;
transition:all 0.3s ease-in-out;
-webkit-transition: all 0.3s ease-in;
-moz-transition: all 0.3s ease-in;
-o-transition: all 0.3s ease-in;
-ms-transition: all 0.3s ease-in;
transition: all 0.3s ease-in;
}
.text:hover{
background:red;
transition-duration:2s;
}
I know that I can't animate the height of an element from 0 to auto. And that ist fine for my case.
When you click the heading the element below should take up its space and fade in. When you click again, it should fade out and then disappear.
But: It seems that the delay on height is ignored. That means, it immediately gets height 0 and then fades out. You can see that very clearly on the red border.
Can someone explain why and what would be a good workaround?
HTML
<h1>Minion Ipsum</h1>
<p>[…]</p>
CSS
p {
visibility: hidden;
opacity: 0;
height: 0;
overflow: hidden;
border: 1px solid red;
-webkit-transition: opacity 0.55s linear, visibility 0 0.6s linear, height 0 0.7s linear;
-moz-transition: opacity 0.55s linear, visibility 0 0.6s linear, height 0 0.7s linear;
-ms-transition: opacity 0.55s linear, visibility 0 0.6s linear, height 0 0.7s linear;
-o-transition: opacity 0.55s linear, visibility 0 0.6s linear, height 0 0.7s linear;
transition: opacity 0.55s linear, visibility 0 0.6s linear, height 0 0.7s linear;
}
p.active {
visibility: visible;
opacity: 1;
height: auto;
-webkit-transition: opacity 0.55s cubic-bezier(0.405, 0.145, 0.505, 1);
-moz-transition: opacity 0.55s cubic-bezier(0.405, 0.145, 0.505, 1);
-ms-transition: opacity 0.55s cubic-bezier(0.405, 0.145, 0.505, 1);
-o-transition: opacity 0.55s cubic-bezier(0.405, 0.145, 0.505, 1);
transition: opacity 0.55s cubic-bezier(0.405, 0.145, 0.505, 1);
}
Test
http://jsfiddle.net/4wqoaek2/
That's because you can't transition with height: auto. When you change the height in your fiddle from auto to i.e. 20px you'll see the transition working correctly. (Using Chrome 36 on OS X)
A workaround would be figuring out the correct height via javascript and apply that style to p.active.