x-deck
<x-deck>
s are components that act similarly to a deck of cards, where any <x-card>
element can be moved into view independent of DOM order
Basic usage
For basic transitions between <x-card>
s, simply call the .nextCard()
and .previousCard()
methods of the <x-deck>
.
You can also call .showCard(index)
if they have a specific index or card element you wish to show (by passing a reference).
The loop
attribute option allows the deck to loop around after reaching the first or last index.
By default, there is no animation applied to card switching, but the transition-type
attribute lets you apply a variety of animations if you choose - more on that later
Markup
JavaScript
// assume prevButton, nextButton, toButton, and deck
//are already defined as their respective DOM elements
prevButton.addEventListener("click", function(){
deck.previousCard();
});
nextButton.addEventListener("click", function(){
deck.nextCard();
});
toButton.addEventListener("click", function(){
deck.showCard(1);
});
Specifying animation direction
The .previousCard()
, .nextCard()
, and .showCard()
methods can also be called with an optional direction parameter of either "forward"
or "reverse"
to specify how animations should progress.
If this parameter is omitted, the animation direction defaults to "forward"
if the new index is greater than the previously selected index, and "reverse"
if less than the previously selected index.
Automatic animation direction
Reverse animation direction
Forward animation direction
Markup
Specifying deck transitions: transition-type/.transitionType
By default, there is no transition animation applied when switching between <x-card>
elements, but we provide a batch of ready-made transitions you can apply using the transition-type
attribute (also accessible by the .transitionType
property).
Valid options:
slide-up,
slide-down,
slide-left,
slide-right,
fade-scale
Markup
JavaScript
// assume transitionButton and deck are already
// defined as their respective DOM elements
var types = [
"slide-up", "slide-down", "slide-left", "slide-right",
"fade-scale",
"none"
];
transitionButton.addEventListener("click", function(){
var oldType = deck.transitionType;
deck.transitionType = types[(types.indexOf(oldType)+1) % type.length];
// force animation to make visible for demo
deck.nextCard("forward");
});
Specifying card-specific animations: transition-type/.transitionType
While the <x-deck>
's transition-type
attribute changes the animation of all cards, it is also possible to specify how a specific <x-card>
animates by specifying a transition-type
attribute on individual <x-card>
elements
Markup
Adding and removing cards
To add or remove <x-card>
s from a deck, simply use .appendChild()
or .removeChild()
in the same way that any other DOM node would be added/removed.
Markup
JavaScript
// assume addButton, removeButton, and deck
//are already defined as their respective DOM elements
// also assume that .randomColor simply returns a random color rgb string
addButton.addEventListener("click", function(){
// deck.numCards retrieves the number of cards currently in the deck
var newIndex = deck.numCards;
var newCard = document.createElement("x-card");
newCard.style.backgroundColor = randomColor();
newCard.textContent = newIndex;
deck.appendChild(newCard);
// for demo, shuffle to newly inserted card
deck.showCard(newIndex);
});
removeButton.addEventListener("click", function(){
if(deck.numCards > 0){
//deck.getCardAt retrieves the <x-card> at the given index
var lastCard = deck.getCardAt(deck.numCards-1);
deck.removeChild(lastCard);
}
});
Styling deck appearances
Styling the <x-deck>
and <x-card>
elements can be done like any other DOM elements. (By default, the deck attempts to be 100% width and height of its parent element, but this can be overridden.)
However, the deck also provides some CSS selectors to customize how cards appear during animations.
To style how a card appears as it is transitioning out of the deck, apply styles to x-card[hide]
To style how a card appears when it is specifically selected in the deck, apply styles to x-card[selected]
Markup
Styling
/* grey out hiding card */
#custom-styles x-card[hide]{
text-shadow: none;
background-color: grey!important;
color: #ccc;
opacity: 0.6;
}
/* add caption to selected card */
#custom-styles x-card[selected]:after{
content: "selected";
display: block;
font-size: .2em;
}
Handling card switching events: show
, hide
When the deck is transitioning cards, it fires events in order to signal different stages of the animation.
An show
event is fired from a card target after it has completed its show animation, and the show state has been finalized.
An hide
event is fired from a card target after it has completed its hide animation, and the hide state has been finalized.