JavaScript CSS Animate

Sicherlich gibt es einige fertige Routinen um eine Animation (slide, resize, ...)  zu erzielen, z.B. jQuery  .animate() aber manchmal reicht auch die kleine Lösung, hier nun die Variante mit CSS Transitions.

cssAnimate.htm HTML (2,31 kByte) 21.02.2021 00:12
<!DOCTYPE html >
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Simple CSS Transition Example</title>
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: " />
    <script type="text/javascript">
      /* <![CDATA[ */
function getOpacity(el, def) {
  var result;
  var style=document.defaultView.getComputedStyle(el, null);
  if ('opacity' in style) {
    result = style.opacity;
  } else if ('MozOpacity' in style) {
    result = style.MozOpacity;
  } else if ('filter' in style) {
    var match = style.filter.match(/alpha\(opacity=([\d.]+)\)/);
    if (match) {
      result = String(match[1] / 100);
    }
  }
  result = parseInt(result);
  if (!isNaN(result)) result = def;
  return result;
}

function setOpacity(el, val) {
  el.style.opacity = val;
  el.style.MozOpacity = val;
  el.style.filter = "alpha(opacity=" + (val*100) + ")";
}
function cssAnimate(obj, name, value) {
  var el = document.getElementById(obj), attr = name, to = value;
  el.style.WebkitTransition = attr + " 0.5s ease";
  el.style.MozTransition = attr + " 0.5s ease";
  el.style.MsTransition = attr + " 0.5s ease";
  el.style.OTransition = attr + " 0.5s ease";
  el.style.transition = attr + " 0.5s ease";
  if (name == 'opacity') {
    setOpacity(el, to);
  } else {
    el.style.setProperty(attr, to + "px");
  }
  var transitionend = function () {
    el.style.WebkitTransition = "none";
    el.style.MozTransition = "none";
    el.style.MsTransition = "none";
    el.style.OTransition = "none";
    el.style.transition = "none";
  };
  setTimeout(function(){ transitionend(); }, 500);
}
/* ]]> */
    </script>
  </head>
  <body><button onclick="cssAnimate('el', 'opacity', '1');">fade in</button><button onclick="cssAnimate('el', 'opacity', '0');">fade out</button><button onclick="cssAnimate('el', 'left', '0');">move left</button><button onclick="cssAnimate('el', 'left', document.getElementById('wrapper').offsetWidth-100);">move right</button>
    <div id="wrapper" style="position: relative; width:100%;">
      <div id="el" style="position:absolute; left:150px; top:0; display:block; background-color:silver; border:1px solid red; width:100px; height:100px;"></div>
    </div>
  </body>
</html>

Author: , published: , last modified:

Kontakt

Udo Schmal

Udo Schmal
Softwareentwickler
Olvengraben 41
47608 Geldern
Nordrhein-Westfalen
Germany





+49 2831 9776557
+49 1575 0663676
+49 2831 1328709
SMS
WhatsApp

Instagram Profile
vCard 3.0

Service Infos

CMS Info Product Name:
UDOs Webserver
Version:
0.5.0.101
Description:
All in one Webserver
Copyright:
Udo Schmal
Compilation:
Sun, 18. Jul 2021 17:42:24
Development Info Compiler:
Free Pascal FPC 3.3.1
compiled for:
OS:Linux, CPU:x86_64
System Info OS:
Ubuntu 20.04.2 LTS (Focal Fossa)
Hardware Info Model:
Hewlett-Packard HP Pavilion dv7 Notebook PC
CPU Name:
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
CPU Type:
x86_64, 1 physical CPU(s), 2 Core(s), 4 logical CPU(s), 1121.995 MHz