jQuery Gallery

jQuery Gallery

Hi all,

Firstly I hope this is the right forum; if not, please redirect me!

I've recently started using a template for my photography portfolio that uses jQuery for the gallery. I have no jQuery knowledge, and the gallery is working fine, apart from one thing I'd like to change.

I'd like the gallery to change image every x number of seconds (i.e. a slideshow). I've had a look around and I can't find anywhere that really helps with that, so I'm hoping someone here would be kind enough to assist me.

I would attach the script but apparently I'm not allowed, so I'll put it below:

  1. //config
  2. //set default images view mode
  3. $defaultViewMode="full"; //full, normal, original
  4. $tsMargin=30; //first and last thumbnail margin (for better cursor interaction) 
  5. $scrollEasing=600; //scroll easing amount (0 for no easing) 
  6. $scrollEasingType="easeOutCirc"; //scroll easing type 
  7. $thumbnailsContainerOpacity=0.8; //thumbnails area default opacity
  8. $thumbnailsContainerMouseOutOpacity=0; //thumbnails area opacity on mouse out
  9. $thumbnailsOpacity=0.6; //thumbnails default opacity
  10. $nextPrevBtnsInitState="show"; //next/previous image buttons initial state ("hide" or "show")
  11. $keyboardNavigation="on"; //enable/disable keyboard navigation ("on" or "off")

  12. //cache vars
  13. $thumbnails_wrapper=$("#thumbnails_wrapper");
  14. $outer_container=$("#outer_container");
  15. $thumbScroller=$(".thumbScroller");
  16. $thumbScroller_container=$(".thumbScroller .container");
  17. $thumbScroller_content=$(".thumbScroller .content");
  18. $thumbScroller_thumb=$(".thumbScroller .thumb");
  19. $preloader=$("#preloader");
  20. $toolbar=$("#toolbar");
  21. $toolbar_a=$("#toolbar a");
  22. $bgimg=$("#bgimg");
  23. $img_title=$("#img_title");
  24. $nextImageBtn=$(".nextImageBtn");
  25. $prevImageBtn=$(".prevImageBtn");

  26. $(window).load(function() {
  27.     $toolbar.data("imageViewMode",$defaultViewMode); //default view mode
  28.     if($defaultViewMode=="full"){
  29.          $toolbar_a.html("<img src='images/toolbar_n_icon.png' width='50' height='50'  />").attr("onClick", "ImageViewMode('normal');return false").attr("title", "Restore");
  30.     } else {
  31.          $toolbar_a.html("<img src='images/toolbar_fs_icon.png' width='50' height='50'  />").attr("onClick", "ImageViewMode('full');return false").attr("title", "Maximize");
  32.     }
  33.     ShowHideNextPrev($nextPrevBtnsInitState);
  34.     //thumbnail scroller
  35.     $thumbScroller_container.css("marginLeft",$tsMargin+"px"); //add margin
  36.     sliderLeft=$thumbScroller_container.position().left;
  37.     sliderWidth=$outer_container.width();
  38.     $thumbScroller.css("width",sliderWidth);
  39.     var totalContent=0;
  40.     fadeSpeed=200;
  41.     
  42.     var $the_outer_container=document.getElementById("outer_container");
  43.     var $placement=findPos($the_outer_container);
  44.     
  45.     $thumbScroller_content.each(function () {
  46.         var $this=$(this);
  47.         totalContent+=$this.innerWidth();
  48.         $thumbScroller_container.css("width",totalContent);
  49.         $this.children().children().children(".thumb").fadeTo(fadeSpeed, $thumbnailsOpacity);
  50.     });

  51.     $thumbScroller.mousemove(function(e){
  52.         if($thumbScroller_container.width()>sliderWidth){
  53.               var mouseCoords=(e.pageX - $placement[1]);
  54.               var mousePercentX=mouseCoords/sliderWidth;
  55.               var destX=-((((totalContent+($tsMargin*2))-(sliderWidth))-sliderWidth)*(mousePercentX));
  56.               var thePosA=mouseCoords-destX;
  57.               var thePosB=destX-mouseCoords;
  58.               if(mouseCoords>destX){
  59.                   $thumbScroller_container.stop().animate({left: -thePosA}, $scrollEasing,$scrollEasingType); //with easing
  60.               } else if(mouseCoords<destX){
  61.                   $thumbScroller_container.stop().animate({left: thePosB}, $scrollEasing,$scrollEasingType); //with easing
  62.               } else {
  63.                 $thumbScroller_container.stop();  
  64.               }
  65.         }
  66.     });

  67.     $thumbnails_wrapper.fadeTo(fadeSpeed, $thumbnailsContainerOpacity);
  68.     $thumbnails_wrapper.hover(
  69.         function(){ //mouse over
  70.             var $this=$(this);
  71.             $this.stop().fadeTo("slow", 1);
  72.         },
  73.         function(){ //mouse out
  74.             var $this=$(this);
  75.             $this.stop().fadeTo("slow", $thumbnailsContainerMouseOutOpacity);
  76.         }
  77.     );

  78.     $thumbScroller_thumb.hover(
  79.         function(){ //mouse over
  80.             var $this=$(this);
  81.             $this.stop().fadeTo(fadeSpeed, 1);
  82.         },
  83.         function(){ //mouse out
  84.             var $this=$(this);
  85.             $this.stop().fadeTo(fadeSpeed, $thumbnailsOpacity);
  86.         }
  87.     );

  88.     //on window resize scale image and reset thumbnail scroller
  89.     $(window).resize(function() {
  90.         FullScreenBackground("#bgimg",$bgimg.data("newImageW"),$bgimg.data("newImageH"));
  91.         $thumbScroller_container.stop().animate({left: sliderLeft}, 400,"easeOutCirc"); 
  92.         var newWidth=$outer_container.width();
  93.         $thumbScroller.css("width",newWidth);
  94.         sliderWidth=newWidth;
  95.         $placement=findPos($the_outer_container);
  96.     });

  97.     //load 1st image
  98.     var the1stImg = new Image();
  99.     the1stImg.onload = CreateDelegate(the1stImg, theNewImg_onload);
  100.     the1stImg.src = $bgimg.attr("src");
  101.     $outer_container.data("nextImage",$(".content").first().next().find("a").attr("href"));
  102.     $outer_container.data("prevImage",$(".content").last().find("a").attr("href"));
  103. });

  104. function BackgroundLoad($this,imageWidth,imageHeight,imgSrc){
  105.     $this.fadeOut("fast",function(){
  106.         $this.attr("src", "").attr("src", imgSrc); //change image source
  107.         FullScreenBackground($this,imageWidth,imageHeight); //scale background image
  108.         $preloader.fadeOut("fast",function(){$this.fadeIn("slow");});
  109.         var imageTitle=$img_title.data("imageTitle");
  110.         if(imageTitle){
  111.             $this.attr("alt", imageTitle).attr("title", imageTitle);
  112.             $img_title.fadeOut("fast",function(){
  113.                 $img_title.html(imageTitle).fadeIn();
  114.             });
  115.         } else {
  116.             $img_title.fadeOut("fast",function(){
  117.                 $img_title.html($this.attr("title")).fadeIn();
  118.             });
  119.         }
  120.     });
  121. }

  122. //mouseover toolbar
  123. if($toolbar.css("display")!="none"){
  124.     $toolbar.fadeTo("fast", 0.4);
  125. }
  126. $toolbar.hover(
  127.     function(){ //mouse over
  128.         var $this=$(this);
  129.         $this.stop().fadeTo("fast", 1);
  130.     },
  131.     function(){ //mouse out
  132.         var $this=$(this);
  133.         $this.stop().fadeTo("fast", 0.4);
  134.     }
  135. );

  136. //Clicking on thumbnail changes the background image
  137. $("#outer_container a").click(function(event){
  138.     event.preventDefault();
  139.     var $this=$(this);
  140.     GetNextPrevImages($this);
  141.     GetImageTitle($this);
  142.     SwitchImage(this);
  143.     ShowHideNextPrev("show");
  144. }); 

  145. //next/prev images buttons
  146. $nextImageBtn.click(function(event){
  147.     event.preventDefault();
  148.     SwitchImage($outer_container.data("nextImage"));
  149.     var $this=$("#outer_container a[href='"+$outer_container.data("nextImage")+"']");
  150.     GetNextPrevImages($this);
  151.     GetImageTitle($this);
  152. });

  153. $prevImageBtn.click(function(event){
  154.     event.preventDefault();
  155.     SwitchImage($outer_container.data("prevImage"));
  156.     var $this=$("#outer_container a[href='"+$outer_container.data("prevImage")+"']");
  157.     GetNextPrevImages($this);
  158.     GetImageTitle($this);
  159. });

  160. //next/prev images keyboard arrows
  161. if($keyboardNavigation=="on"){
  162. $(document).keydown(function(ev) {
  163.     if(ev.keyCode == 39) { //right arrow
  164.         SwitchImage($outer_container.data("nextImage"));
  165.         var $this=$("#outer_container a[href='"+$outer_container.data("nextImage")+"']");
  166.         GetNextPrevImages($this);
  167.         GetImageTitle($this);
  168.         return false; // don't execute the default action (scrolling or whatever)
  169.     } else if(ev.keyCode == 37) { //left arrow
  170.         SwitchImage($outer_container.data("prevImage"));
  171.         var $this=$("#outer_container a[href='"+$outer_container.data("prevImage")+"']");
  172.         GetNextPrevImages($this);
  173.         GetImageTitle($this);
  174.         return false; // don't execute the default action (scrolling or whatever)
  175.     }
  176. });
  177. }

  178. function ShowHideNextPrev(state){
  179.     if(state=="hide"){
  180.         $nextImageBtn.fadeOut();
  181.         $prevImageBtn.fadeOut();
  182.     } else {
  183.         $nextImageBtn.fadeIn();
  184.         $prevImageBtn.fadeIn();
  185.     }
  186. }

  187. //get image title
  188. function GetImageTitle(elem){
  189.     var title_attr=elem.children("img").attr("title"); //get image title attribute
  190.     $img_title.data("imageTitle", title_attr); //store image title
  191. }

  192. //get next/prev images
  193. function GetNextPrevImages(curr){
  194.     var nextImage=curr.parents(".content").next().find("a").attr("href");
  195.     if(nextImage==null){ //if last image, next is first
  196.         var nextImage=$(".content").first().find("a").attr("href");
  197.     }
  198.     $outer_container.data("nextImage",nextImage);
  199.     var prevImage=curr.parents(".content").prev().find("a").attr("href");
  200.     if(prevImage==null){ //if first image, previous is last
  201.         var prevImage=$(".content").last().find("a").attr("href");
  202.     }
  203.     $outer_container.data("prevImage",prevImage);
  204. }

  205. //switch image
  206. function SwitchImage(img){
  207.     $preloader.fadeIn("fast"); //show preloader
  208.     var theNewImg = new Image();
  209.     theNewImg.onload = CreateDelegate(theNewImg, theNewImg_onload);
  210.     theNewImg.src = img;
  211. }

  212. //get new image dimensions
  213. function CreateDelegate(contextObject, delegateMethod){
  214.     return function(){
  215.         return delegateMethod.apply(contextObject, arguments);
  216.     }
  217. }

  218. //new image on load
  219. function theNewImg_onload(){
  220.     $bgimg.data("newImageW",this.width).data("newImageH",this.height);
  221.     BackgroundLoad($bgimg,this.width,this.height,this.src);
  222. }

  223. //Image scale function
  224. function FullScreenBackground(theItem,imageWidth,imageHeight){
  225.     var winWidth=$(window).width();
  226.     var winHeight=$(window).height();
  227.     if($toolbar.data("imageViewMode")!="original"){ //scale
  228.         var picHeight = imageHeight / imageWidth;
  229.         var picWidth = imageWidth / imageHeight;
  230.         if($toolbar.data("imageViewMode")=="full"){ //fullscreen size image mode
  231.             if ((winHeight / winWidth) < picHeight) {
  232.                 $(theItem).attr("width",winWidth);
  233.                 $(theItem).attr("height",picHeight*winWidth);
  234.             } else {
  235.                 $(theItem).attr("height",winHeight);
  236.                 $(theItem).attr("width",picWidth*winHeight);
  237.             };
  238.         } else { //normal size image mode
  239.             if ((winHeight / winWidth) > picHeight) {
  240.                 $(theItem).attr("width",winWidth);
  241.                 $(theItem).attr("height",picHeight*winWidth);
  242.             } else {
  243.                 $(theItem).attr("height",winHeight);
  244.                 $(theItem).attr("width",picWidth*winHeight);
  245.             };
  246.         }
  247.         $(theItem).css("margin-left",(winWidth-$(theItem).width())/2);
  248.         $(theItem).css("margin-top",(winHeight-$(theItem).height())/2);
  249.     } else { //no scale
  250.         $(theItem).attr("width",imageWidth);
  251.         $(theItem).attr("height",imageHeight);
  252.         $(theItem).css("margin-left",(winWidth-imageWidth)/2);
  253.         $(theItem).css("margin-top",(winHeight-imageHeight)/2);
  254.     }
  255. }

  256. //Image view mode function - fullscreen or normal size
  257. function ImageViewMode(theMode){
  258.     $toolbar.data("imageViewMode", theMode);
  259.     FullScreenBackground($bgimg,$bgimg.data("newImageW"),$bgimg.data("newImageH"));
  260.     if(theMode=="full"){
  261.          $toolbar_a.html("<img src='images/toolbar_n_icon.png' width='50' height='50'  />").attr("onClick", "ImageViewMode('normal');return false").attr("title", "Restore");
  262.     } else {
  263.          $toolbar_a.html("<img src='images/toolbar_fs_icon.png' width='50' height='50'  />").attr("onClick", "ImageViewMode('full');return false").attr("title", "Maximize");
  264.     }
  265. }

  266. //function to find element Position
  267.     function findPos(obj) {
  268.         var curleft = curtop = 0;
  269.         if (obj.offsetParent) {
  270.             curleft = obj.offsetLeft
  271.             curtop = obj.offsetTop
  272.             while (obj = obj.offsetParent) {
  273.                 curleft += obj.offsetLeft
  274.                 curtop += obj.offsetTop
  275.             }
  276.         }
  277.         return [curtop, curleft];
  278.     }
I'm sorry for the list, I did try to find a "spoiler" type tag, but you don't appear to have one, so I can't hide it.

Anyway, thanks for any help and apologies if this isn't the right forum and so forth.


Kind regards,
Will.

PS. You can see my site here if it helps (be kind).