I've got two classes, fade-item and fade. fade is nested in fade-item like this:
<a class="product-item fade-item" (mousemove)="hoverOn(i)" (mouseleave)="hoverOff(i) >
<div class='fade' *ngIf='item.active' >
<button class="botonete botonete--primary botonete--hero-one">
Button Text
</button>
</div>
</a>
When I hover over fade-item with mousemove, a value is set on the item so it shows using *ngIf='item.active', but it is supposed to do an opacity transition, which is not happening.
css code below:
.fade {
opacity: 0;
transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-webkit-transition: opacity 1s ease-in-out;
}
.fade-item:hover {
.fade{
opacity: 1;
}
}
Anyone knows what I am doing wrong?
The code you wrote it's scss. If you are looking for a css answer it should be like this:
.fade {
opacity: 0;
transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-webkit-transition: opacity 1s ease-in-out;
}
.fade-item:hover .fade{
opacity: 1;
}
When an element is dynamically added to the page, its styles are computed at that moment. For your transition to work, you would need it to have been already computed despite not actually existing yet.
Minimal example of it not working:
const elem = document.getElementById('hoverme');
elem.addEventListener('pointerover',e=>{
elem.insertAdjacentHTML('beforeend','<span id="fade">Dynamically added!</span>');
});
elem.addEventListener('pointerout',e=>{
elem.children[0].remove();
});
#fade {
opacity: 0;
transition: opacity 1s;
}
#hoverme:hover #fade {
opacity: 1;
}
<div id="hoverme">Hover me!</div>
Instead, you need to give it an animation that will begin when the element is added to the document.
Example:
const elem = document.getElementById('hoverme');
elem.addEventListener('pointerover',e=>{
elem.insertAdjacentHTML('beforeend','<span id="fade">Dynamically added!</span>');
});
elem.addEventListener('pointerout',e=>{
elem.children[0].remove();
});
#fade {
animation: fade-in 1s both;
}
#keyframes fade-in {
from {opacity:0}
to {opacity:1}
}
<div id="hoverme">Hover me!</div>
Related
The base idea is, I have shop menu as list
<li class="shop-menu">shop</li>
when the shop-menu hovered (mouseover jquery function), I want to fadeIn a div I name it
shop-menu-div
I have successfully pop-up this menu without fade-in animation by having css display:none and then display:contents when hovered by addClass('active)
the thing is, I want this shop-menu-div to fadeIn when hovered and fadeout when mouseleave
have tried several things even with css keyframes but it's not working, how to achieve this?
Add event listeners for the mouseover and mouseleave events...
JQuery using css() method:
EDIT You wanted a fade, sorry use fadeIn() and fadeOut()
let $menu = $('.shop-menu');
let $show = $('#shop-menu-div');
$menu.mouseover(function() {
$show.fadeIn('slow');
})
$menu.mouseleave(function() {
$show.fadeOut('slow');
})
#shop-menu-div {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<li class="shop-menu">shop</li>
<div id="shop-menu-div">Show this div on hover</div>
JQUERY: You could also toggle between classes as well using a CSS transition for your opacity
let $menu = $('.shop-menu');
let $show = $('#shop-menu-div');
$menu.mouseover(function() {
$show.addClass('block').removeClass('none');
})
$menu.mouseleave(function() {
$show.removeClass('block').addClass('none');
})
.none {
-webkit-transition: opacity .5s ease-in-out;
-moz-transition: opacity .5s ease-in-out;
-ms-transition: opacity .5s ease-in-out;
-o-transition: opacity .5s ease-in-out;
opacity: 0;
}
.block {
-webkit-transition: opacity .5s ease-in-out;
-moz-transition: opacity .5s ease-in-out;
-ms-transition: opacity .5s ease-in-out;
-o-transition: opacity .5s ease-in-out;
opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<li class="shop-menu">shop</li>
<div id="shop-menu-div" class="none">Show this div on hover</div>
with display you can't fade it, try opacity:1 and opacity:0 with a transition and you could do it even without jQuery pure css
or for jQuery try
$("#myDiv").animate({opacity:0},speed,callback)
I am trying to create a transition on a full screen overlay that is full width and full height with nonactive styles of visibility: hidden and opacity: 0. When clicking on a hamburger icon, an .active class is added to the div and it has the following styles: visibility: visible and opacity: 1.
Here is the CSS:
.overlay {
position: fixed;
left: 0;
right: 0;
height: 100%;
width: 100%;
background: '#272727';
z-index: 100;
transition: visibility 500ms ease, opacity 500ms ease;
visibility: hidden;
opacity: 0;
&.active {
visibility: visible;
opacity: 1;
}
}
The transition is occurring as expected on Chrome and Safari but the "fade-in" part of the transition fails on Firefox. It's basically skipping from the first frame to the last frame without transitioning. Here is a link to the page if you want to see it in action: link to webpage
And a video of what is occurring if you are unable to replicate the issue on your browser screen recording:
Why is this transition not working on Firefox?
I think this is due to when the visibility is changed in the transition and seems to display inconsistently between browsers.
This demonstrates your code and for me in Firefox if you toggle the element quickly it does not transition smoothly. This is always how I've done similar transitions and only recently started noticing the problem.
var element = document.querySelector(".element")
var toggle = document.querySelector(".element-toggle")
toggle.addEventListener("click", function(event) {
element.classList.toggle("active");
});
.element{
opacity: 0;
visibility: hidden;
transition: opacity 500ms ease, visibility 500ms ease;
}
.element.active{
opacity: 1;
visibility: visible;
}
<div class="element">This is a div element</div>
<button type="button" class="element-toggle">Toggle</button>
After reviewing, this is what works for me:
var element = document.querySelector(".element")
var toggle = document.querySelector(".element-toggle")
toggle.addEventListener("click", function(event) {
element.classList.toggle("active");
});
.element{
opacity: 0;
visibility: hidden;
transition: opacity 500ms ease, visibility 0s ease 500ms;
}
.element.active{
opacity: 1;
visibility: visible;
transition: opacity 500ms ease, visibility 0s ease 0s;
}
<div class="element">This is a div element</div>
<button type="button" class="element-toggle">Toggle</button>
Seems like visibility doesn't have transition options. So the transition works incorrect.
In .bbfIaB this part
transition: visibility 500ms ease 0s, opacity 500ms ease 0s;
Change to this
transition: opacity 500ms ease 0s;
UPDATED
The best solution in this situation will be next:
Removing visibility from transition in CSS.
Removing visibility: hidden; from .bbfIaB
Add new css class, like .hidden {visibility: hidden;}
Add a JavaScript, which will add .hidden 500ms after removing .active
Class .hidden should be added to the template by default, should be removed with activation of class .active
UPDATE 2
Working example without visibility transition at all.
var element = document.querySelector(".element")
var toggle = document.querySelector(".element-toggle")
toggle.addEventListener("click", function(event) {
if (element.classList.contains("active")) {
setTimeout(function() {
element.style.visibility = '';
}, 500);
} else {
element.style.visibility = 'visible';
}
element.classList.toggle("active");
});
.element{
opacity: 0;
visibility: hidden;
transition: opacity 500ms ease;
}
.element.active{
opacity: 1;
}
<div class="element">This is a div element</div>
<button type="button" class="element-toggle">Toggle</button>
I am trying to build a class that I can easily slap onto an element that would fade the element into view when it is rendered but instantly hide it when I set display to none.
So far, the classes Ive build fade the element into view, but there is a slight delay on hiding OR the element fade-hides as well.
I have this so far using animations for the class fadeIn:
#keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.fadeIn {
animation: fadeIn 0.2s both ease-in;
}
This one fades-in but there is a delay when hiding it
Another one looks like this:
.fade-show {
opacity: 1;
transition: opacity 2s ease-in;
-moz-transition: opacity 2s ease-in;
-webkit-transition: opacity 2s ease-in;
}
This doesnt actually fade-in and delays on hide.
I would just like something to fade when rendered or display set to block but instantly hide when display set to none.
Usage for this class would be as follows:
<div class="fadeIn" >I fade in but dont fade on hide</div>
Enjoy :)
You have also this library of animations, it's very nice and simply to use!
Animate.css https://daneden.github.io/animate.css/
.fadeMe {
animation:fadeIn 1s linear;
}
#keyframes fadeIn {
0% {
opacity:0
}
100% {
opacity:1;
}
}
.fadeMe.none {
display:none
}
<div class="fadeMe">Fade in (try to add none after the class?)</div>
You don't need to use KeyFrames to achieve this.
Set the initial opacity of the element to 0.
Add a class (.show) to the element which sets the opacity to 1 and adds the transition attributes.
Note, if you were to add the transitions to the 'div' CSS selector instead of the .show class then it would fade in AND out.
Add/remove the .show class to show/hide the element.
HTML:
<div class="show" >I fade in but dont fade on hide</div>
CSS:
div {
opacity: 0;
}
.show {
opacity: 1;
transition: opacity 2s ease-in;
-moz-transition: opacity 2s ease-in;
-webkit-transition: opacity 2s ease-in;
}
I'm using an angular js button, but i cant seem to use conventional css&js methods to put animations on it..i'm trying to implement an opacity animation on the button.
can anyone please help?
HTML
<span id="sign_btn">
<md-button>Button</md-button>
</span>
CSS:
#sign_btn{
-webkit-transition: opacity 0.2s ease-in-out;
-moz-transition: opacity 0.2s ease-in-out;
-o-transition: opacity .2s ease-in-out;
-ms-transition: opacity .2s ease-in-out;
transition: opacity .2s ease-in-out;
display:none;
opacity:0;
}
JS:
$("#sign_btn").css('display', 'block');
$("#sign_btn").css('opacity', '1');
You should use animation instead of transition.
First, create a custom animation
#-webkit-keyframes opanimation {
0% {
opacity:0;
}
100% {
opacity:1;
}
}
#-moz-keyframes opanimation {
0% {
opacity:0;
}
100% {
opacity:1;
}
}
#-o-keyframes opanimation {
0% {
opacity:0;
}
100% {
opacity:1;
}
}
#keyframes opanimation {
0% {
opacity:0;
}
100% {
opacity:1;
}
}
Then apply it to you element
#sign_btn {
animation: opanimation 5s; //you can modify the seconds here
}
Check this fiddle https://jsfiddle.net/2up5y71k/
Transitions only work for changes from one visible state to another. Your button is initially display:none; so the opacity change is not considered as a change in opacity from one state to another. Remove it (use other techniques like positioning, z-index, translate etc to achieve similar effect) and the transition should work.
found out the solution..
$("#sign_btn").delay(0).animate({"opacity": "1"}, 200);
I have this code that works great. When I pass the mouse over the image it crossfades to another image.
What I want is: when I pass over the mouse the image crossfade and rest like this, I mean, make only 1 transition.
1)image 1
2)onmouseover image 2
3)onmouse out image 2
here my code:
<div id="crossfade">
<img class="bottom" src="images/site3/pasarela1.png" />
<img class="top" src="images/site3/pasarela2.png" />
</div>
here my css code:
#crossfade {
position:relative;
height:250px;
width:400px;
}
#crossfade img {
position:absolute;
left:0;
opacity: 1;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
#crossfade img.top:hover {
opacity:0;
}
From what I get is that you want the :hover state to "stick". Though it's not a pure CSS solution, a solid way to handle this is if you are using jquery's .addClass.
$('.yourimage').mouseover(function() { //hover over your image to trigger the event
$(this).addClass('yourAnimationClass'); //add the animation class to the image you hovered over
});