Advertisement
Guest User

slideshowQuinten

a guest
Oct 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 2.02 KB | None | 0 0
  1. ;(function($) {
  2.     'use strict';
  3.     var $currNr = 0;
  4.     var $links, $images;
  5.  
  6.     /**
  7.     * Function to prevent closures; adds click event handler to links
  8.     */
  9.     var addEvents = function(nr){
  10.         $links.eq(nr).on("click",function(e){
  11.             showImage(nr);
  12.             e.preventDefault();
  13.         });
  14.     }
  15.  
  16.     /**
  17.     * Function to show an image
  18.     */
  19.     var showImage = function(nr){
  20.         //find image
  21.         var $img = $images.get(nr);
  22.         if(!$img) return;
  23.  
  24.         //find big image
  25.         var $photoBig = $('#photoBig');
  26.  
  27.         //change
  28.         $photoBig.attr('src') = $img.attr('data-src-l');
  29.         $photoBig.attr('alt') = $img.attr('alt');
  30.  
  31.         //remember current image number
  32.         $currNr = nr;
  33.     }
  34.  
  35.     /**
  36.     * Function to show the next image
  37.     */
  38.     var showNextImage = function(){
  39.         showImage($currNr + 1);
  40.     };
  41.  
  42.     /**
  43.     * Function to show the previous image
  44.     */
  45.     var showPrevImage = function(){
  46.         showImage($currNr - 1);
  47.     }
  48.  
  49.     //start scripts
  50.     $(document).ready(function(){
  51.         //find images and links
  52.         $links = $("#thumbsmenu li>a");
  53.         $images = $("#thumsmenu li>a img");
  54.  
  55.         //add links events
  56.         for(var i = 0; i < $links.length; i++){
  57.             addEvents(i);
  58.         }
  59.  
  60.         // controls
  61.         $('#lnkFirst').on("click",function(e){
  62.             showImage(0);
  63.             e.preventDefault();
  64.         });
  65.         $('lnkPrev').on("click",function(e){
  66.             showPrevImage();
  67.             e.preventDefault();
  68.         });
  69.         $('#lnkNext').on("click",function(e){
  70.             showNextImage();
  71.             e.preventDefault();
  72.         });
  73.         $('#lnkLast').on("click",function(e){
  74.             showImage($images.length - 1);
  75.             e.preventDefault();
  76.         })
  77.  
  78.         // keyboard
  79.         $(document).keydown(function(e){
  80.             var $code = (e.keyCode ? e.keyCode : e.which);
  81.             switch($code){
  82.                 case 37: showPrevImage(); e.preventDefault(); break;
  83.                 case 39: showNextImage(); e.preventDefault(); break;
  84.             }
  85.         });
  86.  
  87.         // play
  88.         var $timer
  89.         $('#btnPlay').on("click",function(e){
  90.             if(this.value == 'start'){
  91.                 this.value = 'stop';
  92.                 $currNr = 0;
  93.                 $timer = setInterval(showNextImage, 1000);
  94.             } else {
  95.                 this.value = 'start';
  96.                 clearInterval($timer);
  97.             }
  98.         });
  99.  
  100.     });
  101.  
  102. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement