How to make a table borders animation with Javascript? - 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.

Related

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
}

How to build a CSS animation that "slides in", revealing a text/div after it finishes sliding?

I am trying to mimic the CSS animations from a website here: https://stanographer.com/
I want to copy the way the site:
starts by showing a full screen black div sliding away to the right
"loads" the black background (div tags) behind text (as in "Hi, I'm Stanley Sakai"), expanding left to right and
"loads" the text over the black background div, expanding left to right.
Now you might ask, "Why not just inspect the page, look at the classes on the divs and text, then inspect the CSS sheet in the network tab?" And I've tried that. The CSS looks weird. My friend said it is pre-processed by SASS, whatever that means. Anyway, I cannot decipher the code.
I've been to a few different StackOverflow pages (here's one) & over a dozen different pages on Google. I learned about using keyframes but I haven't figured out how to recreate the effect on Stanographer.com. My friend, who owns the website, also provided this example, but I don't get how to apply it to individual divs. He said something about using the z-index but I just don't see it.
I know that to make the page start with a full black screen & then slide out, I have to trigger a class change using JavaScript. I have:
let blackStuff = document.getElementById("blackness");
window.addEventListener("load", () => {
console.log("loaded");
blackStuff.setAttribute("class", "black-box-out");
},
false
);
.black-box {
position: fixed;
float: left;
top: 0;
width: 100%;
height: 100%;
bottom: 0;
background-color: #000;
z-index: 999999;
-webkit-animation: powerslide 0.5s forwards;
-webkit-animation-delay: 2s;
animation: powerslide 0.5s forwards;
animation-delay: 2s;
}
#-webkit-keyframes powerslide {
100% {
left: 0;
}
}
#keyframes powerslide {
100% {
left: 0;
}
}
.black-box-out {
margin-left: 100%;
animation: slide 0.5s forwards;
-webkit-transition: slide 0.5s forwards;
transition: slide 0.5s forwards;
}
<div id="blackness" class="black-box"></div>
But this just makes the "blackness" div disappear instantly on page load. I want it to slide out. Clearly, I don't get how to use CSS animations.
If you are interested in seeing more of what doesn't work, read on. Otherwise, you can skip this section: it only shows my failed trials.
I've learned how to make a CSS animation expand horizontally from 0:
.wrapper {
position: relative;
overflow: hidden;
width: 500px;
height: 50px;
border: 1px solid black;
}
.slide-custom {
width: 500px;
height: 50px;
background: cyan;
position: relative;
-webkit-animation: slideIn 2s forwards;
animation: slideIn 2s forwards;
}
/* moz and webkit keyframes excluded for space */
#keyframes slideIn {
0% {
transform: scaleX(0);
}
100% {
transform: scaleX(1);
}
}
<div class="wrapper slide-custom">
<h1 class="slide-custom">
<span>MEET ROLY POLY.</span>
<!-- expands horizontally from 0 width to 100% width -->
</h1>
</div>
And I've learned to make text "slide in" from the left, though it starts at 100% width when I want it to start at 0% width:
/* CSS */
.test-slide {
animation-duration: 3s;
animation-name: testSlide;
}
#keyframes testSlide {
from {
margin-left: 0%;
width: 50%;
}
to {
margin-left: 100%;
width: 100%;
}
}
<div class="test-slide">
<h1><span>ABOUT.</span></h1>
<!-- will slide in from the left -->
</div>
There's more -- unfortunately none of it mimics the website I'm trying to copy.
Explanation
There are multiple ways to achieve what you want actually. I did not opt to animate width. The first few frames of the animation will be not as expected.
So instead, we can use clip-path. What clip-path basically does is masking. You can "crop" a div such that only a part of it is visible. We will utilise clip-path and ::before or ::after pseudo-element (either is fine) to create this animation. What we need to do:
Create the pseudo-element and position it such that it covers (is on top) the whole animatable element (position: absolute)
Set the pseudo-element's background to black
Using clip-path, mask the animatable element to display no parts of the element (this will also cause the pseudo-element to not be displayed as it is part of the element). The direction of the clipping is important. The direction here is from the right side to the left side.
Using animation and #keyframes, unmask the previously masked div. This will reveal it slowly from the left side to the right side (because initially, we masked it from the right to left; upon unmasking, the reverse direction happens)
Upon unmasking the element, the pseudo-element will be on top of the text we want to display
After a short while later, mask the pseudo-element (not the whole element) from the right direction to the left direction, again using clip-path so that the text seems revealed slowly
It works! However, I recommend reading about clip-path. Also, one really handy clip-path CSS generator I really like to use is this (if you want to clip from the right to left, you should drag the points from the right to left). I also highly recommend reading about CSS positioning (a staple in good CSS animations). You needn't be using z-index: 9999; you generally want to keep track of the z-index you use.
Solution
Here's a working solution using the described method. Try running it.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: Helvetica;
}
body,
html {
width: 100%;
height: 100%;
}
#wrapper {
background: #555555;
width: 100%;
height: 100%;
color: white;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
#wrapper * {
margin: 5px;
}
.heading {
font-size: 3em;
padding: 10px 5px;
}
.caption {
font-size: 1em;
padding: 5px;
font-family: Courier;
}
.animatable {
position: relative;
clip-path: polygon(0 0, 0 0, 0 100%, 0% 100%);
animation: .75s cubic-bezier(1,-0.01,.12,.8) 1s 1 reveal forwards;
}
.animatable::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #20262b;
padding: inherit;
animation: .75s cubic-bezier(1,-0.01,.12,.8) 1.75s 1 hideBlack forwards;
}
#keyframes reveal {
from { clip-path: polygon(0 0, 0 0, 0 100%, 0% 100%); }
to { clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%); }
}
#keyframes hideBlack {
from { clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%); }
to { clip-path: polygon(100% 0, 100% 0, 100% 100%, 100% 100%); }
}
<div id="wrapper">
<div class="heading animatable">Hi, I am Richard!</div>
<div class="caption animatable">I am a person.</div>
</div>
Although the simple animation you wanted can be created using merely CSS, I still suggest you read about how to make animations using JavaScript and the various libraries it has in making animations. This is because once there are many animations and transitions going on, it becomes hard to keep track of animations (especially when you want animations to start after another animation ends). A good library is anime.js (do explore more options before settling on one). Furthermore, notice how the animations only appear upon scrolling down in the website you provided? That's doable only with JS (one such method is using IntersectionObserver API provided by most browsers).
Here you have some CSS3 animations, you trigger that animation when the .entrance-animation gets the .active class.
You'll need an observer to watch when the item gets into view and, when the item is visible, you add the .active class to it.
Hope it helps!
setTimeout(() =>
{
let animate = document.querySelectorAll('.entrance-animation');
animate.forEach(item => item.classList.add('active'));
}
,1000);
.entrance-animation
{
position: relative;
color: blueviolet;
white-space: nowrap;
font-size: 24px;
width: 0;
overflow: hidden;
transition: width 0.5s ease;
}
.entrance-animation::before
{
content: '';
position: absolute;
top: 0;
right: 0;
height: 100%;
width: 100%;
background-color: black;
z-index: 10;
transition: width 0.5s ease;
transition-delay: 0.5s;
}
.entrance-animation.active
{
width: 100%;
}
.entrance-animation.active::before
{
width: 0%;
}
<p class="entrance-animation">
Hello
</p>
<p class = "entrance-animation">
Here we are
</p>
You can use CSS3 transitions or maybe CSS3 animations to slide in an element.
For browser support: http://caniuse.com/
I made two quick examples just to show you how I mean.
CSS transition (on hover)
Demo One
Relevant Code
.wrapper:hover #slide {
transition: 1s;
left: 0;
}
In this case, Im just transitioning the position from left: -100px; to 0; with a 1s. duration. It's also possible to move the element using transform: translate();
CSS animation
Demo Two
#slide {
position: absolute;
left: -100px;
width: 100px;
height: 100px;
background: blue;
-webkit-animation: slide 0.5s forwards;
-webkit-animation-delay: 2s;
animation: slide 0.5s forwards;
animation-delay: 2s;
}
#-webkit-keyframes slide {
100% { left: 0; }
}
#keyframes slide {
100% { left: 0; }
}
Same principle as above (Demo One), but the animation starts automatically after 2s, and in this case I've set animation-fill-mode to forwards, which will persist the end state, keeping the div visible when the animation ends.
Like I said, two quick example to show you how it could be done.
EDIT: For details regarding CSS Animations and Transitions see:
Animations
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_animations
Transitions
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_transitions
Hope this helped.

How to fade in an effect on my CSS element?

here's my CSS. First question, I want the effects that occur when the div is hovered above to fade in and out, rather than it being instant. I tried to use transitions, but it seems to mess with the design of the element, so I'm stuck.
My 2nd question is (here's the jfiddle for reference: https://jsfiddle.net/ojvym9cm/2/), is it possible to only allow the hover effect to occur after the 'do' keyanimation has occured? I don't want the hover effect to take place while the div is animating.
My 3rd question is, why is the animation happening immediately instead of only after I've clicked the 'half' div? (jfiddle for reference). Sorry, these are lots of questions I've just spent so many hours trying to figure all this out and came up short.
#middle {
width: 100px;
height: 100px;
top: 0;
left: 50%;
display: block;
position: fixed;
border-radius: 150px;
background: powderblue;
animation: do 4s 1 ease;
-webkit-animation-fill-mode: forwards;
}
#middle:hover {
width: 96px;
height: 48px;
left: 50%;
top: 30%;
position: fixed;
border-color: powderblue;
border-style: solid;
border-width: 2px 2px 50px 2px;
border-radius: 100%;
z-index: 1000;
background: white;
}
#middle:hover:before {
content: "";
position: absolute;
top: 50%;
left: 0;
background: white;
border: 18px solid powderblue;
border-radius: 100%;
width: 12px;
height: 12px;
}
#middle:hover:after {
content: "";
position: absolute;
top: 50%;
left: 50%;
background: powderblue;
border: 18px solid white;
border-radius: 100%;
width: 12px;
height: 12px;
}
You can achive the animation with CSS, BUT, to trigger it the way you want it, you will need jQuery.
Here is an example: https://jsfiddle.net/557g66ry/
And some explainations:
I configure the animation slideDown to affect 2 parameters:
item top position (same as you did)
item opacity (to have the fade animation) [that answer your question 1]
#keyframes slideDown {
from {
opacity: 0;
top: 0;
}
to {
opacity: 1;
top: 30%;
}
}
AND for the initial state of the #middle item (without .down class), I had :
opacity: 0;
For ensure that by default, the item isn't visible.
When I decided to activate the animation only when hte #middle item has the class .down.
That's allow me to trigger it when I click on the #half item (using jQuery) [that answer your question 2] :
#middle.down {
animation: slideDown 4s 1 ease;
-webkit-animation-fill-mode: forwards;
}
Finally, I used the same trick to delay the hover effect after the end of the animation.
As we know the duration of the animation (4s), I activate the hover CSS only when the #middle item has the class .arrived.
Then with the help of JavaScript setTimeout function (and jQuery) I add the .arrived class to the item after 4s (4000 = milisecondes) [that answer your question 3]:
setTimeout(function() {
$('#middle').addClass('arrived');
},
4000);
To answer this question: First question, I want the effects that occur when the div is hovered above to fade in and out, rather than it being instant. I tried to use transitions, but it seems to mess with the design of the element, so I'm stuck.
To do this you will need: transition: opacity 300ms; On both the element rule and also the hover rule.
Im now looking at your other questions and will update the answer in the meantime.

Expand CSS3 animation is not working

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.

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

Categories