Advertisement
sazid_iiuc

Untitled

Apr 5th, 2021
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   @method Cards
  3.   @description this is for label based radio navigation to change the labels style to 'active'
  4.  */
  5. const Cards = ((() => {
  6.   /*
  7.    * @description dom loaded event listener
  8.    */
  9.   window.addEventListener('DOMContentLoaded', () => {setTimeout(init,1)}, true);
  10.  
  11.   /*
  12.    * @method init
  13.    * @parameter e {event}
  14.    * @description initiates event listeners on all cards
  15.    */
  16.   function init(e)
  17.   {
  18.     if(document.querySelector(".cards"))
  19.     {
  20.       let cards = document.querySelector(".cards");
  21.       cards.addEventListener('click', clicked, false);
  22.       document.querySelectorAll(".cards .card")[1].click();
  23.     }
  24.   }
  25.  
  26.   /*
  27.    * @method clicked
  28.    * @parameter e {event}
  29.    * @description this is the callback from the assigned event listener binding
  30.    */
  31.   function clicked(e)
  32.   {
  33.     let card = e.target;
  34.     if(card.getAttribute("data-card")){rearrange(card.getAttribute("data-card"));}
  35.   }
  36.  
  37.   /*
  38.    * @method rearrange
  39.    * @parameter card {object}
  40.    * @description this is the callback from the assigned event listener binding
  41.    */
  42.   function rearrange(card)
  43.   {
  44.     let cards = document.querySelectorAll(".cards .card");
  45.     for(let n = 0; n < cards.length; n++)
  46.     {
  47.       cards[n].classList.remove("card--left");
  48.       cards[n].classList.remove("card--center");
  49.       cards[n].classList.remove("card--right");
  50.     }
  51.     cards[card].classList.add("card--center");
  52.     if(card == 0)
  53.     {
  54.       cards[2].classList.add("card--left");
  55.       cards[1].classList.add("card--right");
  56.     }
  57.     if(card == 1)
  58.     {
  59.       cards[0].classList.add("card--left");
  60.       cards[2].classList.add("card--right");
  61.     }
  62.     if(card == 2)
  63.     {
  64.       cards[1].classList.add("card--left");
  65.       cards[0].classList.add("card--right");
  66.     }
  67.   }
  68.  
  69.   return {
  70.     init
  71.   }
  72. })());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement