So I have a custom expandable select. I want to apply two different transitions, one for the opening and one for the closing of the select.
.select-generic-2 {
max-height: 39px;
overflow: hidden;
z-index: 2;
transition: z-index 0s, max-height .25s ease-in-out 0s;
}
.select-generic-2.expanded {
z-index: 3;
max-height:1000px;
transition: max-height .25s ease-in-out 0s, z-index 0s .25s;
}
Here's the HTML structure of my custom select
<span class="select-generic-2 shadow-input">
<input type="radio" name="status" value="3" id="3">
<label for="3">text</label>
<input type="radio" name="status" value="4" id="4">
<label for="3">text2</label>
</span>
I am toggling the .expanded class with JS, but always the first transition property is executed. I want to change the z-index in such way to avoid overlapping with other elements with the same .select-generic-2 class (only one element has the .expanded class at a time). Any suggestion will be very welcome!
EDIT: I just realized that the problem is different and not related to the transitions. Just every single select has a default z-index value of 2 and when the .expanded class is removed the select boxes located under the collapsing are appearing over it because they have the same z-index (2) and are put over the collapsing (which is located above them in the code) by the browser.
Since you want to toggle the .expnaded class, use the class alone, then use your JS to add or remove the class on 'toggle'.
.select-generic-2 {max-height: 39px; overflow: hidden; z-index: 2; transition: z-index 0s, max-height .25s ease-in-out 0s;}
.expanded {z-index: 3; max-height:1000px; transition: max-height .25s ease-in-out 0s, z-index 0s .25s;}
Then on 'toggle', get the element from DOM and use add the class '.expanded'
To add class: https://www.w3schools.com/howto/howto_js_add_class.asp
To remove class: https://www.w3schools.com/howto/howto_js_remove_class.asp
Well guys I just added a new class called .collapsing which looks like this:
.select-generic-2.collapsing {
z-index: 1;
transition: z-index 0s 0s, max-height .25s ease-in-out 0s;
}
It is called when another select is being open and is removed when the expansion of the another select is complete.
The JS looks like this:
const select = document.querySelectorAll('.select-generic-2');
//this is inside the event listener
selected = this;
select.forEach(function(el){
if (el != selected){
el.classList.remove("expanded");
el.classList.add("collapsing");
setTimeout(function(){
el.classList.remove("collapsing");
}, 250);}
});
A little bit cheesy solution, but it works!
Related
I have a div that I want to be visible as :target using css. This is working fine so far. My problem is: I want it to be faded in and out.
My code:
<div id="stuff">
Content
</div>
#stuff {
opacity:0;
transition: opacity .5s ease-in-out;
}
#stuff:target {
opacity:1;
}
So let's say on the following url it should fade in (what it does):
example.com/example.htm#stuff
But when I change the url to the following (or anything else), it just becomes invisible without a transition:
example.com/example.htm#
Note that I want to change the url by clicking on a link, not by modifing it via JavaScript.
My question is:
Is it possible to have a transition when changing the target for the previous target without the use of JavaScript? How would you accomplish it?
I do not want to use any JavaScript at all, if possible.
I just changed the value of the opacity property and it's working. As far as I know, opacity only accepts a decimal value between 0 and 1 (apart from inherit, initial and unset).
#stuff {
opacity:0;
transition: opacity .5s ease-in-out;
}
#stuff:target {
opacity:1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="stuff">
Content
</div>
Add hash
Remove hash
<div class="logoWrap">
<img src="https://i.imgur.com/OBFbjSK.png">
<p class="logoDesc">this is img desc</p>
</div>
I want to avoid using javascript, is it possible to change the value of other element using :hover? if possible I want to add transition.
http://jsfiddle.net/UH2Aq/
Do you mean that you want the hover on the logoWrap to make the logoDesc appear?
If so:
http://jsfiddle.net/wildandjam/emELN/
.logoWrap:hover .logoDesc{
opacity:1;
}
Then, if you want, you can add a CSS3 transition, to make it more fluid.
Same answer with transition : http://jsfiddle.net/UH2Aq/1/
.logoDesc{
transition: opacity .2s ease-in-out;
opacity : 0;
}
.logoWrap:hover .logoDesc {
opacity: 1;
}
I've got two divs, div 1, and underneath it is hidden div 2. When I hover over div 1, I want it to hide, and show div 2. Then, once I mouse off of the area (now div 2), div 1 is displayed again.
Here is the code:
<a href="javascript://" class="hoverable">
<div class="normal" style="background:#666;">Hover over me!</div>
<div class="hover" style="background:#888;">Now you see me!</div>
</a>
and here is the css:
<style>
.hoverable {
cursor:default;
color:#000;
text-decoration:none;
}
.hoverable .hover {
display:none;
}
.hoverable:hover .normal {
display:none;
}
.hoverable:hover .hover {
display:block;
}
</style>
My only problem with this is that is is very quick, cut and dry, and not very "fancy". I'd like to add something simple like a fade effect.
I've gotten this working, without the fade effect, here:
http://jsfiddle.net/pBDGW/
If anyone knows how to make those two divs transition with a fade-out, please let me know!
You can use CSS transition with opacity like this:
.hoverable {
cursor:default;
color:#000;
text-decoration:none;
position: relative;
display: block;
}
.hoverable .hover {
opacity:0;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.hoverable .hover,
.hoverable .normal{
transition: opacity .5s;
-o-transition: opacity .5s;
-ms-transition: opacity .5s;
-moz-transition: opacity .5s;
-webkit-transition: opacity .5s;
}
.hoverable:hover .normal {
opacity:0;
}
.hoverable:hover .hover {
opacity:1;
}
You can see the jsfiddle here: http://jsfiddle.net/pBDGW/12/
Some explanation:
The transition applied to both div are the main code that make them fade in & out. You can read more about it here: http://css3.bradshawenterprises.com/transitions/
Since you want the first div to fade out, and the second div to fade in, there will be a moment when both div have to be visible partially, hence position: absolute and some positioning on the second div (to make it overlap with the first div).
You are wrapping an anchor (<a>) around both div, which is actually not encouraged, so I have to give it display: block; . A better approach (HTML-wise) is to wrap both div inside another div (still use the same class hoverable), and use 2 different anchors inside each div.
EDIT: this approach http://jsfiddle.net/pBDGW/14/ works too. Here you only fade out the first div, while the second div is always visible but is hidden under the first div when not hovering. It is shorter css, but I don't recommend this approach though because I sometimes have issues with getting the first div to go on top on different browsers.
You can use jQuery, it has functions fadeIn and fadeOut and also its easy to hide() and show() on events mouseOver and mouseLeave.
You can see fiddle here.
$(document).ready(function(){$(".hover").hide();
$(".normal").mouseover(function(){
$(".normal").fadeOut(0);
$(".hover").fadeIn(1000);
});
$(".hover").mouseout(function(){
$(".normal").fadeIn(1000);
$(".hover").fadeOut(0);
});});
You can use transitions:
ADD THIS TO YOUR :HOVER
-o-transition:.5s;
-ms-transition:.5s;
-moz-transition:.5s;
-webkit-transition:.5s;
transition:.5s;
This adds the effect of fade IN/OUT
Addition: this will only work if you have property set for :hover, for example if you want to change the color, or background or what ever..
I'm fairly new to web design and I've been working on a CSS way of creating a drop down menu which appears initially when a parent div is hovered. However once visible, I need the menu to stay up until the mouse is moved off the whole menu.
I've been able to make the menu appear and disappear, however it disappears too quickly - disappearing when I leave the parent div. Example below.
I've tried nesting within a third div which changes between display:block and display:none but no luck
Code
<div class="menuc">
<div class="menua"></div>
<div class="menub">
<p class="footertext">Home</p>
<p class="footertext">Home</p>
<p class="footertext">Home</p>
<p class="footertext">Home</p>
</div>
</div>
Css
.menua {
width:200px;
height:50px;
background-color:rgba(0, 0, 0, 0.50);
z-index:999;
}
.menua:hover ~ div.menub {
opacity:1;
z-index:999;
}
.menub {
width:200px;
height:600px;
background-color:rgba(0, 0, 0, 0.50);
opacity:0;
-webkit-transition: opacity .5s linear;
-moz-transition: opacity .5s linear;
-ms-transitiom: opacity .5s linear;
-o-transition: opacity .5s linear;
transition: opacity .5s linear;
z-index:999;
display:block;
}
.menuc {
display:none;
}
.menuc:hover {
display:block;
}
http://jsfiddle.net/qp3wu/
Apologies if this is a fairly amateur mistake (doh).
All answers and suggestions greatly appreciated
Just let .menub element stay when .menub itself is also hovered:
.menua:hover ~ .menub, .menub:hover {
opacity:1;
}
The comma is used the define a new selector. The above is the same as:
.menua:hover ~ .menub{
opacity:1;
}
.menub:hover {
opacity:1;
}
But shorter.
Ignore any event; hide the element, but still use opacity effect
Now to make sure the .menub does not react to any events(like hovering), you can just hide it:
.menua:hover ~ .menub, .menub:hover {
visibility: visible;
opacity:1;
}
and on hover show it again:
.menub {
visibility: hidden;
}
I use css visibility to keep the same effect as you would have with only opacity. It will still have the space of the element(unlike what display: none; does; collapsing the space).
Delete remaining space when not visible
To go even further and fix the remaining spacing, you can add height: 0; as default and when hovered set the height to your desired height(600px in this case).
Remove the gab inbetween menua and menub
If you want to get rid of the gab between menua and menub but still use the default margin, you can reset the top margin of the first <p> element:
.menub p:first-child
{
margin-top: 0;
}
jsFiddle
The text beneath the menu's is to show you that there will be no space when the element is not visible.
You need to make a few changes:
One wrap the menub with menua div to keep the hover effect:
<div class="menua">
<div class="menub">
<p class="footertext">Home</p>
<p class="footertext">Home</p>
<p class="footertext">Home</p>
</div>
</div>
Two position your menub inside menua i use absolute position. With this you avoid other elements to be moved by the submenu.
.menua {
position:relative;
}
.menub {
position:absolute;
top:100%;
left:0;
}
Three you can use opacity to give an effect but to hide and show you really need display property:
.menub {
display:none;
}
.menua:hover div.menub {
display:block;
}
An example http://jsfiddle.net/qp3wu/23/
I'm trying to make a CSS menu with submenus. When I move the mouse over a child element of list, I want the submenu to appear inside that child if there is any.
As it is avaliable to see above, sub menu appears but I don't want that to happen when I move the cursor over the areas that I've marked with circles. These are padding areas.
Here's my markup:
<ul>
<li>Home</li>
<li>About Us
<ul>
<li>Our Mission</li>
<li>Our Vision</li>
</ul>
</li>
</ul>
And this is the CSS:
a { color: #000; text-decoration: none; }
a:hover { color: #A0A0A0; }
ul > li { float: left; padding: 0px 30px; list-style-type: none;
margin: 0px 5px; background-color: #CACACA; }
ul > li:hover > ul { display: inline; }
ul > li > ul { position: fixed; display: none; }
Now there is no problem with this code. As you can see in this fiddle, it works as I wanted, the submenu appears when I move the mouse over the "About Us" menu.
However, my problem here is that there are blank spaces(padding) inside menu items and the submenu appears even if I hover over these spaces. But I want the submenu to appear only when I hover the <a> element inside the <li>.
To accomplish this, I tried that code:
ul > li > a:hover > ul { display: inline; }
instead of the existing:
ul > li:hover > ul { display: inline; }
but it didn't work, I wasn't expecting since the <a> and <ul> are the children of the very same <li> element.
I'm creating a template for a CMS, so I don't want to get involved with jQuery implementations too much, like setting ids or classes for list elements. I want to make an implementation with pure CSS.
And I will be using seperating images between list elements, so I must use padding instead of margin.
If I was using margin, it would be easy since the width of the list element would be same with the <a> element's width.
But here, after all these exceptions, I need some help. Any ideas are highly appreciated.
Thanks in advance.
This is possible, but only-just, and with some fairly severe caveats; first the CSS to hide/show the sub-menu:
ul > li > ul {
position: fixed;
opacity: 0;
-moz-transition: opacity 1s linear;
-ms-transition: opacity 1s linear;
-o-transition: opacity 1s linear;
-webkit-transition: opacity 1s linear;
transition: opacity 1s linear;
}
ul > li a:hover + ul,
ul > li:hover a + ul:hover{
opacity: 1;
-moz-transition: opacity 1s linear;
-ms-transition: opacity 1s linear;
-o-transition: opacity 1s linear;
-webkit-transition: opacity 1s linear;
transition: opacity 1s linear;
}
JS Fiddle demo.
Now, the caveats:
The display property won't animate (it can't, there's no interim states between none and inline-block (or between any other valid values for the property), so the sub-menu always has to be 'visible'/'present' on the page (albeit with an opacity of 0 hiding it in most browsers).
The transitions are necessary, because that's the only way that the cursor would have time to move from the a link to the ul without it immediately hiding; the transition is, basically, a time-delay to allow the second of the selectors, ul > li:hover a + ul:hover to match.
cross-browser compatibility is likely to be a problem, since transitions aren't supported in Internet Explorer < 10, mind you nor is opacity until version 9.
The major problem, though, is selector specificity. It's going to be rather difficult to write the selectors for arbitrary-depth menus using this technique. So, while it's possible, I certainly wouldn't recommend using this approach, as it seems far too prone to failure.
Perhaps an easier way to do this would be to use the CSS peer selector +. Because the sub-menu <ul> is not a direct child of the <a> tag, you can't use >. Instead use the peer selector + to choose all subsequent peers of a particular type.
In other words: ul li a:hover + ul { display: inline; }
And it has good browser support.