I have two types of elements, let's call them .a
and .b
.
They may have some CSS animations set on them. I don't have control over these keyframes, over whether they're set or not, over what they're animating.
They may be animating opacity
. However, I want the opacity
on my .a
elements to stay a certain value, let's say 1
, regardless of whether it is animated or not.
Consider the following code, where I have three cases:
- there's no animation being set on my elements
- there's an animation, but it's not animating
opacity
- there's an animation and
opacity
is among the properties being animated
div {
/* some dummy styles so we can see stuff */
display: inline-block;
width: 5em; height: 5em;
background: purple;
}
[class*='ani'] { animation: a 1s ease-out infinite alternate }
.ani--one { animation-name: ani-one }
@keyframes ani-one { to { transform: scale(.5) } }
.ani--two { animation-name: ani-two }
@keyframes ani-two {
to {
opacity: 0;
background: orange;
}
}
<div class='a'></div>
<div class='b'></div>
<div class='a ani--one'></div>
<div class='b ani--one'></div>
<div class='a ani--two'></div>
<div class='b ani--two'></div>
In the first two cases (1 = no keyframe animation and 2 = keyframe animation, but it's not animating opacity
) I don't need to do anything.
In the last case, however, I need to somehow force the opacity
of the .a
element to 1
, cancel the animation effect on just that property.
I can't remove the keyframe animation from the .a
element because I want the other properties (background
in this case, whatever else may be in the general case) to keep on being animated.
I can't alter the animation because I want it to work as initially specified, animating the opacity
for other elements (.b
).
So the question is, how can I detect if, on an .a
element, the opacity
property is being animated and, if it is, how can I force its value to stay at 1
, while the other properties set via the same keyframes are being animated?
I want to solve this problem using vanilla JS, no libraries.