JavaScript 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.

animate functions.htm HTML (2,74 kByte) 31.03.2014 00:55
<!DOCTYPE html >
<html lang="en">
  <head><meta charset="utf-8" />
    <title>Simple Animate Example</title>
    <script type="text/javascript">
      /* <![CDATA[ */
window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       ||
          window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame    ||
          function(callback){
            window.setTimeout(callback, 1000 / 60);
          };
})();
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 animate(id, attr, dest, callback) {
  var from, attribute = attr, to = dest, fc = callback, el = document.getElementById(id);
  if (attribute == 'opacity') {
    from = parseInt(getOpacity(el, (to == '0' ? '1' : '0')));
  } else {
    if (el.style.getPropertyValue) {
      from = parseInt(el.style.getPropertyValue(attribute));
    } else {
      from = parseInt(el.style.getAttribute(attribute));
    }
  };
  if (from == to) return;
  var start = Date.now();
  var output = document.getElementById('output');
  var tick = function() {
    var t = Math.min(1, ((Date.now() - start) / 1000));
    var eased = t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1;
    var val = (eased * (to - from)) + from;
    if (attribute == 'opacity') {
      setOpacity(el, val);
    } else {
      if (el.style.setProperty) {
        el.style.setProperty(attribute, val + "px");
      } else {
        el.style.setAttribute(attribute, val + "px");
      }
    };
    if (t<1) {
      window.requestAnimFrame(tick);
    } else {
      if (fc) fc();
    }
  };
  tick();
}
/* ]]> */
    </script>
  </head>
  <body>
    <button onclick="animate('el', 'opacity', '1');">fade in</button>
    <button onclick="animate('el', 'opacity', '0');">fade out</button>
    <button onclick="animate('el', 'left', '0');">move left</button>
    <button onclick="animate('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>

Autor: , veröffentlicht: , letzte Änderung:

Kontakt

Copyright / License of sources

Copyright (c) 2007-2018, Udo Schmal <udo.schmal@t-online.de>

Permission to use, copy, modify, and/or distribute the software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Service Infos

CMS Info Product Name:
UDOs Webserver
Version:
0.4.2.1
Description:
All in one Webserver
Copyright:
Udo Schmal
Compilation:
So, 21 Okt 2018 21:59:50
Development Info IDE:
Lazarus LCL 2.1.0.0
Compiler:
Free Pascal FPC 3.3.1
compiled for:
OS:Linux, CPU:x86_64
Hardware Info Model:
ASRock A330ION
CPU Name:
Intel(R) Atom(TM) CPU 330 @ 1.60GHz
CPU Type:
x86_64, 1 physical CPU(s), 2 Core(s), 4 logical CPU(s), 1599.996 MHz
System Info OS:
Ubuntu 18.04.1 LTS bionic