Keyframes Animation has no transition when moving in other direction (Pure CSS) - html

I have this Keyframe Animation where i move a dot in a square:
* {
margin: 0;
padding: 0;
}
#dot {
height: 10px;
width: 10px;
background: #000;
border-radius: 100%;
position: absolute;
animation: moveDotOne 2s infinite;
transition: all 0.3s;
top: 0;
left: 0;
}
#keyframes moveDotOne {
0% {
top: 0;
}
25% {
top: 20px;
}
50% {
left: 20px;
}
75% {
top: 0;
}
100% {
left: 0;
}
}
<div id="dot"></div>
The only problem is that the 25% keyframe is already running when the 0% isn't even finished. How can i fix that?

Think about what the animation is making it transition the value from.
When it hits 50% it sets left: 20px. So it then transitions from something to 20px. What is that something?
You haven't specified anything. So it is the default value.
You can't transition from auto so it jumps.
Set starting values for left and top in your CSS. Don't assign them only with the animation.

As pointed out you need to think about both x&y positions for the transitions - added variables here to make the effect easier to observe. At each keyframe a x and y position (top,left) are specified.
* {
margin: 0;
padding: 0;
}
:root{
--d:80px;
--w:90px;
}
#box{
padding:1rem;border:1px solid red;
width:var(--w);
height:var(--w);
}
#dot {
height: 10px;
width: 10px;
background: #000;
border-radius: 100%;
position: absolute;
animation: moveDotOne 5s infinite;
transition: all 0.3s;
margin:1rem;
}
#keyframes moveDotOne {
0% {
top:0;
left:0;
}
25% {
top:var(--d);
left:0;
}
50% {
top:var(--d);
left:var(--d);
}
75% {
top:0;
left:var(--d);
}
100%{
top:0;
left:0;
}
}
<div id='box'>
<div id="dot"></div>
</div>

Related

Using #keyframes for megamenu show/hide

I've been playing around with using #keyframes to build a simple mega menu. I'm only learning but would this be an accepted way to show/hide the <nav> items for use in the real world? More than anything I wanted the transistion in/out effect but kept triggering the menu by hovering over the opaque object.
https://codepen.io/ngcook1985/pen/bGvqRRp
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.header {
height: 80px;
width: 100%;
background-color: rgb(84, 161, 228);
position: relative;
}
.menu {
position: absolute;
height: 120px;
width: 100%;
top: -180px;
background-color: rgb(74, 234, 181);
opacity: 0;
animation: hide .3s linear;
}
.header:hover .menu {
animation: show .3s linear;
opacity: 1;
top: 80px;
}
#keyframes show {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
#keyframes hide {
0% {
opacity: 1;
top: 80px;
}
100% {
top: 80px;
opacity: 0;
}
}
<body>
<div class="header">
<div class="menu"></div>
</div>
</body>

Stop line animation in between at center

I want this line will move smoothly from right to left but at the center it will stop for 4-5 seconds. Here we can also add some more lines,when first line came to center from right side ,it stops there and wait for another lines and when all lines came at center then all lines stop there for few seconds then again lines start moving in right side. This will continue for infinite loop.
.spinner {
position: relative;
top: 0;
left: 0;
width: 150px;
border: 15px solid transparent;
border-top-color: red;
animation: load 5s infinite ease-in-out;
}
#div {
animation-timing-function: cubic-bezier(0, 1, 1, 1);
}
#keyframes load {
0% {
opacity: 0;
left: 80%;
}
50% {
opacity: 1;
left: 45%;
}
100% {
opacity: 0;
left: 10%;
}
}
<div id="div" class="spinner"></div>
To "hold" the animation for 4 s means it will take 80% of the initial animation duration you defined. So in order to do that, just define the middle keyframe to span from 10% to 90%, leaving equal amount of 10% on both sides:
.spinner {
position: relative;
top: 0;
left: 0;
width: 150px;
border: 15px solid transparent;
border-top-color: red;
animation: load 5s infinite ease-in-out;
}
#div {animation-timing-function: cubic-bezier(0,1,1,1)}
#keyframes load {
0% {
opacity: 0;
left: 80%;
}
10%, 90% {
opacity: 1;
left: 45%;
}
100% {
opacity: 0;
left: 10%;
}
}
<div id="div" class="spinner"></div>

How to make concave animation with CSS

So, I have sidebar that just display some of its content, when it hovered it will display all the sidebar width.
.sidenav {
height: 100%;
width: 100px;
position: fixed;
z-index: 2;
top: 0;
left: 0;
background-color: #fff;
overflow: hidden;
padding-top: 20px;
transition: 0.8s;
opacity: 0.8;
box-shadow: 0px 20px 50px black;
border-radius: 0;
background: black;
}
.sidenav:hover {
width: 215px;
overflow: hidden;
animation-name: roundborder;
animation-duration: 1s;
animation-iteration-count: 1;
}
#keyframes roundborder {
0% { border-radius: 0; }
50% { border-radius: 0 50% 50% 0; }
100% { border-radius: 0; }
}
<div class="sidenav"></div>
my question is how to make concave animation if the sidebar is not hover? so after it hovered and the pointer not in sidebar again, it will back to the initial state but with concave animation, I cant use the negative percent, so what do I use for it? thanks
I guess that you have to do with SVG you create a normal square and then two
animation the first when it's out with rounded corner then the second is with
concave corner.
Maybe I am misunderstanding the question but can you not just put the same animations on the div (without :hover). Like this:
.sidenav {
height: 100%;
width: 100px;
position: fixed;
z-index: 2;
top: 0;
left: 0;
background-color: #fff;
overflow: hidden;
padding-top: 20px;
transition: 0.8s;
opacity: 0.8;
box-shadow: 0px 20px 50px black;
border-radius: 0;
background: black;
animation-name: roundborder2;
animation-duration: 1s;
animation-iteration-count: 1;
}
.sidenav:hover {
width: 215px;
overflow: hidden;
animation-name: roundborder;
animation-duration: 1s;
animation-iteration-count: 1;
}
#keyframes roundborder {
0% { border-radius: 0; }
50% { border-radius: 0 50% 50% 0; }
100% { border-radius: 0; }
}
#keyframes roundborder2 {
0% { border-radius: 0; }
50% { border-radius: 0 50% 50% 0; }
100% { border-radius: 0; }
}
<div class="sidenav"></div>

css top property is not working properly on animation

There is an issue with this property, while trying to animate a text, I'm using a text cursor to follow the text but on certain point of the animation this "cursor" (just a line) doesn't do what I put on the code, so... I don't know what is happening to it.
Here you have the piece of code:
.code {
position: relative;
width: 0px;
height: 180px;
animation: coding 1.4s;
animation-fill-mode: forwards;
animation-timing-function: steps(20);
overflow: hidden;
}
#keyframes coding {
0% {
width: 0;
}
100% {
width: 230px;
}
}
.code p {
color: red;
width: 258px;
letter-spacing: 3px;
display: inline-block;
}
.code span {
position: absolute;
top: 10px;
right: 0;
color: red;
animation: cods 7s;
animation-fill-mode: forwards;
font-size: 20px;
}
#keyframes cods {
0% {
opacity: 1;
top: 10px;
right: 0;
}
50% {
top: 10px;
right: 0;
}
75% {
top: 30px;
right: 0;
}
100% {
top: 30px;
left: 0;
}
}
<div class="code">
<p><I am the animated text></p><span>|</span>
</div>
as you see here, the cursor first go to the left and then go to the bottom, but that's not on the code. from 50% to 75% I'm telling: "go 20px down" and then from 75% to 100%: "go left".
Fixed it by changing left: 0 into right: 100% in the 100% keyframe!

avoiding slow motion when using css keyframes

I'm trying to achieve this button animation on hover using only CSS solution :
I've succeed on find a way doing this with css keyframe , but now i'm facing some an unexpected slow motion effect, for now I'm only experimenting this with the top left corner here is what I've done so far :
HTML
<div class="borderTop"></div>
CSS
a {
width: 150px;
height: 50px;
border: 2px solid;
margin: 0 auto;
margin-top: 20%;
display: block;
}
a:hover .borderTop {
width: 10px;
height: 2px;
border-top: 2px solid;
position: relative;
top: -2px;
-webkit-animation: topTheleft 2s alternate;
animation: topTheleft 2s alternate;
}
.borderTop {
width: 10px;
height: 2px;
border-top: 2px solid;
position: relative;
top: -2px;
left: 50px;
}
#-webkit-keyframes topTheleft {
0% { left: -2px; }
50% { left: -30px; }
100% { left: -70px; display: none; }
}
#-o-keyframes topTheleft {
0% { left: -2px; }
50% { left: -30px; }
100% { left: -70px; display: none; }
}
#-moz-keyframes topTheleft {
0% { left: -2px; }
50% { left: -30px; }
100% { left: -70px; display: none; }
}
#keyframes topTheleft {
0% { left: -2px; }
50% { left: -30px; }
100% { left: -70px; display: none; }
}
LIVE DEMO
any help on how to avoid this slow motion on the middle of the animation would be highly appreciated , thank you in advance
edit, is there a way to make the line hide when reach the left: -70px with a transition effect not ansta-hide, any other solution to do it are welcome too
Try getting rid of the 50% lines:
#-webkit-keyframes topTheleft {
0% { left: -2px; }
100% { left: -70px; display: none; }
}
#-o-keyframes topTheleft {
0% { left: -2px; }
100% { left: -70px; display: none; }
}
#-moz-keyframes topTheleft {
0% { left: -2px; }
100% { left: -70px; display: none; }
}
#keyframes topTheleft {
0% { left: -2px; }
100% { left: -70px; display: none; }
}
It looks like the default timing function is ease-in-out : from one animation step to the other, the speed goes slow-fast-slow, to make it look more natural (real physics cannot make an object got from speed 0 to 100 instantaneously).
So what happens is the animation starts slow at 0%, goes fast, then slows down for the 50% step, then accelerates again
Is this what you are looking for ?
https://jsfiddle.net/kvyqyg19/1/
a:hover .borderTop {
/* .. */
-webkit-animation: topTheleft 2s alternate;
animation: topTheleft 2s alternate;
animation-timing-function: linear;
}
#-webkit-keyframes topTheleft {
0% { left: -2px; }
100% { left: -70px; display: none; }
}
/* .. */
I removed the middle (50%) step and set the animation-timing-function: linear;