JavaScript Accordion

Beispiel: ohne großes Framework
HTML:
accordion.htm HTML (1,48 kByte) 12.03.2016 16:07
<!DOCTYPE html >
<html lang="en">
  <head><meta charset="utf-8" />
    <title>Accordion</title>
    <script type="text/javascript" src="jsAnimatedAccordion.js"></script>
<link rel="stylesheet" type="text/css" href="accordion.css" /></head>
  <body>
    <ul class="accordion">
      <li class="sheet-current">
        <h4 class="sheet-header">Udo</h4>
        <div class="imageLeft"><img border="0" height="115" width="100" src="/media/udo.jpg" alt="Udo Schmal" title="Udo Schmal" /></div>
        <h5>Udo Schmal</h5>
        <p><strong>Softwareentwicklung</strong></p>
        <table summary="Kontakt" class="noBorder">
          <tbody>
            <tr>
              <td>E-Mail:</td>
              <td><a href="mailto:udo.schmal@t-online.de">udo.schmal@t-online.de</a></td>
            </tr>
          </tbody>
        </table>
      </li>
      <li class="sheet">
        <h4 class="sheet-header">Claudia</h4>
        <div class="imageLeft"><img border="0" height="115" width="100" src="/media/claudia.jpg" alt="Claudia Schmal-Prangs" title="Claudia Schmal-Prangs" /></div>
        <h5>Claudia Schmal-Prangs</h5>
        <table summary="Kontakt" class="noBorder">
          <tbody>
            <tr>
              <td>E-Mail:</td>
              <td><a href="mailto:claudia.prangs@t-online.de">claudia.prangs@t-online.de</a></td>
            </tr>
          </tbody>
        </table>
      </li>
    </ul>
  </body>
</html>
JavaScript:
jsAccordion.js JavaScript (2,13 kByte) 06.12.2013 21:59
function getElementsByClassName(searchClass, tagName, domNode) {
  if (domNode == null) domNode = document;
  if (tagName == null) tagName = '*';
  var el = new Array();
  var tags = domNode.getElementsByTagName(tagName);
  var tcl = " "+searchClass+" ";
  for(var i=0,j=0; i<tags.length; i++) {
    var test = " " + tags[i].className + " ";
    if (test.indexOf(tcl) != -1) {
      el[j++] = tags[i];
    }
  };
  return el;
}

function getChildNodes(parentElement, tagName) {
  var childs = parentElement.childNodes;
  var children = [];
  for (var i=0; i<childs.length; i++) {
    if (((childs[i].nodeType == 1) ? childs[i].tagName.toLowerCase() : '') == tagName) {
      children.push(childs[i]);
    }
  };
  return children;
}

function jsAccordion(){
  this.sheets = [];
  this.currentTab = 0;
}
jsAccordion.prototype = {
  selectTab: function(idx){
    this.sheets[this.currentTab].className = "sheet";
    this.currentTab = idx;
    this.sheets[idx].className = "sheet-current";
  },
  init: function(accordion){
    var self = this;
    this.sheets = getChildNodes(accordion, "li");
    for (var i=0; i<this.sheets.length; i++) {
      if (this.sheets[i].className=="sheet current") this.currentTab = i;
      this.sheets[i].className = "sheet";
      this.sheets[i].sheetid = i;
      this.sheets[i].onmousedown = function(ev) {
        self.selectTab(this.sheetid);
        if ((ev) && (ev.stopPropagation)){
          ev.stopPropagation();
        } else if (window.event){
          window.event.cancelBubble = true;
        }
      };
    };
    this.selectTab(this.currentTab);    
  }
}
function initAccordions() {
  var accordionList = getElementsByClassName("accordion", "ul");
  for (var i=0; i<accordionList.length; i++) {
    var accordion = new jsAccordion();
    accordion.init(accordionList[i]);     
  };
}

if (window.addEventListener) {
  window.addEventListener('load', function(event) {initAccordions();});
} else if (window.attachEvent) {
  window.attachEvent('onload', function(event) {initAccordions();});
} else {
  window['onload'] = function(event) {initAccordions();};
}
jsAnimatedAccordion.js JavaScript (3,96 kByte) 14.09.2014 19:45
window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       ||
          window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame    ||
          function(callback){
            window.setTimeout(callback, 1000 / 60);
          };
})();

if (!Date.now) {
  Date.now = function now() {
    return new Date().getTime();
  };
}

function animate(elem, attr, dest, callback) {
  var from, to = dest, el = elem, fc = callback;
  if (el.style.getPropertyValue) {
    from = parseInt(el.style.getPropertyValue(attr));
  } else {
    from = parseInt(el.style.getAttribute(attr));
  };
  if (from == to) return;
  var start = Date.now();
  var tick = function() {
    var t = Math.min(1, ((Date.now() - start) / 500));
    var eased =  t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1;
    var val = (eased * (to - from)) + from;
    if (el.style.setProperty) {
      el.style.setProperty(attr, val + "px");
    } else {
      el.style.setAttribute(attr, val + "px");
    };
    if (t<1) { 
      window.requestAnimFrame(tick);
    } else {
      if (fc) fc();
    }
  };
  tick();
}

function getElementsByClassName(searchClass, tagName, domNode) {
  if (domNode == null) domNode = document;
  if (tagName == null) tagName = '*';
  var el = new Array();
  var tags = domNode.getElementsByTagName(tagName);
  var tcl = " "+searchClass+" ";
  for(var i=0,j=0; i<tags.length; i++) {
    var test = " " + tags[i].className + " ";
    if (test.indexOf(tcl) != -1) {
      el[j++] = tags[i];
    }
  };
  return el;
}

function getChildNodes(parentElement, tagName) {
  var childs = parentElement.childNodes;
  var children = [];
  for (var i=0; i<childs.length; i++) {
    if (((childs[i].nodeType == 1) ? childs[i].tagName.toLowerCase() : '') == tagName) {
      children.push(childs[i]);
    }
  };
  return children;
}

function jsAccordion(){
  this.sheets = [];
  this.currentTab = -1;
  this.initialHeight = 0;
}
jsAccordion.prototype = {
  selectTab: function(idx){
    if (this.currentTab != idx) { 
      if (this.currentTab>-1) {
        var oldOne = this.sheets[this.currentTab];
        var fcOld = function() {oldOne.style.height = '';};
        oldOne.style.height = parseInt(oldOne.scrollHeight) + 'px';
        oldOne.className = "sheet";
        oldOne.style.overflow = 'hidden';
        animate(oldOne, 'height', this.initialHeight, fcOld);
      };
      if (idx>-1) {
        this.currentTab = idx;
        var newOne = this.sheets[idx];
        var fcNew = function() {newOne.className = "sheet-current"; newOne.style.height = '';};
        if (this.initialHeight == 0) { 
          this.initialHeight = newOne.clientHeight;
        }
        newOne.style.height = this.initialHeight + 'px';
        animate(newOne, 'height', parseInt(newOne.scrollHeight), fcNew);
      }
    }
  },
  init: function(accordion){
    var self = this, idx;
    this.sheets = getChildNodes(accordion, "li");
    for (var i=0; i<this.sheets.length; i++) {
      if (this.sheets[i].className=="sheet-current") idx = i;
      this.sheets[i].className = "sheet";
      this.sheets[i].sheetid = i;
      this.sheets[i].onmousedown = function(ev) {
        self.selectTab(this.sheetid);
        if ((ev) && (ev.stopPropagation)){
          ev.stopPropagation();
        } else if (window.event){
          window.event.cancelBubble = true;
        }
      };
    };
    this.selectTab(idx);    
  }
}
function initAccordions() {
  var accordionList = getElementsByClassName("accordion", "ul");
  for (var i=0; i<accordionList.length; i++) {
    var accordion = new jsAccordion();
    accordion.init(accordionList[i]);     
  };
}

if (window.addEventListener) {
  window.addEventListener('load', function(event) {initAccordions();});
} else if (window.attachEvent) {
  window.attachEvent('onload', function(event) {initAccordions();});
} else {
  window['onload'] = function(event) {initAccordions();};
}
StyleSheet:
accordion.css StyleSheet (1,42 kByte) 06.12.2013 22:04
ul.accordion {
    position: relative;
    margin: 0;
    padding: 5px;
    list-style: none;
    list-style-image: none;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
}
ul.accordion .sheet, ul.accordion .sheet-current {
    position: relative;
    margin: 0 0 1px 0;
    padding: 7px 10px 1px 10px;
    border: 1px solid #C8CED0;
    background-color: #FFFFFF;
    background-image: -moz-linear-gradient(top, #fcfcfc 91%, #ededed 100%);
    -moz-border-radius: 4px;
    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(91%, #fcfcfc), to(#ededed));
    background-image: -webkit-linear-gradient(top, #fcfcfc 91%, #ededed 100%);
    -webkit-border-radius: 4px;
    background-image: -o-linear-gradient(top, #fcfcfc 91%, #ededed 100%);
    -o-border-radius: 4px;
    background-image: -ms-linear-gradient(top, #fcfcfc 91%, #ededed 100%);
    -ms-border-radius: 4px;
    background-image: linear-gradient(to bottom, #fcfcfc 91%, #ededed 100%);
    border-radius: 4px;
}
ul.accordion .sheet .sheet-header, ul.accordion .sheet-current .sheet-header {
    margin: 0 0 5px 0;
    padding: 0;
    color: #626B71;
    height: 18px;
    font-weight: bold;
    font-size: 13px;
    cursor: pointer;
}
ul.accordion .sheet {
    height: 18px;
    overflow: hidden;
}
ul.accordion .sheet:hover {
    background: #EFF1F1;
}
ul.accordion .current, ul.accordion .sheet-current {
    height: auto;
    padding-bottom: 7px;
}

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

Kontakt

Copyright / License of sources

Copyright (c) 2007-2017, 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
UDOs Webserver

0.3.1.24

All in one Webserver

Udo Schmal

Sa, 21 Okt 2017 00:30:10
Development Info
Lazarus LCL 1.9.0.0

Free Pascal FPC 3.1.1

OS:Win64, CPU:x86_64
Hardware Info
Precision WorkStation T3500

Intel(R) Xeon(R) CPU W3530 @ 2.80GHz

x86_64, 1 physical CPU(s), 4 Core(s), 8 logical CPU(s), 2800 MHz