Emulating CSS Timing Functions

In this example below you will see how to do a Emulating CSS Timing Functions with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by DanaChen, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright DanaChen ©
  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Emulating CSS Timing Functions</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/DanaChen/emulating-css-timing-functions-mqBXrv */
body {
  background: linear-gradient(90deg, #ff9800 var(--stop, 0%), #3c3c3c 0);
}
body:after {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  font: calc(16vmin + 3rem)/100vh Leckerli One, cursive;
  text-align: center;
  cursor: pointer;
  content: 'Dana';
}
@supports (mix-blend-mode: screen) {
  body:after {
    background: inherit;
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    filter: invert(1) grayscale(1) contrast(3);
  }
}


/*Downloaded from https://www.codeseek.co/DanaChen/emulating-css-timing-functions-mqBXrv */
var NF = 20;

var rID = null,
    f = 0,
    dir = -1;

function stopAni() {
  cancelAnimationFrame(rID);
  rID = null;
};

function update() {
  f += dir;

  var k = f / NF;

  document.body.style.setProperty('--stop', +(k * 100).toFixed(2) + '%');

  if (!(f % NF)) {
    stopAni();
    return;
  }

  rID = requestAnimationFrame(update);
};

addEventListener('click', function (e) {
  if (rID) stopAni();
  dir *= -1;
  update();
}, false);

Comments