I have been trying to get this to work for a while.
The point is that the inner div will have some shape and there will probably more than one (That's why I used the nth-child selector).
This inner div is supposed to be shown and then be hidden again both for some set amount of time.
the problem is, that I would like to animate all the (later) multiple inner divs in one animation. For this I thought I could use CSS variables, but this does not seem to work.
What I am trying to archieve in this example is the inner div basically just blinking by using the variable. But my result in Firefox is just a black box.
Am I missing anything? I already looked up if one could even use CSS variables in #keyframes and sure enough you can.
The only problem with them in animations seems to be that they are not interpolated in between but that they suddenly switch which is not a problem in this case.
#keyframes test{
from{
--one: 0;
}
to{
--one: 1;
}
}
#test{
width: 100px;
height: 200px;
background-color: black;
animation: test 1s infinite;
}
#test :nth-child(1){
width: 20px;
height: 20px;
margin: auto;
background-color: white;
opacity: var(--one,0);
}
<div id="test">
<div></div>
</div>
This can be achieved by defining variables using (as of writing this, not well-supported) #property, which allows declaring types and that allows the browser to "understand", for example, that a certain property (variable) is a Number and then it can gradually animate/transition that variable.
Example Code:
#property --opacity {
syntax: '<number>'; /* <- defined as type number for the transition to work */
initial-value: 0;
inherits: false;
}
#keyframes fadeIn {
50% {--opacity: 1}
}
html {
animation: 2s fadeIn infinite;
background: rgba(0 0 0 / var(--opacity));
}
The current types that are allowed include:
length, number, percentage, length-percentage, color, image, url, integer, angle, time, resolution, transform-list, transform-function, custom-ident (an identifier string)
Helpful articles:
https://web.dev/at-property/#writing-houdini-custom-properties
https://css-tricks.com/using-property-for-css-custom-properties
Cool Houdini demos
As stated in the specification:
Animatable: no
and also
Notably, they can even be transitioned or animated, but since the UA
has no way to interpret their contents, they always use the "flips at
50%" behavior that is used for any other pair of values that can’t be
intelligently interpolated. However, any custom property used in a
#keyframes rule becomes animation-tainted, which affects how it is
treated when referred to via the var() function in an animation
property.
So even if you use opacity with var() in the keyframes it won't animate:
#keyframes test {
from {
--one:0;
opacity: var(--one);
}
to {
opacity: var(--one);
--one: 1;
}
}
#test {
width: 100px;
height: 200px;
background-color: black;
}
#test :nth-child(1) {
width: 20px;
height: 20px;
margin: auto;
background-color: white;
animation: test 1s infinite;
}
<div id="test">
<div></div>
</div>
By the way you can make it working if you use it as a transition because in this case you will apply a transtion to the opacity and not the custom property:
#test {
width: 100px;
height: 200px;
background-color: black;
}
#test:hover {
--one:1;
}
#test :nth-child(1) {
width: 20px;
height: 20px;
margin: auto;
background-color: white;
opacity: var(--one,0);
transition:1s all;
}
<div id="test">
<div></div>
</div>
Related
I have been trying to get this to work for a while.
The point is that the inner div will have some shape and there will probably more than one (That's why I used the nth-child selector).
This inner div is supposed to be shown and then be hidden again both for some set amount of time.
the problem is, that I would like to animate all the (later) multiple inner divs in one animation. For this I thought I could use CSS variables, but this does not seem to work.
What I am trying to archieve in this example is the inner div basically just blinking by using the variable. But my result in Firefox is just a black box.
Am I missing anything? I already looked up if one could even use CSS variables in #keyframes and sure enough you can.
The only problem with them in animations seems to be that they are not interpolated in between but that they suddenly switch which is not a problem in this case.
#keyframes test{
from{
--one: 0;
}
to{
--one: 1;
}
}
#test{
width: 100px;
height: 200px;
background-color: black;
animation: test 1s infinite;
}
#test :nth-child(1){
width: 20px;
height: 20px;
margin: auto;
background-color: white;
opacity: var(--one,0);
}
<div id="test">
<div></div>
</div>
This can be achieved by defining variables using (as of writing this, not well-supported) #property, which allows declaring types and that allows the browser to "understand", for example, that a certain property (variable) is a Number and then it can gradually animate/transition that variable.
Example Code:
#property --opacity {
syntax: '<number>'; /* <- defined as type number for the transition to work */
initial-value: 0;
inherits: false;
}
#keyframes fadeIn {
50% {--opacity: 1}
}
html {
animation: 2s fadeIn infinite;
background: rgba(0 0 0 / var(--opacity));
}
The current types that are allowed include:
length, number, percentage, length-percentage, color, image, url, integer, angle, time, resolution, transform-list, transform-function, custom-ident (an identifier string)
Helpful articles:
https://web.dev/at-property/#writing-houdini-custom-properties
https://css-tricks.com/using-property-for-css-custom-properties
Cool Houdini demos
As stated in the specification:
Animatable: no
and also
Notably, they can even be transitioned or animated, but since the UA
has no way to interpret their contents, they always use the "flips at
50%" behavior that is used for any other pair of values that can’t be
intelligently interpolated. However, any custom property used in a
#keyframes rule becomes animation-tainted, which affects how it is
treated when referred to via the var() function in an animation
property.
So even if you use opacity with var() in the keyframes it won't animate:
#keyframes test {
from {
--one:0;
opacity: var(--one);
}
to {
opacity: var(--one);
--one: 1;
}
}
#test {
width: 100px;
height: 200px;
background-color: black;
}
#test :nth-child(1) {
width: 20px;
height: 20px;
margin: auto;
background-color: white;
animation: test 1s infinite;
}
<div id="test">
<div></div>
</div>
By the way you can make it working if you use it as a transition because in this case you will apply a transtion to the opacity and not the custom property:
#test {
width: 100px;
height: 200px;
background-color: black;
}
#test:hover {
--one:1;
}
#test :nth-child(1) {
width: 20px;
height: 20px;
margin: auto;
background-color: white;
opacity: var(--one,0);
transition:1s all;
}
<div id="test">
<div></div>
</div>
So I just saw this question where op defined an animation called flash. It causes weird behaviour in all browsers I tested (Chrome and Firefox - both recently updated):
The animation values I enter get ignored and instead the block fades in and out with a yellow color - kind of like a flash.
Here's a screenshot of what I'm seeing:
I couldn't find anything about predefined CSS animations and I can't seem to override them either.
Here's the code snippet:
.block {
height: 100px;
width: 100px;
background-color: red;
}
.flash {
animation: flash 2s infinite;
}
#keyframes flash {
0% {
background-color: blue;
}
50% {
background-color: red;
}
100% {
background-color: blue;
}
}
.notFlash {
animation: notFlash 2s infinite;
}
#keyframes notFlash {
0% {
background-color: blue;
}
50% {
background-color: red;
}
100% {
background-color: blue;
}
}
<div class='block flash'>Flash Animation Name</div>
<div class='block notFlash'>A Different Animation Name</div>
What is happening here? Does such a thing as preset animations exist?
I couldn't find anything googling.
StackOverflow uses an animation with that name in its own styles. As the StackSnipppet is not sandboxed, this style overrides the one you define in the snippet.
The following snippet shows the animation in use in the console output to indicate a new item has been logged to the console:
let i = 0
setInterval(() => {
console.log(++i)
}, 1000)
You can see that this is not the case if you try a different snippet tool or run your code locally.
See the post on meta for more information (linked by esqew in the comments).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have some right to left scrolling text that teleports back to the origin at the end of the animation, while I am more so looking for it to disappear out of and appear into the margin.
.example1 {
height: 50px;
overflow: hidden;
position: relative;
}
.example1 h3 {
position: absolute;
width: 100%;
height: 100%;
margin: 0;
color: white;
line-height: 50px;
animation: example1 10s linear infinite;te
}
#keyframes example1 {
from {
margin-left: 60%;
width: 300%;
}
to {
margin-left: 35%;
width: 100%;
}
}
body {
background-color: black;
}
<div class="example1">
<h3>text</h3>
</div>
If you need your CSS animation to only run once, you have to set the animation-iteration-count property to 1 (in fact, to not set it at all, as 1 is its default value).
You're currently setting it to infinite, using the animation shorthand, which sets multiple animation properties in one single declaration. Just remove infinte from that line. You should also remove the te following that declaration, which is invalid CSS.
To have your animation animate multiple properties, you can add as many animatable properties to your keyframes and they will animate accordingly. In your case, adding a 50% keyframe with opacity:1 and adding opacity:0 to the to keyframe will make your element fade from 1 to 0 starting at half of the animation until its end.
Using animation-timing-function, particularly with timing functions (a.k.a. as easings), allows adding acceleration and deceleration to animations, making them look more "natural", especially when used on movement animations.
Another handy property of CSS animations is the animation-fill-mode. It allows setting the animated properties to the values they have been animated to, when the animation ends (as opposed to being reset to any applying CSS). This avoids the "jump" whenever you have animated a property to a different value that what normally applies to it.
Last note, on performance: to make sure your animations run smoothly on any device, you should only animate properties which do not trigger repaints on subsequent elements. In fact, you should strive to ever animate only 2 properties: transform and opacity. In your case, rather than animating margin-left, which moves your element around and triggers repaint on subsequent elements in DOM, you should never actually move it and use transform to paint it at different positions.
Here's an example (not sure if this is what you asked for, but you can play around with it some more):
body {
overflow-x: hidden;
background-color: #212121;
}
.example1 h3 {
color: white;
font-size: 3rem;
margin: 0;
line-height: 50px;
animation: example1 5s cubic-bezier(.4,0,.2,1) forwards;
}
#keyframes example1 {
from{
transform: translateX(107%);
}
38% {
opacity: 1;
}
42% {
transform: translateX(35%);
}
60% {
opacity: 0;
transform: translateX(35%);
}
62% {
transform: translateX(0);
opacity: 0;
}
to {
opacity: 1;
}
}
<div class="example1">
<h3>text</h3>
</div>
If, on the contrary, you want your animation looping but want to simply create a smooth transition between end and start, the golden rule is in both from and to keyframes the animated properties have to have the same values (because default value of transform:translateX() is 0 and of opacity is 1, I don't need to set them in from - that's the starting point):
body {
overflow-x: hidden;
background-color: #212121;
}
.example1 h3 {
color: white;
text-align:right;
padding-right: 1rem;
font-size: 3rem;
margin: 0;
line-height: 50px;
animation: example1 5s cubic-bezier(.4,0,.2,1) infinite;
}
#keyframes example1 {
38% {
opacity: 1;
}
42% {
transform: translateX(-60%);
}
58% {
opacity: 0;
transform: translateX(-60%);
}
62% {
transform: translateX(0);
opacity: 0;
}
to {
opacity: 1;
}
}
<div class="example1">
<h3>text</h3>
</div>
For more on animation syntax and examples, I recommend MDN, a well curated documentation library, joint effort of Mozilla, Google, Microsoft and many, many others. Arguably, its most useful feature is linking, in the Specifications section, at the bottom, currently applying standards for the respective property or method, so you don't have to waste time tracking them yourself.
You can use more keyframes percentage to control better your animation in stead of using just two keyframes (from/to).
Below a quick example:
.example1 {
height: 50px;
overflow: hidden;
position: relative;
}
.example1 h3 {
position: absolute;
width: 100%;
height: 100%;
margin: 0;
color: white;
line-height: 50px;
animation: example1 10s linear infinite;te
}
#keyframes example1 {
0% {
margin-left: 60%;
width: 300%;
opacity: 0;
}
50% {
opacity: 1;
}
100% {
margin-left: 35%;
width: 100%;
opacity: 0;
}
}
body {
background-color: black;
}
<div class="example1">
<h3>text</h3>
</div>
Instead of animating the margin-left style you should animate the left style, example:
.example1 {
height: 50px;
width: 400px;
overflow: hidden;
position: relative;
margin: 0 auto;
}
.example1 h3 {
position: absolute;
top: 0;
left: 100%;
line-height: 50px;
animation: example1 10s linear infinite;
}
#keyframes example1 {
from {
left: 100%;
}
to {
left: -10%;
}
}
<div class="example1">
<h3>text</h3>
</div>
I have a basic setup. When I increase the height, I get a smooth increase. When decreased, I should get a smooth decrease but instead a sharp decrease.
<div className="foo <!-- -->">Hey</div>
You may have noticed className and <!-- -->, I'm using react. <!-- --> gets replaced with the class to decrease the height.
// SCSS
.foo {
height 400px;
// background props
transition: all 250ms ease-out
}
.foo.decreaseClass {
height: 40px;
transition: all 250ms ease-in
}
When the new class is attached, the div becomes
<div className="foo decreaseClass">Hey</div>
How to get both transitions down/up?
It's because you're not properly closing the height declaration in .foo. You're using a comma instead of a semi-colon, rendering both height and transition declarations invalid. Also note the same declaration should contain a colon between the style property name and its value (height: 400px;).
Therefore, your element only has defined height and transition only when having both classes.
See it working:
document.querySelector('.foo').addEventListener('click', function(event) {
event.target.classList.toggle('decreaseClass')
})
.foo {
height: 200px;
transition: all 250ms ease-out;
border: 1px solid
}
.foo.decreaseClass {
height: 40px;
transition-timing-function: ease-in
}
<div class="foo">Hey</div>
Use CSS #keyframe animation and alternate properties. infinite is added just for demo purposes. Instead of height I added transform:scaleY(1) to (10).
Demo
body {
overflow: hidden
}
.test {
width: 200px;
margin: 10px;
background: red;
font-size: 32px;
text-align: center;
color: white
}
.B {
height: 40px;
animation: animB 1s alternate infinite;
transform-origin: top;
}
#keyframes animB {
0% {
transform: scaleY(1);
}
100% {
transform: scaleY(10);
}
}
<div class='test B'>TEST</div>
.box {
width: 100px;
height: 100px;
background: red;
transition: all 1s ease-in-out;
}
.box:hover {
background: yellow;
}
<div class="box"></div>
If the cursor is over the .box for less than a second, the transition stops and falls back to it's original phase.
Is there a way to somehow force the whole animation, regardless of hover duration?
fiddle
Edit: Similar solution but relying on transition and animation: https://jsfiddle.net/ok7pnrsL/
This is my solution: https://jsfiddle.net/9yu0cozq/1/
Basically you need to add a container for the box and then play with CSS animations.
<div id="container">
<div class="box"></div>
</div>
When the mouse enters the .box then the hidden container appears (please note that for this to work that container should have enough width and height to fit the whole area where the mouse might go).
This container creates an animation for itself to "hide" back in 1s. and while it is shown the .box has an animation for the same time.
#container {
position:absolute;
z-index:1;
width: 0;
height: 0;
}
#container:hover{
animation-name:changeSize;
animation-duration: 1s;
}
#container:hover .box{
animation-name:changeColor;
animation-duration: 1s;
}
.box {
z-index:0;
position:absolute;
width: 100px;
height: 100px;
background: red;
transition:1s background;
}
.box:hover {
background: yellow;
}
#keyframes changeColor {
0% {
background: red;
}
100% {
background: yellow;
}
}
#keyframes changeSize {
0%,99% {
width: 100%;height: 100%;
}
100% {
width: 0;height: 0;
}
}
So, without knowing the real context, this solution gives a series of assumptions that might or might not fit your exact case but gives an idea of how to solve it using pure CSS.
I think you heave to use JS for this. First you need to create animation for background change, and and then you can set it as class and add that class on hover, and remove it when animation ends or on webkitAnimationEnd.
$('.box').hover(function() {
$(this).addClass('animate');
$(this).on('webkitAnimationEnd', function() {
$(this).removeClass('animate');
})
})
.box {
width: 100px;
height: 100px;
background: red;
display: inline-block;
margin: 10px;
}
.box.animate {
animation: changeColor 2s linear;
}
#keyframes changeColor {
0% {
background: red;
}
50% {
background: yellow;
}
100% {
background: red;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box"></div>
<div class="box"></div>
I don't think you can do this without javascript, but it would be interesting to find out.
A light weight javascript solution could be something like this:
// Get the elemnt
var myDiv = document.getElementById('box');
// Detect hover
myDiv.onmouseover = function() {
// Add a force class to the element
myDiv.className += " force";
// Reset the cass name after 1sec (100ms)
setTimeout(function(){ myDiv.className = "box"; }, 1000, myDiv);
}
Change your markup slightly to make things easier for now:
<div id="box" class="box"></div>
And add an extra class to your css styles along with the hover state:
.box {
width: 100px;
height: 100px;
background: red;
transition: all 1s ease-in-out;
}
.box.force,
.box:hover {
background: yellow;
}
Check the jsfiddle