Instafeed js在每4个图像周围添加包装器

时间:2014-12-18 03:28:08

标签: javascript jquery html5 plugins instafeedjs

我正在使用instafeed js从Instagram调用照片。有没有办法将每4个图像包装在div中?这有可能吗?这是我的代码:

  jQuery(window).load(function(){

      var userFeed = new Instafeed({
        get: 'user',
        userId: 1509441625,
        accessToken: '1509441625.5b9e1e6.c20b3eb91b15404ab30084283ab3a9c9',
        limit : 4,             
        resolution:'standard_resolution',
        template: '<a target="_Blank" href="{{link}}"><img src="{{image}}" /></a> ',

      });
      userFeed.run();

    });

我联系了instafeed的开发人员,他给了我一个未经测试的代码,我试图调试:

      var count = 0;
      var userFeed = new Instafeed({
        get: 'user',
        userId: 1509441625,
        accessToken: '1509441625.5b9e1e6.c20b3eb91b15404ab30084283ab3a9c9',
        limit : 4,             
        resolution:'standard_resolution',
        filter: function(image) {
            count += 1;
            if (count % 4 === 0) {
                image.customTagOpen = '<div>';
                image.customTagClose = '</div>';
            } else {
                image.customTagOpen = '';
                image.customTagClose = '';
            }
            return true;
        },
        template: '{{model.customTagOpen}}<a target="_Blank" href="{{link}}"><img src="{{image}} /></a>{{model.customTagClose}}';

      });
      userFeed.run();

    });

但是我收到错误:在列出房产后遗漏了“}”。有什么建议吗?

3 个答案:

答案 0 :(得分:3)

template选项行的末尾有一个错误放置的分号。

尝试将分号;更改为逗号,

template: '{{model.customTagOpen}}<a target="_Blank" href="{{link}}"><img src="{{image}} /></a>{{model.customTagClose}}',

答案 1 :(得分:1)

除了史蒂文的回答,您可以使用此修复程序:

您可以使用此修复程序:

if (count % 4 === 0 || count === 0) {
    image.customTagOpen = '<div>';
    image.customTagClose = '';
} else if (count + 1 % 4 === 0) {
    image.customTagOpen = '';
    image.customTagClose = '</div>';
} else {
    image.customTagOpen = '';
    image.customTagClose = '';
}

干杯,

答案 2 :(得分:0)

对于div行每4个计数使用此:

  filter: function(image) {
    count += 1;
    if (count == 1 || (count - 1) % 4 == 0 ) {
      image.customTagOpen = '<div class="row">';
      image.customTagClose = '';
    } else if (count % 4 == 0) {
      image.customTagOpen = '';
      image.customTagClose = '</div>';
    } else {
      image.customTagOpen = '';
      image.customTagClose = '';
    }
    return true;
  },

或使用

  filter: function(image) {
    if (count == 0 || count % 4 == 0 ) {
      image.customTagOpen = '<div class="row">';
      image.customTagClose = '';
    } else if ((count + 1) % 4 == 0) {
      image.customTagOpen = '';
      image.customTagClose = '</div>';
    } else {
      image.customTagOpen = '';
      image.customTagClose = '';
    }
    count += 1;
    return true;
  },