I am building a navigation that's going to fade in and out a dropdown sub menu. Because I want to build it in pure CSS, I am trying to use a transition. But whatever I do, it does not work, though it seems to be the correct way.
I define the hidden, normal version of the sub list to be display none and opacity 0, telling it to use transitions. And then, on hover of its parent it shall be display-block'd and the opacity should be transitioned.
Where's the problem here?
CSS:
#nav-main>ul>li>ul {
display: none;
opacity: 0;
position: absolute;
top: 2rem;
background-color: #fef1a3;
margin: 0;
padding: 0.25rem 0;
text-align: center;
list-style: none;
box-shadow: 0 0 3px rgba(0, 0, 0, 0.25);
-o-transition: all 0.5s linear;
-moz-transition: all 0.5s linear;
-khtml-transition: all 0.5s linear;
-webkit-transition: all 0.5s linear;
-ms-transition: all 0.5s linear;
transition: all 0.5s linear
}
#nav-main>ul>li:hover>ul {
display: block;
opacity: 1
}
HTML:
<nav id="nav-main">
<ul>
<li>
Link 1
<ul>
<li>
Link 1.1
</li>
</ul>
</li>
</ul>
</nav>
For your reference, here's a fiddle (don't mind the call for transitioning "all" instead of just "opacity", tried both :-) ):
https://jsfiddle.net/hzhnqx1r/
Tried it in Firefox and Safari so far. Both have the same problem.
Thanks for your help!
Transitions will not work if you toggle the display property to/from none.
(MDN: Which CSS properties are animatable)
A trick is to move it in/out of sight as well.
A simplified example (of your code)
#nav-main>ul>li>ul {
opacity: 0;
position: absolute;
top: 1rem;
left:-9999rem;
transition: opacity 0.5s linear 0s, left 0s linear 0.5s;
}
#nav-main>ul>li:hover>ul {
opacity: 1;
left:0;
transition: opacity 0.5s linear;
}
What we do here is we initially place the ul way off the screen (left:-9999rem and set it to 0 on :hover
Using a delay on the left property animation when un-hovering to allow the fadeout to be visible.
Demo at https://jsfiddle.net/hzhnqx1r/3/
Then you don't need to display: none and display: block on hover.
like this
#nav-main>ul>li>ul {
opacity: 0;
position: absolute;
top: 2rem;
background-color: #fef1a3;
margin: 0;
padding: 0.25rem 0;
text-align: center;
list-style: none;
box-shadow: 0 0 3px rgba(0, 0, 0, 0.25);
-o-transition: all 0.5s linear;
-moz-transition: all 0.5s linear;
-khtml-transition: all 0.5s linear;
-webkit-transition: all 0.5s linear;
-ms-transition: all 0.5s linear;
transition: all 0.5s linear
}
#nav-main>ul>li:hover>ul {
opacity: 1
}
For your knowledge opacity property value work with in 0-0.9
I also updated your fiddle reference for your better understanding
https://jsfiddle.net/hzhnqx1r/2/
Related
I was practicing css on an example i found. I tried to show the submenu above the nav with transition effects. I can change the position of the submenu on hover :
nav li:hover .menu-sub {
display: block;
transform: translateY(-100%);
}
I also modified the code to add a transition effect:
.menu-sub {
position: absolute;
left: 0;
background: #444;
width: 100%;
display: none;
color: #fff;
padding: 2em;
-webkit-transition: -webkit-transform 1.5s ease;
-moz-transition: -moz-transform 1.5s ease;
-o-transition: -o-transform 1.5s ease;
transition: transform 1.5s ease;
}
The position changed but I don't see any transition effect at all. What am i doing wrong ?
Please modify the transition to shown below, it was written wrong.
.menu-sub {
position: absolute;
left: 0;
background: #444;
width: 100%;
opacity:0;
overflow:hidden;
box-sizing:border-box;
height:0px;
color: #fff;
-webkit-transition: opacity 1.5s ease-out;
-moz-transition: opacity 1.5s ease-out;
-o-transition: opacity 1.5s ease-out;
transition: opacity 1.5s ease-out;
}
Transition does not work with display, instead use the below effect.
Codepen Demo
Where we can toggle the height from 0px to auto(full height) and opacity from 0(invisible) to 1(visible). You can see that we only see the animation on opacity, this will produce the best effect.
Use visibility:hidden then visible
display: none disables it in the active DOM and such elements with this CSS can't be selected for stuffs like animations.
.menu-sub {
position: absolute;
left: 0;
background: #444;
width: 100%;
visibility: hidden;
color: #fff;
padding: 2em;
transition: transform 1.5s ease;
}
nav li:hover .menu-sub {
visibility: visible;
transform: translateY(-100%);
}
I have a simple code with CSS transition and image hover effect.
Codepen Demo
background-color: rgba(0, 0, 0, 0.22);
-webkit-transition: all 0.2s ease;
-moz-transition: all 0.2s ease-in;
-o-transition: all 0.2s ease-in-out;
transition: all 0.2s ease;
visibility:hidden;
overflow:hidden;
Could you please explain to me:
Why this part causes left to right animation? (apparently "all" property)
transition: all 0.2s ease;
How can I change it to make a simple fade instead of left/right move? Do I have to use jQuery for it?
Thanks
Kris
Change your CSS to the following:
.item .overlay {
position: absolute;
left: 0%;
top:0;
width: 0%;
height: 100%;
opacity:0;
background-color: rgba(0, 0, 0, 0.22);
-webkit-transition: opacity .3s ease-in-out;
-moz-transition: opacity .3s ease-in-out;
-o-transition: opacity .3s ease-in-out;
transition: opacity .3s ease-in-out;
overflow:hidden;
}
And on hover
.item:hover .overlay {
top: 0;
left: 0;
visibility: visible;
width: 100%;
height: 100%;
opacity: 1;
}
Updated codepen demo
the left to right is because the position of
.item:hover .overlay and .item .overlay are both 'left: 0;' if you change both to 'right: 0;' you get the animation from right to left
no Jquery is required for a fade effect, you could just add two css properties (opacity) opacity 0 on the .item .overlay and opacity 1 on the .item:hover .overlay
Does anyone know how to show the Address Bar in the boxes always, not just on hover/mouse over?
*You have to put your mouse pointer over a box to see the address I am talking about.
http://search.epicmountainhomes.com/i/snyderville-basin-real-estate
Thanks!
When you hover over .IDX-resultsCell it's changing the opacity of .IDX-streetInfo.
Set the opacity to 1 to show all the time...
.IDX-grid .IDX-resultsCell .IDX-streetInfo {
opacity: 1
}
Change opacity: 0 to opacity: 1 for .IDX-grid .IDX-streetInfo in your stylesheet.
.IDX-grid .IDX-streetInfo {
background: none repeat scroll 0 0 rgba(0, 0, 0, 0.45);
color: #F2F2F2;
font-size: 11px;
font-weight: bold;
left: 6px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
padding: 5px 5px 0;
position: absolute;
right: 6px;
text-align: right;
text-shadow: 0 1px 0 rgba(0, 0, 0, 0.8), 0 0 10px rgba(0, 0, 0, 0.6);
top: 8px;
opacity: 0;
-webkit-transition: top 0.2s ease 0s, opacity 0.2s ease 0s;
-moz-transition: top 0.2s ease 0s, opacity 0.2s ease 0s;
-o-transition: top 0.2s ease 0s, opacity 0.2s ease 0s;
-ms-transition: top 0.2s ease 0s, opacity 0.2s ease 0s;
transition: top 0.2s ease 0s, opacity 0.2s ease 0s;
}
Look For this code block in your CSS stylesheet. Find Opacity which is currently set to 0. Change it to Opacity:1; Opacity:0; means transparent. Setting it to 1 will make it fully visible.
I'm using the pseudo elements ::after and ::before for an animation on my buttons. I have reproduced this problem on 3 different platforms. It's hard to see but there's extra space on the corners. Zooming in makes it a bit easier to see.
I'm using Bootstrap but I've reproduced this with and without Bootstrap.
http://codepen.io/sinrise/pen/vLaGzN
<a href="#" class="btn btn-default btn-action">Test Button</button>
Normal
Hover
Updated
Made some more testing and research and found the real issue ... remove the z-index: 1; from the a.btn-action rule
a.btn-action {
position: relative;
overflow: hidden;
box-sizing: border-box;
color: black;
transition: color 0.4s;
transition-delay: 0.3s;
}
Updated codepen
Remove the transform3d transition for hover pseudo class and add height: 100%; for ::before and ::after pseudo selector targeting the hover animation you wanted to use.
a.btn-action {
position: relative;
box-sizing: border-box;
color: black;
transition: color 0.4s;
transition-delay: 0.3s;
}
a.btn-action::before, a.btn-action::after {
position: absolute;
top: 0;
left: 0;
content: "";
width: 100%;
height: 0;
background: rgba(0, 0, 0, 0.1);
-webkit-transition: all 0.2s ease 0s;
-moz-transition: all 0.2s ease 0s;
-ms-transition: all 0.2s ease 0s;
-o-transition: all 0.2s ease 0s;
transition: all 0.2s ease 0s;
z-index: -1;
box-sizing: border-box;
}
a.btn-action:hover::before,
a.btn-action:hover::after {
height:100%;
}
Modified Demo: Codepen
I'm new up here, and I tried ti find an answer to my question on existing forums. I tried some and couldn't resolve the issue.
I want to make the hover effect to stay after mouseout on this pre-structured code.
Someone have an idea ?
Thanks much !!!
#search_block_top {
position:absolute;
right: 0;
top: 120px;
z-index: 99;
}
#search_block_top p {padding:0;}
#search_block_top #search_query_top {
border: medium none;
height: 31px;
padding: 0;
width: 1px;
background:#FFF;
box-shadow:none;
line-height: 33px\9;
-moz-transition: width 0.5s ease 0s;
-o-transition: width 0.5s ease 0s;
-ms-transition: width 0.5s ease 0s;
-webkit-transition: width 0.5s ease 0s;
}
#search_block_top .button {
background: url("img/bg-search.png") no-repeat scroll right center transparent;
border: medium none;
border-radius: 0 0 0 0;
height: 33px;
line-height: 33px;
margin: 10px -5px 0 0;
padding: 0;
text-indent: -9999px;
width: 57px;
}
#search_query_top:focus,
#searchbox:focus #search_query_top,
#searchbox:hover #search_query_top{
-moz-transition: width 0.5s ease 0s;
-o-transition: width 0.5s ease 0s;
-ms-transition: width 0.5s ease 0s;
-webkit-transition: width 0.5s ease 0s;
width: 130px;
padding-left:10px;
margin: 10px 0 0;
background:#fff;
border: 1px solid #CCC;
}
#searchbox{}
#searchbox label{
text-indent: -9999px;
}
#searchbox #search_query_block{
border: 1px solid #CCCCCC;
-webkit-border-radius:3px !important;
-moz-border-radius:3px !important;
border-radius:3px !important;
height: 18px;
margin-top:10px;
}
#searchbox #search_button{padding: 1px 4px;}
#badAdviceGuy is right on this one. There's no way in pure css to make the change stay there after you've hovered off of it.
If you do want to venture into jQuery territory however, it would look something like this:
$(document).on('mouseenter', '#hover-element', function(){
$(this).addClass('highlight');
});
Then you would just add whatever changes you want to take place to the highlight class in css and voila!
hope that helps
Well it's best practice to put the jQuery code into a separate js file, but it seems like you'd rather just get it to work so here's the quick and dirty way:
using the css you added above, your new css will look like this:
Change this:
#search_query_top:focus,
#searchbox:focus #search_query_top,
#searchbox:hover #search_query_top{
-moz-transition: width 0.5s ease 0s;
-o-transition: width 0.5s ease 0s;
-ms-transition: width 0.5s ease 0s;
-webkit-transition: width 0.5s ease 0s;
width: 130px;
padding-left:10px;
margin: 10px 0 0;
background:#fff;
border: 1px solid #CCC;
}
To be this:
#search_query_top:focus,
#searchbox:focus #search_query_top,
#searchbox.highlight #search_query_top {
-moz-transition: width 0.5s ease 0s;
-o-transition: width 0.5s ease 0s;
-ms-transition: width 0.5s ease 0s;
-webkit-transition: width 0.5s ease 0s;
width: 130px;
padding-left:10px;
margin: 10px 0 0;
background:#fff;
border: 1px solid #CCC;
}
the html you copied in won't need to be changed, don't worry about that. Then you'll want to add this code into your header before the closing </head> tag
<script type="text/javascript">
$(document).ready(function(){
$(document).on('mouseenter', '#searchbox', function(){
$(this).addClass('highlight');
});
});
</script>
The only issue with this is that you may not have already included the jQuery library. If you put this code in and it doesn't work, add the following code into the header right before the js code you just put in:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
Let me know if that helps, to answer your second question, no, css won't be able to override the styling on mouseleave.
Keep in mind also that if you're using something like wordpress, then the rules are different and none of this code will work.
good luck!