JQuery gallery includes next and previous buttons

I am trying to make some kind of Turn Over Script gallery with jQuery. So I got an array with - let's say 13 - images:

galleryImages = new Array(
'images/tb_01.jpg',
'images/tb_02.jpg',
'images/tb_03.jpg',
'images/tb_04.jpg',
'images/tb_05.jpg',
'images/tb_06.jpg',
'images/tb_07.jpg',
'images/tb_08.jpg',
'images/tb_09.jpg',
'images/tb_10.jpg',
'images/tb_11.jpg',
'images/tb_12.jpg',
'images/tb_13.jpg'
);

      

My gallery looks like a grid showing only 9 images at a time. My current Script already counts the number of li elements in the #gallery, loads the first 9 images and displays them. HTML looks like this:

<ul id="gallery">
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
</ul>

<ul id="gallery-controls">
    <li id="gallery-prev"><a href="#">Previous</a></li>
    <li id="gallery-next"><a href="#">Next</a></li>
</ul>

      

I am new to jQuery, my problem is that I cannot figure out how to split an array into pieces with 9 elements to attach it as a link to the control buttons. I need something like this:

$('#gallery-next').click(function(){
    $('ul#gallery li').children().remove();
    $('ul#gallery li').each(function(index,el){
        var img = new Image();
        $(img).load(function () {
            $(this).css('display','none');
            $(el).append(this);
            $(this).fadeIn();
        }).attr('src', galleryImages[index]); //index for the next 9 images?!?!
    });
});

      

Thanks for the help!

+2


a source to share


3 answers


Try the following:



<html>
<head>
<style>
#gallery-prev, #gallery-next{text-decoration:underline;color:Blue;cursor:pointer;list-style:none;display:inline;padding:5px;}
#gallery li{list-style:none;display:inline;}
</style>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script> 
<script type="text/javascript">
  galleryImages = new Array(
    'images/tb_01.jpg','images/tb_02.jpg','images/tb_03.jpg',
    'images/tb_04.jpg','images/tb_05.jpg','images/tb_06.jpg',
    'images/tb_07.jpg','images/tb_08.jpg','images/tb_09.jpg',
    'images/tb_10.jpg','images/tb_11.jpg','images/tb_12.jpg',
    'images/tb_13.jpg');

  function loadImages(p) {
    var startIndex = parseInt($('ul#gallery').attr("startIndex")) + 9 * p;
    if (startIndex < 1 || startIndex >= galleryImages.length) return;

    $('ul#gallery')
    .attr('startIndex', startIndex)
    .find("li").children().remove().end()
    .each(function(i, el) {
      var nextID = startIndex - 1 + i;
      if (nextID >= 0 && nextID < galleryImages.length) {
        $(this).append($("<img src='" + galleryImages[nextID] + "' alt='image " + (nextID + 1) + "' />"));
      }
    });
  }

  $(document).ready(function() {
    $('ul#gallery').attr('startIndex', '1');

    for (var i = 1; i <= 9; i++) 
      $('ul#gallery').append("<li></li>");

    loadImages(0);
    $('li#gallery-prev, li#gallery-next').each(function(i) {
      $(this).click(function() {
          loadImages(i == 1 ? 1 : -1); 
      }) 
    });
  });

</script>
</head>
<body>
<ul id="gallery"></ul>
<ul id="gallery-controls">
    <li id="gallery-prev">Previous</li>
    <li id="gallery-next">Next</li>
</ul>
</body>
</html>

      

0


a source


Check out my solution for your problem .

Several important notes:



  • I changed the way your buttons from the list work to two ranges.
  • I have moved the line $(el).append(img)

    to a better place.
  • curIndex

    is the index of the last image.
  • To get the next index image, I used curIndex+index

    and then changed it with galleryImages.length so that it would overflow in an overflow loop rather than throwing errors.
  • You can't see the images (for obvious reasons), so I've added an index as text in each <li>

    so you can see what's going on.
+1


a source


You can create a variable, for example var firstIndex = 0;

, to hold the array index of the first currently displayed image. You have your "next" button increasing it, and your "previous" button decreasing it as needed (by 1 or 9 or whatever you want). Add this value index

to your code.

Naturally, you will need to check the bounds of the array.

0


a source







All Articles