Home > other >  CSS Transition : large div disappears completely while animating
CSS Transition : large div disappears completely while animating

Time:10-14

I have some problems with a CSS transition effect. I don't understand why, but it isn't working. Here is a demo that isn't working :

https://codyhouse.co/demo/ink-transition-effect/index.html

Here is an article about how this effect was done (before, when it did work) :

https://codyhouse.co/gem/ink-transition-effect

The code I'm working on to debug is this one :

https://codepen.io/1019/pen/YzxzNGX

HTML file :

<body>
  CSS ANIMATIONS TEST
  <div class='cd-transition-layer'>
    <div class="bg-layer"></div>
  </div>
</body>

CSS file :

.cd-transition-layer {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 30;
    height: 100%;
    width: 100%;
}

.cd-transition-layer .bg-layer {
    position: absolute;
    left: 50%;
    top: 50%;
    z-index: 15;
    transform: translateY(-50%) translateX(-2%);
    height: 100%;
    width: 2500%;
    background: url('https://i.imgur.com/9uDdPAP.png') no-repeat 0 0;
    background-size: 100% 100%;
    animation: cd-sprite 5s steps(24);
    animation-fill-mode: forwards
}

.cd-transition-layer.opening .bg-layer {
    z-index: 15;
    animation: cd-sprite .8s steps(24);
    animation-fill-mode: forwards
}

@keyframes cd-sprite {
    0% {
        transform: translateY(-50%) translateX(-2%)
    }

    100% {
        transform: translateY(-50%) translateX(-98%)
    }
}

Can you please help me find what is wrong ?

Thank you !

EDIT : Okay, weird : it seems the div just completely disappears during the animation before reappering. If I keep focus on the div in the inspector, it stays there. Is it because it's too long (2500% width) ?

CodePudding user response:

Moving large divs

It seems that animating a large div over the screen very fast can cause a render/flicker in webkit based browsers.

If i have to guess, it's probably due to performance reasons, where the browser cuts off things thats are not in the viewport. when moving to the next frame, it will not have the pixels ready to be rendered, resulting in a flicker.
It becomes more apparent when you remove the steps(24) from the animation.
The div will slide over the screen, and at some point just stop being visible.

Using background-position instead

When animating, instead of moving a div over the screen, we can also opt to move only the background instead.

  background: url("https://i.imgur.com/9uDdPAP.png") no-repeat;
  background-size: 2500% 100%;  /* Size is needed to stretch 1 frame to fit the div */
  background-position: 0% 0%; /* we can start from frame 0 */
 animation: cd-sprite 1s steps(24);

/* the animation is the same, we only move the background instead. (in 24 steps) */
@keyframes cd-sprite {
  0% {
    background-position: 0% 0%;
  }
  100% {
    background-position: 100% 0%;
  }
}

* {
  box-sizing: border-box;
}

.cd-transition-layer {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 30;
  height: 100%;
  width: 100%;
}

.cd-transition-layer .bg-layer {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 15;
  height: 100%;
  width: 100%;
  background: url("https://i.imgur.com/9uDdPAP.png") no-repeat;
  background-size: 2500% 100%;
  background-position: 4.16% 0%;
  transform: translateY(-50%) translateX(-50%);
  animation: cd-sprite 1s steps(24) infinite;
  animation-direction: alternate;
  animation-delay: 1s;
  animation-fill-mode: forwards;
  border: 36px solid red;
}

@keyframes cd-sprite {
  0% {
    background-position: 0% 0%;
  }
  100% {
    background-position: 100% 0%;
  }
}
<body>
  <div class='cd-transition-layer'>
    <div class="bg-layer"></div>
  </div>
</body>

  • Related