Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining keyframe animations inside media queries

I'm trying to get objects to "slide in" from the bottom of the screen, but since I can't get the screen height as a unit in CSS, I'm trying to do this with media queries, like so:

@media(max-height:500px) {   @keyframe slideUp {     0%    { transform: translate3d(0,500px,0); }     100%  { transform: translate3d(0,0,0); }   } } @media(max-height:750px) {   @keyframe slideUp {     0%    { transform: translate3d(0,750px,0); }     100%  { transform: translate3d(0,0,0); }   } } /* etc. */ 

This doesn't work (it uses the first version of slideUp regardless of height), so I assume keyframes, once defined, cannot be overwritten or reassigned based on media queries? Is there any way to achieve this effect (short of having many different keyframe setups and using a media query to assign the appropriate one to the class)?

like image 444
futuraprime Avatar asked Dec 05 '13 18:12

futuraprime


People also ask

How do you keyframe in media query?

Just move the @keyframe rule outside of the media query block, then all browsers are happy. You can still apply the animation to the devices you want by wrapping the animation property in a media query should you wish. Take a look at the example below and try it out in various browsers.

How do you define a keyframe?

In animation and filmmaking, a key frame (or keyframe) is a drawing or shot that defines the starting and ending points of a smooth transition. These are called frames because their position in time is measured in frames on a strip of film or on a digital video editing timeline.

How do I delete an animation in media query?

Use what ever media queries you use for mobile devices and use the * wildcard to apply the no transition to all elements. If you have animations as well as transitions you can try setting the animation-name:none; which should disable all animations.


2 Answers

I don't know why no one else has suggested this, but instead of setting the keyframes in the media query you can set the animation in the media query.

@media(max-height:500px)  {     #selectorGroup {         animation: slideUp500 1s forwards;     } }  @media(max-height:750px)  {     #selectorGroup {         animation: slideUp750 1s forwards;     } }   @keyframes slideUp500 {     0%    { transform: translate3d(0,500px,0); }     100%  { transform: translate3d(0,0,0); } }  @keyframes slideUp750 {     0%    { transform: translate3d(0,750px,0); }     100%  { transform: translate3d(0,0,0); } } 
like image 103
Simon_Weaver Avatar answered Oct 23 '22 15:10

Simon_Weaver


Nowadays you can solve this using native CSS variables.

In your case:

@media(max-height:500px) {     :root {         --slide-up-y: 500px     } }  @media(max-height:750px) {     :root {         --slide-up-y: 750px     } }  @keyframes slideUp {     0%    { transform: translate3d(0,var(--slide-up-y),0); }     100%  { transform: translate3d(0,0,0); }   } } 
like image 26
tomijovanoski Avatar answered Oct 23 '22 15:10

tomijovanoski