MagicPopup动态图库conditionnal

时间:2018-05-30 12:54:11

标签: javascript jquery ajax magnific-popup

我的问题可能听起来很容易解决,但我被困了......

我使用jQueryMagnific Popup来创建动态图库。

但并非所有画廊都有六张照片,有些可能只有四张或五张。

所以这就是我的所作所为,并且由于某些原因它只是不起作用......如果有人可以帮助我......我会很高兴

var magic1;
var magic2;
var magic3;
var magic4;
var magic5;
var magic6;

if (post.photo1 != "") {
    var magic1 = '{ src: "http://linkto/img/'+post.photo1+'}'
} else {
    var magic1 = ""
}

if (post.photo2 != "") {
    var magic2 = '{ src: "http://linkto/img/'+post.photo2+'}'
} else {
    var magic2 = ""
}

if (post.photo3 != "") {
    var magic3 = '{ src: "http://linkto/img/'+post.photo3+'}'
} else {
    var magic3 = ""
}

if(post.photo4 != ""){
var magic4 ='{ src: "http://linkto/img/'+post.photo4+'}'
} else {
    var magic4 = ""
}

if (post.photo5 != "") {
    var magic5 = '{ src: "http://linkto/img/'+post.photo5+'}'
} else {
    var magic5 = ""
}

if (post.photo6 != "") {
    var magic6 = '{ src: "http://linkto/img/'+post.photo6+'}'
} else {
    var magic6 = ""
}

$('#fading-slider' + post.ID).magnificPopup({
    items: [
        magic1 + ',' +
        magic2 + ',' +
        magic3 + ',' +
        magic4 + ',' +
        magic5 + ',' +
        magic6
    ],
    gallery: {
      enabled: true
    },
    type: 'image' // this is default type
});

1 个答案:

答案 0 :(得分:0)

首先,你声明var两次,你可以尝试这样的事情:

 var imgs = new Array();
 if(post.photo1 != "")
   imgs.push({'src': 'http://linkto/img/'+post.photo1});
 if(post.photo2 != "")
   imgs.push({'src': 'http://linkto/img/'+post.photo2});

  ....
  ....
  $('#fading-slider' + post.ID).magnificPopup({
    items: imgs ,
    gallery: {
      enabled: true
    },
    type: 'image' // this is default type
  });