0.1.2 • Published 4 years ago

card-changer v0.1.2

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

Card-changer

Cards stack UI lib. No dependencies.

Check out a live demo

Preview

Usage

Install lib:

npm install card-changer

Make root container for card-changer:

<div id="root"></div>

Init card-changer:

import CardChanger from "card-changer";

const cardChanger = new CardChanger(document.querySelector("#root"), {
  cards: [
    {
      id: "card1",
      frontContent: "...", // HTML string or element with front card content
      backContent: "...", // HTML string or element with back card content
    },
    {
      id: "card2",
      frontContent: "...",
      backContent: "...",
    },
    // ...
  ],
  activeCardId: "card1",
  stackSize: 3,
  stackHeight: 40,
});

// Set active card
cardChanger.activeCardId = "card2";

// Rotate current card
cardChanger.rotate();

// Add handler on active card change
cardChanger.on("change", (activeCardId) => {
  console.log(`Active card id is "${activeCardId}"`);
});

API

Options

Option fields

FieldTypeDescription
stackSizeNumberAmount of visible cards in stack
stackHeightNumberHeight of back cards area
rootClassNameStringRoot class name for all generated sub-elements
dotsNavigationBooleanShow dots navigation
keepChangeOrderBooleanCards change sequentially with animations for each
animationSpeedNumberAnimation speed for change one card (should be the same as CSS animation duration)
cardsArrayArray with cards object (see Card fields)
activeCardIdStringCard id that should be on top of the stack
unRotateOnChangeBooleanRotate to front card when it becomes inactive

Card fields

FieldTypeDescription
idStringcard unique ID string
frontContentString or HTMLElementHTML content with the front side of the card
backContentString or HTMLElementHTML content with the back side of the card

Methods

  • rotate() - rotate active card
  • on(event, handler) - add handler for card-changer event
  • off(event, handler) - remove handler for card-changer event

To set an active card id - assign it to activeCardId field:

cardChanger.activeCardId = "new_id";

Events

FieldParamsDescription
changeactiveCardIdfires on active card change

Examples

Check out an example code here

License

MIT