Advertisement
Guest User

Untitled

a guest
Jun 1st, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Flashcard Logic */
  2. $(document).ready(function($){ 
  3.  
  4.     var cardNumber = 1;
  5.    
  6.     // Flip the card when clicked
  7.     $('.flashcard-container').on('click', '.flashcard', function(){
  8.  
  9.         if($(this).hasClass('flipped'))
  10.         {
  11.             $(this).removeClass('flipped');
  12.         }
  13.         else
  14.         {
  15.             $(this).addClass('flipped');
  16.         }
  17.     });
  18.    
  19.     // The right arrow has been clicked to show the next card
  20.     $('.arrow.right').click(function(){
  21.         // Make sure that the left arrow is showing now that we are no longer on the first card
  22.         if($('.arrow.left:not(:visible)'))
  23.         {
  24.             $('.arrow.left').css('visibility', 'visible');
  25.         }
  26.        
  27.         // If we're at the end then hide the right arrow
  28.         if($('.flashcard:visible').next().next().length == 0)
  29.         {
  30.             $(this).css('visibility', 'hidden');
  31.         }
  32.        
  33.         //
  34.         $('.flashcard:visible').next().removeClass('hidden');
  35.         //
  36.         $('.flashcard:visible').prev().addClass('hidden').removeClass('flipped');;
  37.        
  38.         // Update the flashcard counter
  39.         cardNumber++;
  40.         $('#flashCount').text( cardNumber + ' / ' + kanaCount);
  41.    
  42.     });
  43.    
  44.     // The left arrow has been clicked to show the previous card
  45.     $('.arrow.left').click(function(){
  46.         // Make sure that the right arrow is showing now that we are no longer on the last card
  47.         if($('.arrow.right:not(:visible)'))
  48.         {
  49.             $('.arrow.right').css('visibility', 'visible');
  50.         }  
  51.        
  52.         // If we're back at the beginning then hide the left arrow
  53.         if($('.flashcard:visible').prev().prev().length == 0)
  54.         {
  55.             $(this).css('visibility', 'hidden');
  56.         }
  57.        
  58.         // Show the previous card
  59.         $('.flashcard:visible').prev().removeClass('hidden');
  60.         // Hide the next card
  61.         $('.flashcard:visible').next().addClass('hidden').removeClass('flipped');
  62.        
  63.         // Update the flashcard counter
  64.         cardNumber--;
  65.         $('#flashCount').text( cardNumber + ' / ' + kanaCount);
  66.    
  67.     });
  68.    
  69.     // Shuffle the deck of cards
  70.     $('#shuffle').click(function(){
  71.    
  72.         // Reset the card counter
  73.         cardNumber = 1;
  74.         $('#flashCount').text( cardNumber + ' / ' + kanaCount);
  75.         // Hide the left arrow
  76.         $('.arrow.left').css('visibility', 'hidden');
  77.         // Show the right arrow
  78.         $('.arrow.right').css('visibility', 'visible');
  79.         // Hide the visible flashcard
  80.         $('.flashcard:visible').addClass('hidden');
  81.         // Shuffle the deck
  82.         $('.flashcard').shuffle();
  83.         $('.flashcard:first').removeClass('hidden');
  84.    
  85.     });
  86.    
  87.     // Shuffle the deck of flashcards
  88.     (function($){
  89.      
  90.         $.fn.shuffle = function() {
  91.      
  92.             var allElems = this.get(),
  93.                 getRandom = function(max) {
  94.                     return Math.floor(Math.random() * max);
  95.                 },
  96.                 shuffled = $.map(allElems, function(){
  97.                     var random = getRandom(allElems.length),
  98.                         randEl = $(allElems[random]).clone(true)[0];
  99.                     allElems.splice(random, 1);
  100.                     return randEl;
  101.                });
  102.      
  103.             this.each(function(i){
  104.                 $(this).replaceWith($(shuffled[i]));
  105.             });
  106.      
  107.             return $(shuffled);
  108.      
  109.         };
  110.      
  111.     })(jQuery);
  112.    
  113.    
  114. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement