CSS Transition not firing with Opacity + Display - html

I'm trying to fade a Modal in when it's clicked, and have the experience be smooth on mobile devices.
I'm setting both opacity to 0 and display to none. Setting opacity alone isn't enough, as it makes the area underneath unclickable.
#Modal {
display: none;
opacity: 0;
transition: opacity 500ms ease 0s;
}
Fade in Code:
$('#Modal').show();
$('#Modal').css('opacity','100');
However, the Modal doesn't fade in, it simply pops into existence.
Setting a setTimeout here works, but who wants a click delay for the fade in?
What's the best way to fade an element in with an opacity transition without chaining together massive properties like z-index, or some such nonsense?

Toogling display property it's bad way for fade element, Similar topics were already processed e.g: CSS3 transition doesn't work with display property
"display:none; removes a block from the page as if it were never there. A block cannot be partially displayed; it’s either there or it’s not. The same is true for visibility; you can’t expect a block to be half hidden which, by definition, would be visible! Fortunately, you can use opacity for fading effects instead."
quotation author:
Hashem Qolami
You should try to do this by deelay like here Animating from “display: block” to “display: none”
or try toogling class like here: http://jsfiddle.net/eJsZx/19/
CSS:
.Modal {
display: block;
opacity: 0;
transition: all 300ms ease 0s;
height: 0px;
overflow: hidden;
}
.ModalVisible {
display: block;
opacity: 1;
height: 50px;
}
Jquery:
$('button').on('click', function () {
$('#ModalId').addClass('ModalVisible');
});
Html:
<div id='ModalId' class="Modal" > content <br> content </div>
<button>show</button>

Why don't you use jQuery's $("selector").fadeIn() method?

The supposedly correct answer above implies that the OP is attempting a transition on display. They are not. Calling show() will set the display property to block. Then setting the opacity should theoretically trigger the transition from opacity:0.
A similar question has been answered here. To quote #WhoTheHellIsThat, the reason the transition is not triggered is...
...because of the way styles are figured out. Style changes are
expensive so they are effectively saved up until they are needed (a
recalc check like .offsetHeight is called or the next frame needs to
be drawn).
However the answer code in that question was Vanilla Javascript, and I couldn't make it work in jQuery. I found another answer that solved it in jQuery, using a class to trigger the transition.
Here is the full CSS...
#Modal {
display: none;
opacity: 0;
transition: opacity 500ms ease 0s;
}
#Modal.fade-in {
opacity: 1;
}
And here is the full JS:
$('#Modal').show(0, function() {
$(this).addClass('fade-in');
});
Here is a fiddle from RoryMcRossan's answer, demonstrating the solution.

Related

Is there a way to transition a div to be visible by making it visible from top and then going down?

Is there a way to transition a div to be visible by making it visible from the top and then going down?
Example:
#div {
visibility: hidden;
transition: visibility 1s (??);
}
#outerdiv:hover #div{
visibility: visible;
}
Yes you can, but you can´t do fades in and out with the display option, because it doesn´t have a intermediate state. If you want to acheve a opacity fade, you must use the css opacity option, and a trigger for the div to call this changes. The code will look like the following:
CSS:
div{
opacity: 0;
transition: 1s;
}
/*in this case I will use hover as the trigger*/
div:hover{
opacity:1;
}
On the other hand, if you want the div to do the fade in at the moment that the web loads, you will need to create a function in js, that will be called at the moment that the page loads, using onload="function()".

Transition an element after reintroducing it into the flow

I know that is not possible to transition properties like display, position, visibility but how can one add a transition effect (over opacity in my case; working either on show and hide) AND being able to put a display: none on that element?
There's no need to transition: display .., but just that after it is set to block then the transition on opacity would start.
I thought I could cheat using transition: display 0s, opacity .3s but I can't manage to make it work.
Of course I could use tricks like setting height: 0; overflow: hidden; to hide my element but in my case I also need to remove the element from the flow (the element is position: fixed) and prevent TABbing on it's descendants and focusing hidden elements.
Is it using animations the only supported alternative?
No Javascript solutions, please; I believe the presentation should be a concern of CSS.
You can probably use CSS animation #keyframes. Note, it can add appearing animation, but won't do the disappearing part, as display:none will be applied instantly.
function show() {
var el = document.getElementById("display");
el.classList.remove("hide");
el.classList.add("show");
}
.hide {
display: none;
}
.show {
display: block;
}
.animation {
animation: fadeIn 2s;
}
#keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
<p>
<input type="text" placeholder="input 1">
</p>
<p id="display" class="animation hide">
<input type="text" placeholder="input 2">
</p>
<button onclick="show()">show</button>

One way transition delay

I have a sidebar with a logo on the top. Next to the sidebar is a button, which is used to switch between the normal and compact class of the sidebar. This changes the width of the sidebar, so I added a 0.25s transition-duration, so it has a nice opening and closing animation. The buttonpress also changes the source of the logo. What I want to do is when I first click on the button (in normal view) it changes the image instantly, but when I click on it a second time (in compact view) the image has a 0.25s transition-delay so it only changes after the sidebar has finished opening. Is there a way I can achieve this?
sidebar {
background: red;
transition: background 1s;
}
sidebar.active {
background: pink;
transition: none;
}
<div>click</div>
<sidebar>
lorem ipsum
</sidebar>
Are you open to using jQuery (or vanilla Javascript, for that matter)?
(psuedo code/untested)
in css:
.sidebar-logo.delay-transition {
transition-delay: 0.25s;
}
in script:
$('.my-special-button').on('click', function() {
$('.sidebar-logo').addClass('delay-transition');
})
So, as soon as you click the button the first time it adds a class to give the sidebar a delay. It will continue to behave that way for each subsequent click.

Delay showing button on hover

I have angular code which shows different time segments for a particular day. I want a delete button to appear over a segment if the cursor hovers over it for 2 seconds. I have tried a few different things but I can't seem to get it working
.delete-button {
display: none;
transition: all 3s ease;
}
.time-segment:hover .delete-button{
display: block;
transition-delay: 3s;
}
<button class="delete-button">delete</button>
CSS 'display' property is not animatable. You need to use visibility if you want to do it purely with CSS. Many people set transitions on both opacity and visibility, with the visibility lagging ever-so-slightly behind the opacity, so it neatly fades in and out of view.
here, the delete button must be IN your time segment in order for your css to work. this can be done with pure css, but we must know at least the parent div, else there must be one delete for each segment.
if you have multiple segments and you want to delete a specific on, maybe javascript will be more friendly, with angular it can be pretty simple.
.delete-button {
opacity : 0.2;
}
.time-segment:hover .delete-button{
opacity: 1;
transition: opacity 3s cubic-bezier(1,0,1,0);
}
<div class="time-segment">
hover me
<button class="delete-button">delete</button>
</div>

The "hover" not work properly

I have a problem with div "transition"\"hover" at my project :
Comverese.
At the section "SUCCESS STORIES" . when you hover the div's , it go up this o.k ,but not stay up when you move the mouse in that div.
What is the problem?
.story1{
position: absolute;
width: 22rem;
height: 12rem;
z-index: 9;
background: white;
padding: 1rem;
-webkit-transition:transform .6s ease-out;
-moz-transition:transform .6s ease-out;
-ms-transition:transform .6s ease-out;
-o-transition:transform .6s ease-out;
transition:transform .6s ease-out;
}
.story1:hover{
-webkit-transform:translate(0px,-200px);
-moz-transform:translate(0px,-200px);
-ms-transform:translate(0px,-200px);
-o-transform:translate(0px,-200px);
transform:translate(0px,-200px);
}
The problem is that your div is not hovered anymore after it translates. The div moved up, and you're now hovering the div below (namely the .quote div) -> the story div comes back down.
One way to fix this is to use a bit of javascript instead of solely relying upon CSS. Adding a specific class on mouseenter and removing it when you're leaving the div below.
Something like the following (note that the JavaScript is a bit simple and should be improved upon to work for every story).
CSS:
.story1.translated {
-webkit-transform:translate(0px,-200px);
-moz-transform:translate(0px,-200px);
-ms-transform:translate(0px,-200px);
-o-transform:translate(0px,-200px);
transform:translate(0px,-200px);
}
JS :
var story1 = document.getElementsByClassName("story1")[0];
var blockquote = document.getElementsByClassName("quote")[0];
story1.addEventListener("mouseenter", function () {
story1.className += " translated";
}
blockquote.addEventListener("mouseleave", function () {
story1.className = story1.className.replace(" translated", "");
}
Edit
A pure CSS solution would be to move the :hover part to .story and style .story1 accordingly when it occurs. As follow:
.story:hover .story1{
-webkit-transform:translate(0px,-200px);
-moz-transform:translate(0px,-200px);
-ms-transform:translate(0px,-200px);
-o-transform:translate(0px,-200px);
transform:translate(0px,-200px);
}
This seems to work just fine when I change it locally to try it on your site.
And the same change can be applied to .float-icon:hover as well. Changing it to .story-icon:hover .float-icon works wonders