I am having an issue with a CSS opacity transition. It doesn't seem to work well on Chrome - most of the time.
When it does work, it is very choppy - goes to about 1/2 opacity, then full.
When it doesn't work, it goes to less than full opacity, and stops there.
What am I doing wrong? I thought it had been working well when I first implemented this (mid June, I think). Since it does transition to full opacity sometimes, I'm not entirely sure that it was ever fully working in Chrome. But both me and my client noticed it (separately) at about the same time - late June/early July.
Here is my HTML and CSS code, and a JSFiddle. (Possibly a little more than I need to replicate this error)
.fade {
opacity:0;
transition: opacity 0.5s;
-moz-transition: opacity 0.5s;
-webkit-transition: opacity 0.5s;
-o-transition: opacity 0.5s;
}
#submenu {
opacity:0;
position:absolute;
left:185px;
}
#submenu a {
width: 90px;
padding: .4em .5em;
}
.horizontal-menu2 {
opacity:inherit;
position:absolute;
left:113px;
}
.horizontal-menu1 {
opacity:inherit;
position:absolute;
left:5px;
}
.image-menu {
opacity:inherit;
position:absolute;
right:175px;
}
.person:hover #submenu {
opacity: 1;
}
<ul>
<li class="person">Person One
<ul class="fade" id="submenu">
<li class="horizontal-menu1"><a onclick="#">menu1</a></li>
<li class="horizontal-menu2">menu2</li>
<li class="image-menu"><img src="#" alt="photo of person"></li>
</ul>
</li>
</ul>
I'm aware of an old bug in Chrome v. 40, however that was back in January (I think) and I'm on v. 43 now
It appears to be a similar issue to this, however mine does appear to work sometimes, and so perhaps this persons' issue isn't fully resolved.
Also, the other questions of this type are older - some are a direct result of Chrome v. 40's (now fixed) bug.
Your rendering bug appears to be related to the use of opacity: inherit; on the children of an element with transitioning opacity. If you remove these lines, it behaves as expected.
Working Example (JSFiddle):
.fade {
opacity:0;
transition: all 0.5s;
-moz-transition: all 0.5s;
-webkit-transition: all 0.5s;
-o-transition: all 0.5s;
}
#submenu {
opacity:0;
position:absolute;
left:185px;
}
#submenu a {
width: 90px;
padding: .4em .5em;
}
.horizontal-menu2 {
position:absolute;
left:113px;
}
.horizontal-menu1 {
position:absolute;
left:5px;
}
.image-menu {
position:absolute;
right:175px;
}
.person:hover #submenu {
opacity: 1;
}
<ul class="interview-menu">
<li class="person">Person One
<ul class="fade" id="submenu">
<li class="horizontal-menu1"><a onclick="#">menu1</a>
</li>
<li class="horizontal-menu2">menu2
</li>
<li class="image-menu">
<img src="#" alt="photo of person">
</li>
</ul>
</li>
</ul>
Pure speculation, but I'm guessing it's caching the value it inherits from the parent element at some point during the transition, and not updating it during the transition.
Related
I need some insight pls.
I tried to build a sidebar menu with an animation that will show from left -100% to 100% fullscreen when state is true and close from 100% fullscreen to left -100% when the state is false.
The animation works when the state is true but the animation doesn't work when closing the sidebar menu.
Btw if there's a better approach for this pattern please let me know. Thank you in advance !
const PhoneNavigation = ({ handleClose, open }) => {
return (
<nav className={open ? "show_side_bar" : "close_side_bar"}>
<span onClick={handleClose}>X</span>
<ul>
<li className="subMenu navItem">
Courses
</li>
<li className="navItem">
Pricing
</li>
<li className="navItem">
Mission
</li>
<li className="subMenu navItem">
About us
</li>
</ul>
</nav>
);
};
export default PhoneNavigation;
// STYLING SHEET
.show_side_bar {
position: fixed;
background-color: brown;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 99;
animation: showMenu 2s;
}
.close_side_bar {
animation: showMenu 2s reverse;
}
#keyframes showMenu {
from {
left: -100%;
}
to {
left: 0%;
}
}
This will not work on closing the sidebar because animations are applied only on the entry of an element. Instead, you can use transition like this :
const PhoneNavigation = ({ handleClose, open }) => {
return (
// when open is true, there will be an additional class show_side_bar
<nav className={open ? "show_side_bar side_bar" : "side_bar"}>
<span onClick={handleClose}>X</span>
<ul>
<li className="subMenu navItem">
Courses
</li>
<li className="navItem">
Pricing
</li>
<li className="navItem">
Mission
</li>
<li className="subMenu navItem">
About us
</li>
</ul>
</nav>
);
};
export default PhoneNavigation;
And the following CSS will make it work :
.side_bar {
position: fixed;
background-color: brown;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 99;
// transition applies to every render, i.e., adding and removing any class from
// the element
transition : all 2s ease;
// place it initially at -100%
transform : translate(-100%);
}
.show_side_bar {
// when this class is active, sidebar will be visible
transform : translate(0);
}
I have the following HTML markup and CSS to build navigation menus:
.submenu {
max-height: 0;
overflow-y: hidden;
transition: max-height 0.3s ease-in;
}
.submenu.opened{
max-height: 200px;
}
<ul class="navigation navigation-main navigation-accordion">
<li>Dashboard</li>
<li>
Products
<ul class="submenu">
<li>All Products</li>
<li>Popular Products</li>
</ul>
</li>
</ul>
I toggle the class opened on submenu using JavaScript. The submenu slides up and down on click with the help of the transition. But I want the transitions as smooth as possible.
How can I improve the above CSS code to make it as smooth as possible, as it doesn't work well if the Product link is clicked in quick succession.
I guess everybody has another definition of smooth, but I would start here with changing the ease function from ease-in to ease-in-out and than just play with the duration of the transition. 0.3s is a bit fast in this situation, increase the duration until it fits for you. Here is an example with a duration of 1.5s:
document.querySelector('#prod').addEventListener('click', function() {
document.querySelector('.submenu').classList.toggle('opened');
}, false);
.submenu {
max-height: 0;
overflow-y: hidden;
transition: max-height 1.5s ease-in-out;
}
.submenu.opened{
max-height: 200px;
}
body {
font-size: 1.5em;
font-family: sans-serif;
line-height: 1.7
}
<ul class="navigation navigation-main navigation-accordion">
<li>Dashboard</li>
<li>
Products
<ul class="submenu">
<li>All Products</li>
<li>Popular Products</li>
</ul>
</li>
</ul>
I have a little experiment going on. I'm trying to create an onclick menu without javascript using :focus. The issue I'm having is with grandchild, which clicked it still closes the parent. I tried using the ~ selector to keep it open, but it isn't working and I don't understand as to why.
<nav id="main-menu">
<ul>
<li>Menu 1</li>
<li tabindex="0" class="onclick-item"><span>Menu 2</span>
<ul class="onclick-show-content">
<li>Sub-Menu 1</li>
<li tabindex="0" class="onclick-item"><span>Sub-Menu 2</span>
<ul class="onclick-show-content">
<li>Sub-Sub-Menu 1</li>
</ul>
</li>
</ul>
</li>
<li>Menu 3</li>
<li>Menu 4</li>
</ul>
</nav>
.onclick-item { outline: none; }
.onclick-item:focus {
pointer-events: none;
}
.onclick-item > .onclick-show-content {
overflow: hidden;
max-height: 0;
-webkit-transition: all .5s ease-in-out;
-moz-transition: all .5s ease-in-out;
transition: all .5s ease-in-out;
}
.onclick-item:focus > .onclick-show-content, .onclick-item:focus ~ .onclick-show-content {
max-height: 540px;
pointer-events: auto;
}
codepen: http://codepen.io/anon/pen/iuhtn
When you click on the grandchild, focus is taken off of its grandparent. That causes the :focus > rule to no longer apply to the grandparent.
The ~ selector doesn't work the way you're using it as there are no .onclick-show-content elements that are following siblings of .onclick-item elements. The structure that you have doesn't seem to be conducive for using ~ either, as it's parent-child-based, not sibling-based.
What I'm trying to create is a toggle that's workable without using JavaScript or jQuery. The main issue that I'm having is with getting the toggle text to switch from Open to Close when :target is applied on the #menu selector. The navigation is collapsed by default and is opened by #menu toggle.
HTML
<nav class="site-nav" id="menu">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</nav>
<section class="site-nav__toggle">
<a class="site-nav__toggle--open" href="#menu">Open</a>
<a class="site-nav__toggle--close" href="">Close</a>
</section>
CSS via SCSS
.site-nav {
border-bottom: 4px solid black;
float: left;
height: 0;
overflow: hidden;
width: 100%;
&:target {
height: 100%;
min-height: 170px;
transition: height .25s ease, min-height .25s ease;
}
}
.site-nav__toggle--open > .site-nav:target,
.site-nav__toggle--close > .site-nav {
display: none;
}
.site-nav__toggle--close > .site-nav:target,
.site-nav__toggle--open > .site-nav {
display: inline;
}
The main problem I have is that the text is not switching on the target states. Maybe a 2nd pair of eyes would help and spot the obvious if I've missed anything in this particular problem.
From W3C:
div > p
Selects all P elements where the parent is a DIV element
div + p
Selects all P elements that are placed immediately after DIV elements
I think your selectors should look like
.site-nav:target + .site-nav__toggle .site-nav__toggle--open,
.site-nav + .site-nav__toggle .site-nav__toggle--close {
display: none;
}
.site-nav:target + .site-nav__toggle .site-nav__toggle--close,
.site-nav + .site-nav__toggle .site-nav__toggle--open {
display: inline;
}
Check this codepen:
http://codepen.io/anon/pen/goAIu
Best,
Marek
I am creating a menu where I want the menu items to fade in when I hover on them and fade out when I hover off of them. I am using CSS 3. This code works in Chrome and in no other browsers. It seems to be the line in the CSS where there is a direct descendent selector after the hover psuedo class that is causing the problem.
HTML
<div class="mainMenu">
<ul id="nav">
<li class="dir">
Menu Item
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</li>
</ul>
</div>
CSS
li.dir > ul{
opacity: 0;
transition: opacity .3s ease-in-out;
-moz-transition: opacity .3s ease-in-out;
-webkit-transition: opacity .3s ease-in-out;
}
li.dir:hover > ul {
opacity: 1.0;
}