Expand CSS3 animation is not working - javascript

I have a very simplistic overlay navigation, see below:
<nav role="navigation" class="navigation">
<img src="img/beer-jug-logo.png" alt="brand logo">
<ul class="navigation-list">
<li>Home</li>
<li>About</li>
<li>Design</li>
<li>Interiors</li>
<li>Contact</li>
</ul>
<a class="close"></a>
</nav>
Now I have added a very simple CSS expand and shrink animation and these animations are toggled using jQuery. The animations code are below:
.navigation.shrinkMenu {
-webkit-animation-name: shrinkMenu;
-o-animation-name: shrinkMenu;
animation-name: shrinkMenu;
-webkit-animation-duration: 1s;
-o-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
-o-animation-fill-mode: both;
animation-fill-mode: both;
}
.navigation.expandMenu {
-webkit-animation-name: expandMenu;
-o-animation-name: expandMenu;
animation-name: expandMenu;
-webkit-animation-delay: 3s;
-o-animation-delay: 3s;
animation-delay: 3s;
-webkit-animation-duration: 1s;
-o-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
-o-animation-fill-mode: both;
animation-fill-mode: both;
}
#keyframes shrinkMenu {
90% {
opacity: 0;
}
99% {
left: 50%;
top: 50%;
bottom: 50%;
right: 50%;
}
100% {
opacity: 0;
visibility: hidden;
left: 50%;
top: 50%;
bottom: 50%;
right: 50%;
}
}
#keyframes expandMenu {
0% {
left: 50%;
top: 50%;
bottom: 50%;
right: 50%;
}
100% {
left: 0;
top: 0;
bottom: 0;
right: 0;
}
}
The initial styles on the menu are below:
.navigation {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
/*background: rgba(244 ,41, 65,.9);*/
background: rgba(255 ,255, 255,.9);
display: flex;
align-items:center;
justify-content:center;
flex-direction:column;
z-index: 999;
}
The jQuery code to toggle the menu visibility is below:
$('.close').on('click', function() {
$('.navigation').addClass('shrinkMenu');
});
$('.H-menu').on('click', function() {
$('.navigation').addClass('expandMenu');
});
FIDDLE HERE
Now if you see the fiddle or even run the animation in Chrome or FF you'll notice that the shrink animation works just fine, but the expand animation is abrupt, I.E. it just doesn't work. Can anybody please explain why the animation is not working?

Note: This doesn't seem to be consistent problem. The problem happens in Fiddle only when it is loaded for the first time (by giving the URL in the address bar and clicking Go). When any edit is made to the Fiddle and we just "Run" it, the error does not happen. I could not re-create the issue in Stack Snippet either.
There are no problems with your animation code or the CSS. The problem seems to be due to href attribute in the a.H-menu tag. When this attribute is specified and there is no value, it seems like the whole page is getting reloaded on the click of the menu icon and thus you don't get to see animation.
You could do one of the following things:
Set href='#' and use e.preventDefault() within the click event handler (or)
Don't even mention the href attribute in the a.H-menu tag.
Doing either of the above would mean that the page won't be reloaded and hence animation will get displayed.
You could verify what I am saying by visiting the following Fiddles (view the console on first load):
Fiddle with original code - Open the Fiddle and open the Console immediately. Error message indicating that the image (beer-jug-logo.png) could not be loaded would be displayed. Clear this error message, close the menu and click the icon to re-open it. You would notice that the error gets displayed again (indicating that it is getting re-loaded).
Fiddle with href='#' - Do same steps as above and you'd notice the animation working properly and that there is no error message in the console upon clicking the .H-menu icon.
Fiddle with no href - Same steps and you'd notice same result as the second Fiddle.

Related

Setting element to visible does not run css animation

I have the following html code
var feedback = document.getElementById('openNotification');
feedback.addEventListener('click', function (){
a = document.getElementById("notification");
a.style.visibility = "visible";
});
#notification {
position: fixed;
z-index: 101;
bottom: 0;
transform: translateY(calc(100% + 10px));
left: 10vw;
right: 10vw;
text-align: center;
height: 20vh;
overflow: hidden;
background-color: #ededed;
color: #000;
}
#keyframes slideUp {
0% { transform: translateY(calc(100% + 10px)); }
100% { transform: translateY(0); }
}
#notification {
animation: slideUp 2.5s ease forwards;
}
<button id="openNotification">
Open notification
</button>
<!--If I remove the (style="visibility: hidden;") the animation works as expected-->
<div id="notification" style="visibility: hidden;">
121
</div>
The CSS for this div contains "transform" code to make the notification slide up the screen...
When I run the following code in a setTimeout function the notification simply appears on the screen and does not slide up as it should.
a = document.getElementById("notification");
a.style.visibility = "visible";
How do I fix this?
On further testing I can see that the animation code seems to be running from the moment the code is loaded. I assume I need to somehow change this behaviour so the animation code is kicked off by the setTimout function or in this case the button click. Any examples on how to do this?
The animation takes place but as it only lasts a short time, by the time you come to push the button it has finished, and you then make the thing visible.
Instead we remove the animation from the initial state of the element and add it (by adding a class in this case) only when you click the button.
Note: if you want this to be repeatable you will have to include sensing the animationend event and removing the slide class at that point. Otherwise the system will think it's done the animation and needn't do it again.
var feedback = document.getElementById('openNotification');
feedback.addEventListener('click', function (){
a = document.getElementById("notification");
a.style.visibility = "visible";
a.classList.add('slide');
});
#notification {
position: fixed;
z-index: 101;
bottom: 0;
transform: translateY(calc(100% + 10px));
left: 10vw;
right: 10vw;
text-align: center;
height: 20vh;
overflow: hidden;
background-color: #ededed;
color: #000;
}
#keyframes slideUp {
0% { transform: translateY(calc(100% + 10px)); }
100% { transform: translateY(0); }
}
#notification.slide {
animation: slideUp 2.5s ease forwards;
}
<button id="openNotification">
Open notification
</button>
<!--If I remove the (style="visibility: hidden;") the animation works as expected-->
<div id="notification" style="visibility: hidden;">
121
</div>

CSS keyframe animation not working on floated element

Hi I am building a simple slider to present a project.
The slider is based on swipe.js.org. I am doing everything as I should, except one thing: While every slide div contains only one image, one slide contains 2 overlapping images #img7-1 & #img7-2. I am overlaying those two images to fade the opacity of the upper image.
Below is my css. The order of elements represents the structure of the elements in the DOM.
I also have a link to the presentation at the end if you just want to look at the page source.
.swipe {
overflow: hidden;
visibility: hidden;
position: relative;
}
.swipe-wrap {
overflow: hidden;
position: relative;
}
.swipe-wrap > div {
float: left;
width: 100%;
position: relative;
}
.swipe-wrap > div img {
display: block;
width: 100vw;
height: 100vh;
object-fit: contain;
}
#img7-1{
position: relative;
z-index: 1;
}
#img7-2{
position: absolute;
z-index: 2;
animation: fade 1.5 ease-in-out 1.5s alternate infinite running;
}
#keyframes fade{
from {opacity: 0%;}
to {opacity: 100%;}
}
You can view the presentation here, all the code, styles & js (except cdn library) is on that html page.
If anyone knows this, please help me - coding is not my best skill.
Thanks everyone.
Edit:
div elements affected in dom:
<div class="swipe-wrap">
<div>
<img id="img7-1" src="/images/rivian/Rivian_Storyboards-7-1.jpg">
<img id="img7-2" src="/images/rivian/Rivian_Storyboards-7-2.jpg">
</div>
</div>
I ran your code through the CSS validator and it came back saying your shorthand notation for animation was incorrect so this fixed that problem. My next question is what is #img7-2 referring to in your html? I don't see what this animation is being used on in your source code.
EDITED:
Once you add top:0 to the img7-2 you can now see the effect happening, before the image was placed outside the browser window. You can change the timing however you'd like.
#img7-2 {
position: absolute;
z-index: 2;
/*animation: overlay 6s ease-in-out infinite running;*/
animation-name: fade;
animation-duration: 1.5s;
animation-timing-function: ease-in-out;
animation-delay: 1.5s;
animation-direction: alternate;
animation-iteration-count: infinite;
animation-play-state: running;
top:0
}

Fire Javascript during CSS/JS animation (vanilla JS)

Edit: this is an example of what I'm trying to do: http://codepen.io/anon/pen/OVzOjW
(Note that the menu and nav don't perfectly align, as the nav transition is being controlled by the CSS, and the menu delay is being controlled by the JS.)
I'm trying to create a slideout menu that fires some JS during the slide animation.
On page load, nav is fixed hidden to the right of the viewport and menu is fixed to the top right of the viewport. nav is wider than menu. On menu click fires the slideout animation of nav. I want to add a namespace class to nav that changes the CSS properties of menu. I want to do this the moment the visible portion of the nav becomes equal in width to the width of the menu, at which point the menu will just become part of the nav for the rest of the slideout.
I need to do this with some combination of CSS3 and vanilla JS (jQuery is unavailable). I can do the nav animation with CSS or JS easy enough, but timing the CSS property changes on menu is what I can't figure out.
I've tried to write a loop that constantly evaluates the right property value of nav to see if it's >= the width of menu (using CSS to do the transition), but that seems to fire the entire loop right away.
I'm not picky over a CSS vs JS solution for the animation itself, but I'd prefer CSS as I feel it's easier to control the transition settings and it runs smoother.
Relevant code below. Any help would be greatly appreciated!
HTML:
<nav id="nav">
<a id="menu" href="#">Menu</a>
Foo
Foo
Foo
</nav>
CSS:
#nav {
position: fixed;
right: -100px;
top: 0;
width: 100px;
}
#nav.expanded-nav {
right: 0;
}
#nav.expanded-menu #menu {
position: absolute;
right: auto;
top: auto;
width: 100%;
}
#menu {
position: fixed;
right: 0;
top: 0;
width: 50px;
}
You can do that with CSS animation chaining or animation-delay or simple setTimeout of Vanilla JavaScript
Check out the below code for CSS way..
$("#go").click(function() {
$(".container").addClass("demo");
});
.container {position: relative;}
#nav, #menu {
height: 100px;
width: 100px;
position: absolute;
}
#nav {
top: 10px;
left:-100px;
background: #000;
}
#menu {
top: 150px;
left:200px;
background: #f00;
}
.demo #nav {
-webkit-animation: demo 1s, demo1 2s;
-webkit-animation-delay: 0s, 1s;
-webkit-animation-timing-function: ease-in-out;
}
.demo #menu {
-webkit-animation: demo1 2s;
-webkit-animation-delay: 1s;
-webkit-animation-timing-function: ease-in-out;
}
#-webkit-keyframes demo {
0% {
left: -100px;
}
100% {
left: 200px;
}
}
#-webkit-keyframes demo1 {
0% {
left: 200px;
}
100% {
left: 300px;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="go">Go</button>
<div class="container">
<div id="nav"></div>
<div id="menu"></div>
</div>
This was actually way easier than I initially thought. It can actually rather easily be solved by setting a min-width on menu and allowing it to "grow" to the full length of the parent `nav' when it slides out. Demo here: http://codepen.io/anon/pen/EjobEJ

I want to make a word disappear after it performs a CSS animation during a mouseover

I am trying to get my code to play an animation (one word falling to the bottom of the page before disappearing), when a mouse hovers over a word in a class div, and after that have it disappear for good.
The CSS 'visibility property' allows me to choose whether the word is visible or not, but when dealing with 'class:hover' like I am, the word comes back when the mouse is not hovering over the word's position. Same with 'display: none';
When JavaScript (document.getElementById("myP").style.visibility = "hidden";) is applied with the help of onmouseover, the word will disappear without playing the CSS animation. Is there a way I can have the word perform the animation and then have it disappear from the page?
I can't show you my current code, as I'm using it in a final project soon. I'll provide an outline of it though:
<style>
.word:hover{
/*This makes the words fall to the bottom of the screen.*/
-webkit-animation-name: fallDown;
-webkit-animation-duration: 6s;
animation-name: fallDown;
animation-duration: 6s;
}
#1{
position: absolute;
left: 200px;
top: 200px;
}
/* Chrome, Safari, Opera */
#-webkit-keyframes fallDown {
0% {animation-timing-function: ease-in;}
100% {top:97%; display: none;}
}
/* Standard syntax */
#keyframes fallDown {
0% {animation-timing-function: ease-in;}
100% {top:97%; display: none;}
}
</style>
</head>
<body>
<div class="word" id="1"> Falling </div>
</body>
Please let me know if you have any ideas.
You need animationend - Event reference, it is fired when a CSS animation has completed.
$('.word').hover( function(){
$(this).addClass('animated').on('animationend webkitAnimationEnd', function(){
$(this).hide();
});
});
here is the css
.animated {
-webkit-animation: fallDown 6s;
animation: fallDown 6s;
}
DEMO (Use full page mode to see it)
$('.word').hover( function(){
$(this).addClass('animated').on('animationend webkitAnimationEnd', function(){
$(this).hide();
});
});
/* Chrome, Safari, Opera */
#-webkit-keyframes fallDown {
to {top:97%; display: none;}
}
/* Standard syntax */
#keyframes fallDown {
to {top:97%; display: none;}
}
.word{
position: absolute;
left: 200px;
top: 200px;
-webkit-animation-timing-function: ease-in;
animation-timing-function: ease-in;
}
.animated {
-webkit-animation: fallDown 6s;
animation: fallDown 6s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="word"> Falling </div>

How to make a table borders animation with Javascript?

I would like to create a Javascript function that will display the borders of a table in html like in the video here.
(Since we can't post videos here, I uploaded the video on YouTube. Please watch it. It's only 5 sec.)
I mean when the body/page loads, the table borders will appear in this way.
I do not have any idea how to do this. Can anybody please give me a help coding this animation?
Interesting question. I would try to fake border using :after pseudo-element and CSS animations. Take a look at this demo.
table:after {
content: '';
z-index: -1;
position: absolute;
top: -3px;
left: -3px;
bottom: 100%;
right: 100%;
background: green;
-webkit-animation-name: border;
-webkit-animation-duration: 3s;
-webkit-animation-iteration-count: 1;
-webkit-animation-timing-function: lenear;
-webkit-animation-fill-mode: forwards;
}
#-webkit-keyframes border {
from {
top: -3px;
top: -3px;
}
to {
bottom: -4px;
right: -4px;
}
}
Demo: http://jsfiddle.net/dfsq/uSmL3/
As alternative to CSS animations you can use some element (i.e. div), position it properly under the table and animate its width and height wiht javascript.

Categories