Selector scope problem I think

Selector scope problem I think

I am creating a little slideshow with jQuery and I had everything working fine. At first I had the slideshow visible on document ready, but really I want it to appear only when a user clicks on an image. As soon as I put my code in a .click() event, it stopped working. If I merely comment out the .click() event the code works fine and the slideshow is visible..After debugging, it appears that the only difference is that during execution the variable slideWidth is initially set to 0 instead of the size of the image as it should. Any know what it could be?

  1. $(document).ready(function(){
  2. var currentPosition;
  3. var slides = $('.slide')
  4. var currentSlide;
  5. var currentImg;
  6.  var slideWidth;
  7.  var numberOfSlides = slides.length;
  8.  
  9. $('#filter').hide();
  10. $('#photoWrap').hide();

  11. $('.imgHolder').click(function(){
  12. currentPosition = $('.imgHolder').index(this);
  13. currentSlide = $('.slide').get(currentPosition);
  14. currentImg = $(currentSlide).children('img');
  15. slideWidth = $(currentImg).width();
  16. $('#slideShow').width(slideWidth);

  17.  // Remove scrollbar in JS
  18.  $('#slideContainer').css('overflow', 'hidden');

  19.  // Wrap all .slides with #slideInner div
  20.  slides
  21.  .wrapAll('<div id="slideInner"></div>')
  22.  // Float left to display horizontally, readjust .slides width
  23.  .css({
  24. 'float' : 'left',
  25. 'width' : slideWidth
  26.  });

  27. // Set #slideInner width equal to total width of all slides
  28. $('#slideInner').css('width', (slideWidth * numberOfSlides));

  29. // Hide left arrow control on first load
  30. manageControls(currentPosition);
  31. $('#slideInner').css('margin-left' , slideWidth*(-currentPosition));
  32.  
  33. $('#filter').show();
  34. $('#photoWrap').show();
  35. });




  36.   // Create event listeners for .controls clicks
  37.   $('.control')
  38.     .bind('click', function(){
  39.     // Determine new position
  40. currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
  41.   
  42. currentSlide = $('.slide').get(currentPosition);
  43. currentImg = $(currentSlide).children('img');
  44. var preWidth = slideWidth;
  45. slideWidth  = $(currentSlide).width();
  46. $('#slideShow').width(slideWidth);
  47.       // Hide / show controls
  48.       manageControls(currentPosition);
  49.       // Move slideInner using margin-left
  50.       $('#slideInner').animate({
  51.         'marginLeft' : slideWidth*(-currentPosition)
  52.       }, 'slow');
  53.  
  54.  
  55.     });
  56. $('#filter').click(function(){
  57. $(this).hide();
  58. $('#photoWrap').hide();
  59. });

  60.   // manageControls: Hides and shows controls depending on currentPosition
  61.   function manageControls(position){
  62.     // Hide left arrow if position is first slide
  63.     if(position==0){ $('#leftControl').hide() }
  64.     else{ $('#leftControl').show() }
  65.     // Hide right arrow if position is last slide
  66.     if(position==numberOfSlides-1){ $('#rightControl').hide() }
  67.     else{ $('#rightControl').show() }
  68.     }
  69. });