Image change opacity on hover without jQuery - css

I am trying to make a list of social buttons. When one button is hovered it lights up, and the other buttons should darken.
I got it to work partially. When the first button is hovered all works fine, but it is malfunctioning when the other buttons are hovered. I think this happens because all the latter list items are siblings of the first and the :hover event can't cause previous elements to change style.
Does anyone have any thoughts on how to get this to work?
Here's the code (jsFiddle live demo):
<div id="bg">
<ul id="social_buttons">
<li id="item_1"><img src="http://i43.tinypic.com/104rasi.png"></li>
<li id="item_2"><img src="http://i43.tinypic.com/k4tide.png"></li>
<li id="item_3"><img src="http://i44.tinypic.com/2ry0gt3.png"></li>
</ul>
</div>
#bg {
height: 100px;
width: 215px;
background-color: black;
position: relative;
}
#social_buttons img {
width: 24px;
height: 24px;
-webkit-transition:all 0.2s ease-in-out;
-moz-transition:all 0.2s ease-in-out;
-o-transition:all 0.2s ease-in-out;
transition:all 0.2s ease-in-out;
}
#social_buttons {
list-style-type: none;
position: absolute;
top: 20px;
right: 70px;
-webkit-transition:all 0.2s ease-in-out;
-moz-transition:all 0.2s ease-in-out;
-o-transition:all 0.2s ease-in-out;
transition:all 0.2s ease-in-out;
}
#social_buttons li {
float: left;
margin-right: 2px;
-webkit-transition:all 0.2s ease-in-out;
-moz-transition:all 0.2s ease-in-out;
-o-transition:all 0.2s ease-in-out;
transition:all 0.2s ease-in-out;
}
/* puur css3 multiple hover animaties */
#item_1 {
opacity: 0.8;
}
#item_1:hover ~ #item_2, :hover ~ #item_3 {
opacity: 0.5;
}
#item_1:hover {
opacity: 0.9;
}
#item_2 {
opacity: 0.8;
}
#item_2:hover ~ #item_1, :hover ~ #item_3 {
opacity: 0.5;
}
#item_2:hover {
opacity: 0.9;
}
#item_3 {
opacity: 0.8;
}
#item_3:hover ~ #item_1, :hover ~ #item_2 {
opacity: 0.5;
}
#item_3:hover {
opacity: 0.9;
}

Something like this?
#social_buttons:hover li {
opacity: 0.5;
}
#social_buttons li:hover {
opacity: 0.9;
}
General sibling selector (~) can't select preceding element, it works only in one direction.

This is actually pretty easy with the right selectors
jsFiddle
ul li {
opacity: 0.8;
}
ul:hover li {
opacity: 0.5;
}
ul li:hover {
opacity: 1.0;
}

http://jsfiddle.net/VnMFP/4/
#social_buttons li {
opacity: 0.8;
}
#social_buttons:hover li {
opacity: 0.5;
}
#social_buttons li:hover {
opacity: 0.9
}
Don't select the li:hover, select ul#social_buttons:hover li. Then it works.

Related

CSS3 transition delay, different timing for in and out

I'm trying to build a menu open and close transition in Vue, but added a class on a button click.
See:
button {
position: absolute;
top: 50px;
right: 0;
}
.logo {
position: absolute;
top: 0;
left: 0;
transform-origin: top left;
transition: transform 1s;
transition-delay: 0s;
}
.menu {
position: absolute;
top: 0;
left: 150px;
transform: translateY(-100%);
transition: transform 1s;
transition-delay: 1s;
}
li {
opacity: 0;
transition: opacity 1s;
transition-delay: 0.8s;
}
li.active {
opacity: 1;
}
/* Opened menu */
.menu-opened .logo {
transform: scale(2);
transition-delay: 1s;
}
.menu-opened .menu {
transform: translateX(0);
transition-delay: 0s;
}
.menu-opened li {
opacity: 1;
}
https://codepen.io/drewbaker/pen/zYGEJQJ
Opening menu: Logo scales up, then 1 second later, the menu slides down, then items fade in.
Closing menu: Items fade out, then menu slides up, then 1 second later, logo scales down.
For the life of me I can't get it to work as I'd expect. I think I don't really understand how classes effect CSS transitions.
I understand that you want to
Logo scales up
1 second later menu slides down
items fade in (may be 0.5s or other.It is depend on you)
And then
items fade out (may be 0.5s or other.It is depend on you)
menu slides up
1 second later menu scales down
If it is correct desires, you can change following css transitions.
.logo {
transition-delay: 1.5s;
}
.menu {
transition-delay: 0.5s;
}
.menu-opened .logo {
transition-delay: 0s;
}
.menu-opened .menu {
transition-delay: 1s;
}
/* Fade in and out menu items */
.menu li{
opacity: 0;
transition: opacity 1s;
transition-delay: 0s;
}
.menu-opened .menu li{
opacity: 1;
transition: opacity 1s;
transition-delay: 1.5s;
}
When you click toggle menu-opened will add immediately.So, you should remove transition-delay in .menu-opened .logo and add transition-delay in .menu-opened .menu first.You just need to change this way.
new Vue({
el: '#container',
data: {
menuOpened: false,
},
computed: {
classes() {
return [
"main",
{"menu-opened": this.menuOpened }
]
},
menuClasses() {
return [
"menu"
]
}
}
});
button {
position: absolute;
top: 50px;
right: 0;
}
.logo {
position: absolute;
top: 0;
left: 0;
transform-origin: top left;
transition: transform 1s;
transition-delay: 1.5s;
}
.menu {
position: absolute;
top: 0;
left: 150px;
transform: translateY(-100%);
transition: transform 1s;
transition-delay: 0.5s;
}
/* Opened menu */
.menu-opened .logo {
transform: scale(2);
transition-delay: 0s;
}
.menu-opened .menu {
transform: translateX(0);
transition-delay: 1s;
}
.menu li{
opacity: 0;
transition: opacity 1s;
transition-delay: 0s;
}
.menu-opened .menu li{
opacity: 1;
transition: opacity 1s;
transition-delay: 1.5s;
}
<html>
<head>
</head>
<body>
<div id="container">
<main :class="classes">
<button #click="menuOpened = !menuOpened">Toggle</button>
<h1 class="logo">Logo</h1>
<div :class="menuClasses">
<ul>
<li>Menu item here</li>
<li>Menu item here</li>
<li class="active">Menu item here</li>
<ul>
</div>
</main>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
</body>
</html>
It can be achieved using the transitions in the right place.
I have added your expectations in the following codepen.
https://codepen.io/ravinila-the-flexboxer/pen/LYVemGj
Here you go:
.logo {
transition-delay: 1.5s;
}
.menu {
transition-delay: 0.5s;
}
.menu-opened .logo {
transition-delay: 0s;
}
.menu-opened .menu {
transition-delay: 1s;
}
.menu li{
opacity: 0;
transition: opacity 1s;
transition-delay: 0s;
}
.menu-opened .menu li{
opacity: 1;
transition: opacity 1s;
transition-delay: 1.5s;
}

Better way to write this in LESS

I have this piece of code which, on hover fades out the other li in a list apart from the one in focus. I was able to convert the CSS into LESS.. but I feel the last part needs to be optimised. I've recently started adopting LESS, so any help would be greatly appreciated.
.folder-child li {
/*some code*/
opacity: 1;
}
.folder-child:hover li {
opacity: .33;
-webkit-transition: opacity 0.5s, -webkit-transform 0.5s;
transition: opacity 0.5s, transform 0.5s;
}
.folder-child li:hover {
opacity: 1;
}
While trying to convert to LESS i'm not sure how to do it in a more optimized way..
.navigation {
ul {
.folder-collection.folder {
.folder-child {
li {
/*some code*/
opacity: 1;
a {
/*some code*/
}
}
&:hover,
&:focus {
li {
opacity: .33;
-webkit-transition: opacity 0.5s, -webkit-transform 0.5s;
transition: opacity 0.5s, transform 0.5s;
}
}
}
}
}
}
.navigation {
ul {
.folder-collection.folder {
.folder-child {
li:hover,
li:focus {
opacity: 1;
-webkit-transition: opacity 0.5s, -webkit-transform 0.5s;
transition: opacity 0.5s, transform 0.5s;
}
}
}
}
}
If you want a STRICT equivalent, then you can try :
.folder-child{
li{
/*some code*/
opacity:1;
&:hover{
opacity: 1;
}
}
&:hover{
li{
opacity: .33;
-webkit-transition: opacity 0.5s, -webkit-transform 0.5s;
transition: opacity 0.5s, transform 0.5s;
}
}
}

How to animate opencart dropdown menu

So, I am working on my ecommerce with opencart and I wanted to make the dropdown menu to show with an animation, a simple one.
The problem is that I apply the transition, but it doesn't work.
The code part is this one
.navbar-nav > li > .dropdown-menu {
margin-top: 0;
border-top-left-radius: 0;
border-top-right-radius: 0;
transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-webkit-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
-ms-transition: all 0.5s ease-in-out;
}
I am usign version 2.2
As .dropdown-menu has display:none style in bootstrap, you can't animate it with pure css without overriding, you can animate it this way:
.navbar-nav > li > .dropdown-menu {
display: block; /* Override the bootstrap display: none */
height: 0;
opacity: 0;
overflow: hidden;
transition: opacity 0.3s ease;
-webkit-transition: opacity 0.3s ease;
visibility: hidden;
}
#menu .dropdown:hover .dropdown-menu {
height: auto;
opacity: 1;
visibility: visible;
}
Another example:
.navbar-nav > li > .dropdown-menu {
display: block; /* Override the bootstrap display: none */
height: 0;
opacity: 0;
overflow: hidden;
transition: all 0.5s ease;
-webkit-transition: all 0.5s ease;
visibility: hidden;
top: 200%;
}
#menu .dropdown:hover .dropdown-menu {
height: auto;
opacity: 1;
visibility: visible;
top: 100%;
}
note: don't edit bootstrap files, add above code at the end of your theme's stylesheet: stylesheet.css

Apply separate transition timing for same CSS property

I have an icon inside a div that I would like to fade in over x time, and fade out faster.
What I am trying to achieve is something like: transition: opacity 1s ease-in .1s ease-out;
So when I hover over project, bottom-icons should only ease-in the opacity to 1, but on offhover, it should return to opacity: 0 instantly.
What I have is:
.project {
position: relative;
&:hover {
.bottom-icons {
opacity: 1 !important;
}
}
.bottom-icons {
transition: opacity .5s ease-in;
//other stuff
}
I am trying to do this avoiding jQuery.
You can apply your transition on hover, like so:
.project {
position: relative;
&:hover {
.bottom-icons {
transition: opacity 1s ease-in;
opacity: 1 !important;
}
}
.bottom-icons {
transition: opacity .1s ease-out;
//other stuff
}
https://jsfiddle.net/8x184o6x/
It can be done pretty easily.
.project .bottom-icons {
transition: opacity 0.5s ease-out; /* leaving effect */
}
.project:hover .bottom-icons {
transition: opacity 1s ease-out; /* entering effect */
}
(Sorry that code is in pure CSS but I find it more universal.)
Transitions can be different for normal state and :hover state, and this is what we’re using here.
.project {
position: relative;
&:hover {
.bottom-icons {
opacity: 1 !important;
transition: opacity .5s ease-out;
}
}
.bottom-icons {
transition: opacity 2s ease-out;
//other stuff
}
Just set the icons to easy out of their normal state, and ease out of their hover state differently.
.project {
position: relative;
background: #eee;
text-align: center;
padding: 30px;}
.bottom-icons {
display: block;
background: #ddd;
padding: 5px;
opacity: 1 ;
transition: opacity .5s ease-out;
}
.project:hover .bottom-icons {
opacity: 0;
transition: opacity 2s ease-out;
}
<div class="project">
<div class="bottom-icons">
xxxxx
</div>
</div>
According to your explanation, you can use the following things:
ADD transition to the hover,
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
opacity : 1;
-moz-transition: opacity .25s ease-in-out;
}
div:hover {
opacity: .1;
-moz-transition:2s;
}
</style>
</head>
<body>
<p><b>Note:</b> This example does not work in Internet Explorer 9 and earlier versions.</p>
<div>
</div>
<p>Hover over the div element above, to see the transition effect.</p>
</body>
</html>

Opacity transition not working in both directions

I expected this to transition when entering and leaving the dimmed state but it only transitions when leaving. How can I make the transition work when entering it? I also tried ease-in and ease-out but these don't seem to make a difference.
.is-dimmed-unless-active:not(:active):not(:focus):not(:hover) {
opacity: .5;
transition: opacity .5s ease-in-out;
}
Live example of problem http://codepen.io/ryanve/pen/doKdgW
Because you need to define the transition on .card:
.card {
transition: opacity .5s ease-in-out;
}
Instead on:
.is-dimmed-unless-active:not(:active):not(:focus):not(:hover) {
transition: opacity .5s ease-in-out;
}
Change you css to this:
.is-dimmed-unless-active {
transition: opacity .5s ease-in-out;
opacity: 1;
}
.is-dimmed-unless-active:hover {
opacity: .5;
}
.card {
width: 60%;
margin: 1em auto;
padding: 1em;
color: crimson;
border: 3px dotted crimson;
}
body {
font-family: sans-serif;
background: white;
}

Resources