2012-01-30 46 views
-1

我使用jQuery插件(http://tympanus.net/codrops/2011/08/30/automatic-image-montage/)来显示div中的图像。它工作正常。但。当我用ajax将新图片加载到这个div中时,它什么也不做。如何让这个脚本使用ajax?

我认为这个问题是在这里:

(function(window, $, undefined) { 

这里是我的ajax调用(对不起,我忘了):

$('#albums').live('click', function() { 
$('#left_col div').hide(); 
$.ajax({ 
    url: document.base_url + 'welcome/list_albums', 
    success: function(data) { 
     $('#thumbs, #images').html(''); 
     $('#glist').html(data).show(); 

     var $container = $('#glist'), 
      $imgs  = $container.find('img').hide(), 
      totalImgs = $imgs.length, 
      cnt   = 0; 


     $imgs.each(function(i) { 
     var $img  = $(this); 
     $('<img/>').load(function() { 
      ++cnt; 
      if(cnt === totalImgs) { 
        $imgs.show(); 
        $container.montage({ 
          fillLastRow        : true, 
          alternateHeight     : true, 
          alternateHeightRange : { 
            min  : 70, 
            max  : 140 
          } 
        }); 

        /* 
        * just for this demo: 
        */ 
        $('#overlay').fadeIn(500); 
      } 
     }).attr('src',$img.attr('src')); 
     });  
    } 
}); 
return false;  

});

所以,我的问题是:我如何重新加载这个脚本或使它以某种方式与AJAX工作。

+0

向我们显示您的服务器的回应... – TOUDIdel 2012-01-30 08:20:00

+1

代码请... ...! – gdoron 2012-01-30 08:20:21

+0

我们需要看你的代码;初始化插件的代码以及执行AJAX请求的代码。 – Jasper 2012-01-30 08:21:12

回答

0

插件示例页面上有“示例4”,显示了您想要的内容。有一个“加载更多”按钮,将执行以下代码

$('#loadmore').show().bind('click', function() { 
    var len = imgarr.length; 
    for(var i = 0, newimgs = ''; i < 15; ++i) { 
     var pos = Math.floor(Math.random() * len), 
     src = imgarr[pos]; 
     newimgs += '<a href=""><img src="images/' + src + '.jpg"/></a>'; 
    } 
    var $newimages = $(newimgs); 
    $newimages.imagesLoaded(function(){ 
     $container.append($newimages).montage('add', $newimages); 
    }); 
}); 

所以基本上它创建与新(固定)图像的字符串,包装此字符串转换成jQuery对象并实现了回调的图像时,里面的字符串已被加载。在加载之后,您必须使用'add'参数和jQuery对象作为第二个参数来调用.montage()。

+0

Yeap,我见过这个,但这不是我所需要的。我想用新图像填充div,然后使用此插件。 Thx为您的答案。 – 2012-01-30 08:35:42

+0

Ahem,但在imagesLoaded()回调中的代码行正在做这个...用新图像填充$容器,然后使用插件。我不认为有任何其他(简单)方法 – devnull69 2012-01-30 09:06:59