How can I use animation in styled-components css``? - css

I read documents, and I used keyframes`` for declare css animation.
But I got error message like this:
Please wrap your string in the css`` helper which ensures the styles are injected correctly.
So, I wrapped animation with css`` instead keyframes``, but animation didn't work.
const rippleAnimation = css`
0% {
transform: translate(-50%, -50%) scale(1);
}
100% {
transform: translate(-50%, -50%) scale(var(--material-scale));
opacity: 0;
}
`;
This didn't work well.
I don't know I wrote perfectly...
ordinary code(error about keyframes) :
const rippleAnimation = keyframes`
0% {
transform: translate(-50%, -50%) scale(1);
}
100% {
transform: translate(-50%, -50%) scale(var(--material-scale));
opacity: 0;
}
`;

You can do it like this. Create keyframes in CSS. And add the keyframes with js. You can also add this keyframe after clicking in js.
const hello = document.querySelector(".hello")
hello.style.animation = "5s colorChange linear infinite"
.hello {
color: #000000;
}
#keyframes colorChange {
0% {
color: black;
}
50% {
color: red;
}
100% {
color: black;
}
}
<h1 class="hello">Hello World</h1>

Related

CSS - remove box shadow with keyframes animation

I have an icon that initially has a box shadow set. I am animating the icon and scaling it, but I would also like to remove the shadow while I am doing it. I have tried it like this:
.loading-icon {
width: 80px;
height: 80px;
animation-name: earth;
animation-timing-function: ease-in-out;
animation-duration: 3s;
}
#keyframes earth {
0% {
transform: scale(1);
}
100% {
transform: scale(1.5) { box-shadow: none; };
}
}
But, this is not working, how can I do that?
In keyframes you've extra {} surrounding box-shadow which is not needed.
#keyframes earth {
0% {
transform: scale(1);
}
100% {
transform: scale(1.5);
box-shadow: none;
}
}

Vuetify button with loader with text inline

I'm trying to use loader inside button along with text. I've modified example from vuetify documentation:
<span slot="loader">
<v-icon light class="custom-loader">cached</v-icon>
Loading...
</span>
codepen
On click event button text changes and loader appears. However button's content is not inline.
Please advise how to make button content composed inline and independent of text width?
DEMO: https://codepen.io/anon/pen/ejgEoO
I've added styles for the classes used by vuetify: .v-btn__loading and .v-btn--loader .v-btn__content: So your CSS becomes:
.v-btn__loading {
position: relative;
}
.v-btn--loader .v-btn__content {
display: none;
}
.custom-loader {
animation: loader 1s infinite;
}
#-moz-keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
#-webkit-keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
#-o-keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
#keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}

CSS animation not following rules specified in the keyframes?

Codepen
<div></div>
html, body {
margin: 0;
padding: 0;
}
div {
width: 150px;
height: 100px;
background: black;
transform: translate(100px, 50px) rotate(140deg);
animation: circle-top 1.5s ease-in-out forwards;
}
#keyframes circle-top {
10% {
transform: translate(500, -190px) rotate(120deg);
}
30% {
transform: translate(300, -150px) rotate(100deg);
}
70% {
transform: translate(100, -50px) rotate(360deg);
}
90% {
transform: translate(50, -30px) rotate(30deg);
}
100% {
transform: translate(0, 0) rotate(0);
}
}
I specified some random rotation in the keyframes. Obviously the animation is not following the rules because it seems to be so smooth and not how I specified.
#keyframes circle-top {
10% {
transform: translate(500, -190px) rotate(120deg);
/* !!! */
}
Your invalid value for the first translate parameter is causing most of your keyframes to become invalid as well.
Every length in CSS always needs a unit, unless the value happens to be 0.

Smooth CSS transform animations

I'm looking at optimising CSS animations for performance.
From what I can find out using
.item { transform: translate(-25px,-50px)
is much more efficient than
.item { left: -25px; top: -50px }
I've setup a little animation that move and rotates a box .balloon which as multiple steps to the animation. The problem is though the animation becomes very jerky, even with positioning ans rotation declared for each step
Here is my standard CSS
#keyframes balloon {
0%,100% { left:809px; top:50px; transform: rotate(0deg) }
10% { transform: rotate(-9deg) }
25%,75% { top:25px }
50% { left:235px;top:75px }
45% { transform: rotate(3deg) }
75% { transform: rotate(12deg) }
}
This is my optimised CSS, which is where the jerky animation comes in to effect
#keyframes balloon {
0% { transform: translate(0,0) rotate(0deg) }
10% { transform: translate(-57.5px,-10px) rotate(-9deg) }
25% { transform: translate(-143.75px,-25px) rotate(-4deg) }
45% { transform: translate(-517.5px,22.5px) rotate(3deg) }
50% { transform: translate(-575px,25px) rotate(4.5deg) }
75% { transform: translate(-287.5px,-25px) rotate(12deg) }
100% { transform: translate(0,0) rotate(0deg) }
}
Is there an alternative solution for this?
I've put a CodePen together here.
In your animation-property, try to add a value for the animation-timing-function
Something like
animation: balloon 15s infinite linear;
would make it more smooth.

How to modify a CSS tag value dynamically using javascript

Perspective based rotation : I have a div which i want to rotate in CSS3 using webkit. The div is rotated after a usermouse down event in jquery
I use keyframes to do that
#-webkit-keyframes rotate {
enter code here
0% {
-webkit-transform: rotate(0deg);
}100% {
-webkit-transform: rotate(90deg);
}
}
#-webkit-keyframes rotate2 {
0% {
-webkit-transform: rotate(90deg);
-webkit-transform: rotateX(45deg);
}100% {
-webkit-transform: rotate(180deg);
-webkit-transform: rotateX(45deg);
}
}
#-webkit-keyframes rotate3 {
0% {
-webkit-transform: rotate(180deg);
}100% {
-webkit-transform: rotate(270deg);
}
}
#-webkit-keyframes rotate4 {
0% {
-webkit-transform: rotate(270deg);
}100% {
-webkit-transform: rotate(360deg);
}
}
My question is I have also applied webkit perspective to give a 3d kind of view and I have to rotate the div keeping the perspective X angle to 45deg...how is it possible ?
body {
-webkit-transform-style: preserve-3d;
-webkit-perspective: 500;
-webkit-perspective-origin: 45%;
}
#mydiv {
height: 300px;
width: 100px;
border: 2px solid #D3DAED;
position:absolute;
top:29%;
left:45%;
**-webkit-transform: rotateX(45deg);**
}
I don't really understand exactly what you are trying to do, but in jquery you can alter css dynamically like this:
$('#mydiv').css('-webkit-transform', 'rotateX(45deg)');

Resources