1

I am using vw and vh values in my CSS transform. It causes the css animation to use the actual window width. In this case the cloud image moves from the left to the right of the screen.

This works fine for any window size, except when I resize the window after the css is loaded. Is there a way to recalculate the vw value after resizing?

I cannot use percentages because that doesn't work with transform:translate.

cloud {
    animation: cloudanimation 2s linear infinite;
}

@keyframes cloudanimation {
  0% {
    transform: translateX(10vw);
  }

  100% {
    transform: translateX(90vw);
    /* percentages don't work correctly with translate */
    /* transform : translateX(90%);*/
  }
}
1
  • This seems to be a bug. Different browsers -> different results. Commented Apr 8, 2016 at 11:18

2 Answers 2

3

When passing relative values to animation these get converted to absolute values, but when you resize these don't get updated;

one solution is to restart animation when the resize event takes place;

with jquery:

$(window).resize(function() {
  //remove animation
  $('div').removeClass('cloud')

  //reflow width 
  let w = $('div').outerWidth();
  $('div').outerWidth(w);

  // add animation class back
  $('div').addClass('cloud')
});

fiddle

to restart animation adding and removing the css class is not enough you also need to reflow width as explained here.


if you can display your element as inline-block these seem to behave consistently without adding and removing animation , fiddle.

4
  • Yes, by using javascript I can reset the animation by removing and adding the class. (Although that will also stop the currently running animation). But I am really looking for a way to do this entirely in CSS : make a div animate from left to right, regardless of window width.
    – Kokodoko
    Commented Apr 8, 2016 at 10:59
  • look at the last fiddle
    – maioman
    Commented Apr 8, 2016 at 11:01
  • Thanks! that latest fiddle works in Chrome. Even when adding browser prefixes it still looks weird in Safari. Must be a safari bug then?
    – Kokodoko
    Commented Apr 8, 2016 at 11:31
  • 1
    can't test it in safari but in FF and chrome it looks ok
    – maioman
    Commented Apr 8, 2016 at 12:25
1

VW and VH are automatically recalculated with window resize.

transform: translateX(10vw);
transform: translateX(90vw);

Maybe the problem is somewhere else? Check here Fiddle

3
  • Actually, it seems that the animation in the Fiddle is NOT using the new width after resizing! When making the window smaller, it takes a lot longer for the cloud div to reappear on the left side. That is because it is still animating outside of the screen.
    – Kokodoko
    Commented Apr 8, 2016 at 10:57
  • I found out that Chrome displays your fiddle correctly. Must be a safari bug. Adding browser prefixes makes no difference.
    – Kokodoko
    Commented Apr 8, 2016 at 11:31
  • 1
    caniuse.com/#feat=viewport-units That's weird, CanIUse reports those units to be supported by Safari. I've had some problems as well with Safari for iPhone with VW and VH, maybe it's just a bug. Commented Apr 8, 2016 at 13:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.