图库项目计数?

时间:2015-01-03 00:13:37

标签: javascript jquery json count gallery

我尝试使用存储在json对象中的数据创建一个简单的图库。它工作得很好,除了我似乎无法成功地重置'关闭图像后的图像计数,每次打开图像时,排序都是错误的。我知道它与模数有关,但我无法弄清楚我哪里出错了。

$.getJSON('js/work.json', function(data){

  var current = 0;
  var projects = data.projects;
  var currArr;

  $('.work-row > div').on('click', function(){

    current = 0;

    var i = $(this).index('.work-row div');
    currArr = projects[i];

    preloadImages(currArr, function(){


      $('.pop-forward').click(function(){
        current ++;
        current = current % currArr.images.length;
        $('.pop').css('backgroundImage', 'url(../images/work/main/'+currArr.dir+'/'+currArr.images[current].src+'.jpg)');
      });


      $('.pop-back').click(function(){
        current--;
        current = (current < 0) ? currArr.images.length-1 : current;
        $('.pop').css('backgroundImage', 'url(../images/work/main/'+currArr.dir+'/'+currArr.images[current].src+'.jpg)');
      });


      $('.close-image').click(function(){
        current = 0;
      });

    });

  });

这里是json:

{
  "projects": [
      {
          "dir" : "project1",
          "copy" : "Lorem Ipsum",
          "images" : [
          { 
           "src": "image1"
          },
          { 
           "src": "image2"
          },
          { 
           "src": "image3"
        }
      ]
    }
 ]
}

任何帮助都将非常感谢!!

1 个答案:

答案 0 :(得分:0)

current = current % currArr.images.length; 

应该是

current = current >= currArr.images.length ? 0 : current; 

这应该可以解决订购问题。

将全局current var放在第一个函数之前。