JavaScript Fade

Sicherlich gibt es einige fertige Routinen um einen Fade Effekt zu erzielen, z.B. jQuery .fadeIn() und .fadeOut(), aber manchmal reicht auch die kleine Lösung.

HTML

fade.htm HTML (324 bytes) 27.10.2021 22:44
<!DOCTYPE html >
<html lang="de">
  <head>
    <meta charset="utf-8" />
    <title>fade</title>
  </head>
  <body><button id="fadeIn">in</button><button id="fadeOut">out</button>
    <div id="wrapper">
      <div id="el"></div>
    </div>
    <script src="fade_object.js"></script>
  </body>
</html>

JavaScript

fade functions.js JavaScript (1,77 kByte) 27.10.2021 22:46
// coding: utf-8
/** Created by: Udo Schmal | https://www.gocher.me/ */
(function () {
  function getOpacity(el, def) {
    var result;
    var style=document.defaultView.getComputedStyle(el, null);
    if ('opacity' in style) {
      result = style.opacity;
    }
    result = parseInt(result);
    if (!isNaN(result)) result = def;
    return result;
  }

  function setOpacity(el, val) {
    el.style.opacity = val;
  }

  function fadeIn(el) {
    var opacity = getOpacity(el, 0);
    var last = new Date();
    var tick = function() {
      opacity += (new Date() - last) / 400;
      setOpacity(el, (opacity>1 ? 1 : opacity));
      last = new Date();
      if (opacity < 1) { window.requestAnimationFrame(tick); }
    };
    tick();
  }

  function fadeOut(el) {
    var opacity = getOpacity(el, 1);
    var last = new Date();
    var tick = function() {
      opacity -= (new Date() - last) / 400;
      setOpacity(el, (opacity<0 ? 0 : opacity));
      last = new Date();
      if (opacity > 0) { window.requestAnimationFrame(tick); }
    };
    tick();
  }
  var el = document.getElementById('el'),
  wrapper = document.getElementById('wrapper');
  wrapper.style.position = 'relative';
  wrapper.style.height = '105px';
  wrapper.style.width = '100%';
  el.style.display = 'block';
  el.style.backgroundColor = 'silver';
  el.style.border = '1px solid red';
  el.style.width = '100px';
  el.style.height = '100px';
  var btnFadeIn = document.getElementById('fadeIn');
  if (btnFadeIn) {
    btnFadeIn.addEventListener('click', function () {
      fadeIn(el);
    });
  }
  var btnFadeOut = document.getElementById('fadeOut');
  if (btnFadeOut) {
    btnFadeOut.addEventListener('click', function () {
      fadeOut(el);
    });
  }
})();
fade object.js JavaScript (2,53 kByte) 27.10.2021 22:46
// coding: utf-8
/** Created by: Udo Schmal | https://www.gocher.me/ */
(function () {
  if (!window.Element ) {
    Element = function(){};
    var __createElement = document.createElement;
    document.createElement = function(tagName) {
      var element = __createElement(tagName);
      if (element == null) {return null;}
      for (var key in Element.prototype)
        element[key] = Element.prototype[key];
      return element;
    }
    var __getElementById = document.getElementById;
    document.getElementById = function(id) {
      var element = __getElementById(id);
      if (element == null) {return null;}
      for (var key in Element.prototype)
        element[key] = Element.prototype[key];
      return element;
    }
  }
  Element.prototype.getOpacity = function(def) {
    var result;
    var style = document.defaultView.getComputedStyle(this, null);
    if ('opacity' in style) {
      result = style.opacity;
    }
    result = parseInt(result);
    if (!isNaN(result)) result = def;
    return result;
  };
  Element.prototype.setOpacity = function(val) {
    this.style.opacity = val;
  };
  Element.prototype.fadeIn = function() {
    var self = this;
    var opacity = this.getOpacity(0);
    var last = new Date();
    var tick = function() {
      opacity += (new Date() - last) / 400;
      self.setOpacity(opacity>1 ? 1 : opacity);
      last = new Date();
      if (opacity < 1) { window.requestAnimationFrame(tick); }
    };
    tick();
  };
  Element.prototype.fadeOut = function() {
    var self = this;
    var opacity = this.getOpacity(1);
    var last = new Date();
    var tick = function() {
      opacity -= (new Date() - last) / 400;
      self.setOpacity(opacity<0 ? 0 : opacity);
      last = new Date();
      if (opacity > 0) { window.requestAnimationFrame(tick); }
    };
    tick();
  };

  var el = document.getElementById('el'),
  wrapper = document.getElementById('wrapper');
  wrapper.style.position = 'relative';
  wrapper.style.height = '105px';
  wrapper.style.width = '100%';
  el.style.display = 'block';
  el.style.backgroundColor = 'silver';
  el.style.border = '1px solid red';
  el.style.width = '100px';
  el.style.height = '100px';
  var fadeIn = document.getElementById('fadeIn');
  if (fadeIn) {
    fadeIn.addEventListener('click', function () {
      el.fadeIn();
    });
  }
  var fadeOut = document.getElementById('fadeOut');
  if (fadeOut) {
    fadeOut.addEventListener('click', function () {
      el.fadeOut();
    });
  }
})();

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

Google Maps Profile
Instagram Profile
vCard 3.0

Service Infos

CMS Info

Product Name:
UDOs Webserver
Version:
0.5.0.167
Description:
All in one Webserver
Copyright:
Udo Schmal
Compilation:
Sat, 25. Jun 2022 14:46:24

Development Info

Compiler:
Free Pascal FPC 3.3.1
compiled for:
OS:Linux, CPU:x86_64

System Info

OS:
Ubuntu 20.04.4 LTS (Focal Fossa)

Hardware Info

Model:
Hewlett-Packard HP Pavilion dm4 Notebook PC
CPU Name:
Intel(R) Core(TM) i5-2430M CPU @ 2.40GHz
CPU Type:
x86_64, 1 physical CPU(s), 2 Core(s), 4 logical CPU(s), 800.000 MHz