1 /**2 * Card.js3 * Description: CardItem.4 * Author: gerardooscarjt@gmail.com5 * Tipical use:6 var card1 = new Card();7 // ... ///8 stack.push(card1);9 * Requisites:10 * Extended classes from this one, must implement 'free' method.11 */12 13 var Card = function() {14 this.dom = document.createElement('section');15 this.dom.setAttribute('b', 'Card');16 this.dom.classList.add('scroll');17 }18 19 Card.prototype.free = function() {20 //alert('Card.free() must be implemented.');21 }22 23 24 /**25 * StackCards.js26 * Description: Stack system with animation.27 * Author: gerardooscarjt@gmail.com28 * Tipical use:29 var frame = document.getElementById(...);30 var card1 = new Card();31 var card2 = new Card();32 33 var stack = new StackCards(div);34 stack.push(card1);35 stack.push(card2);36 37 stack.pop();38 stack.pop();39 * 40 */41 42 var StackCards = function(parent) {43 this.dom = parent;44 this.stack = ([]);45 46 this.dom.setAttribute('b', 'StackCards');47 }48 49 /**50 * Returns top card or null if stack is empty51 */52 StackCards.prototype.top = function() {53 return this.stack[this.stack.length-1];54 }55 56 /**57 * Push a card on top of the stack and perform animation by default58 * It is possible force animation.59 */60 StackCards.prototype.push = function(card, animation) {61 if (typeof animation == 'undefined')62 animation = true;63 64 if (this.stack.indexOf(card)<0) {65 var top = this.top();66 67 if (animation)68 card.dom.classList.add('Card-out');69 70 card.dom.setAttribute('b', 'Card');71 this.stack.push(card);72 73 card.dom.addEventListener('transitionend', function(e){74 if (top != null)75 top.dom.style.display = 'none';76 }, false);77 78 this.dom.appendChild(card.dom);79 card.dom.offsetTop; // Refresh render tree80 card.dom.classList.remove('Card-out');81 }82 }83 84 /**85 * Pop top card free its resources and return reference to it86 * TODO: write animation87 */88 StackCards.prototype.pop = function() {89 var card = this.stack.pop();90 if (card != null) {91 card.free();92 var top = this.top();93 if (top != null) {94 top.dom.style.display = '';95 }96 this.dom.removeChild(card.dom);97 return card;98 }99 return null;100 }101 102 /**103 * Empty stack104 */105 StackCards.prototype.clear = function() {106 while (this.pop() != null) {}107 }108
Este ShareCode tiene versiones:
- Cards stack Push card Pop c... (24/04/2013)
- Cards stack Push card Pop c... (24/04/2013)
- Cards stack Push card Pop c... (24/04/2013)
- Cards stack Push card Pop c... (24/04/2013)
Enlace
El enlace para compartir es: