简体   繁体   English

对象内的For循环

[英]For loop inside object javascript

I'm using lightGallery and I'm using dynamic creation of galleries, this is the code to generate just one image: 我正在使用lightGallery,并且正在使用动态创建画廊,这是仅生成一张图像的代码:

$(this).lightGallery({
    dynamic:true,
    dynamicEl: [{
        'src':'css/images/pictures/gal_'+id+'/1.jpg',
        'thumb':'css/images/thumbnails/gal_'+id+'/1.jpg'
    }]
});

This id variable is always the same, but I want to loop through a number which I take for example from variable x . 这个id变量总是相同的,但是我想遍历一个数字,例如,我从变量x取一个数字。 So, if x=4 the code generated would look like this: 因此,如果x=4 ,则生成的代码将如下所示:

$(this).lightGallery({
    dynamic:true,
    dynamicEl: [{
        'src':'css/images/pictures/gal_'+id+'/1.jpg', //here's 1
        'thumb':'css/images/thumbnails/gal_'+id+'/1.jpg'
    },{
        'src':'css/images/pictures/gal_'+id+'/2.jpg', //here's 2 and so on
        'thumb':'css/images/thumbnails/gal_'+id+'/2.jpg'
    },{
        'src':'css/images/pictures/gal_'+id+'/3.jpg',
        'thumb':'css/images/thumbnails/gal_'+id+'/3.jpg'
    },{
        'src':'css/images/pictures/gal_'+id+'/4.jpg',
        'thumb':'css/images/thumbnails/gal_'+id+'/4.jpg'
    }]
});

So I guess the question is how to include a for loop inside an object, if that's even possible, thanks in advance! 所以我想问题是如何在对象中包含for循环,如果可能的话,请提前感谢!

No. It's not possible to have control structures(like loops) inside an object definition. 不能。在对象定义内不能有控制结构(如循环)。 You need to create your array of images first, like this: 您需要首先创建图像数组,如下所示:

var dynamicEl = [];
for (var i = 1; i <= 4; i++) {
  dynamicEl.push({
    'src':'css/images/pictures/gal_' + id + '/'+ i + '.jpg',
    'thumb':'css/images/thumbnails/gal_' + id + '/' + i + '.jpg'
  });
}

And then to pass it onto the object definition: 然后将其传递给对象定义:

$(this).lightGallery({
    dynamic:true,
    dynamicEl: dynamicEl
});

Try this 尝试这个

var genEls = function(id, count)
{
    var els = [];
    for(i = 1; i <= count; i++)
    {
        els.push({
            'src':'css/images/pictures/gal_'+ id + '/' + i + '.jpg',
            'thumb':'css/images/thumbnails/gal_' + id + '/' + i + '.jpg',
        });
    }
return els;
}   

var id = 3;
var count = 4;
$(this).lightGallery({
    dynamic:true,
    dynamicEl: genEls(id,count);
});

This is as inline as it can get ;) 这是内联的;)

Hope this helps ... 希望这可以帮助 ...

first create a method to dynamically generate thumbs 首先创建一种动态生成大拇指的方法

function genThumbs(count, id)
{
   var arr = [];
   for ( var counter = 1; counter <= count; counter++)
   {
      arr.push( {
        'src':'css/images/pictures/gal_'+id+'/' + counter + '.jpg',
        'thumb':'css/images/thumbnails/gal_'+id+'/' + counter + '.jpg'
      } );
   }
   return arr;
}

then use the same while calling the gallery 然后在调用图库时使用相同的

$(this).lightGallery({
    dynamic:true,
    dynamicEl: genThumbs(5, id)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM