2011-02-19 102 views
2

我建立这个幻灯片,特此一个临时网址:

http://ferdy.dnsalias.com/apps/jungledragon/html/tag/96/homepage/slideshow/mostcomments

有多种方式浏览,点击大图进入下一个图像,点击箭头进入下一个或下一张图像,你也可以使用你的键盘箭头。所有这些事件都会调用方法loadImage(在slideshow.js中)。

的图像加载是好的,但在该例程我正在使用$不用彷徨远程Ajax调用结束。此调用的目的是计算该图像的视图。这里是伪,斩件:

function loadImage(id,url) { 

     // general image loading routine 

     // enable loader indicator 
     $("#loading").show(); 

     var imagePreloader = new Image(); 
     imagePreloader.src = url; 
     loading = true; 
     $(imagePreloader).imagesLoaded(function() { 

      // load completed, hide the loading indicator 
      $("#loading").hide(); 

      // set the image src, this effectively shows the image 
      var img = $("#bigimage img"); 
      img.attr({ src: url, id: id }); 
      imageStartTime = new Date().getTime(); 

      // reset the image dimensions based upon its orientation 
      var wide = imagePreloader.width >= imagePreloader.height; 
      if (wide) { 
       img.addClass('wide'); 
       img.removeClass('high'); 
       img.removeAttr('height'); 
      } else { 
       img.addClass('high'); 
       img.removeClass('wide'); 
       img.removeAttr('width'); 
      } 

      // update thumb status 
      $(".photos li.active").removeClass('active'); 
      $("#li-" + id).addClass('active'); 

      // get the title and other attributes from the active thumb and set it on the big image 
      var imgTitle = $("#li-" + id + " a").attr('title'); 
      var userID = $("#li-" + id + " a").attr('data-user_id'); 
      var userName = $("#li-" + id + " a").attr('data-user_name'); 

      $(".caption").fadeOut(400,function(){ 
       $(".caption h1").html('<a href="' + basepath + 'image/' + id + '">' + imgTitle + '</a>'); 
       $(".caption small").html('Uploaded by <a href="' + basepath + 'user/' + userID + '">' + userName + '</a>'); 
       $(".caption").fadeIn(); 
      }); 

      // update counter 
      $(".counter").fadeOut(400,function() { $(".counter").text(parseInt($('.photos li.active .photo').attr('rel'))+1).fadeIn(); }); 

     // call image view recording function 
     $.get(basepath + "image/" + id + "/record/human"); 

     // loading routine completed 
     loading = false; 

} 

有很多东西在那里是不相关的。最后你可以看到我正在做$ .get调用。问题在于它以非常奇怪的方式触发。我第一次导航到tumb时,它被调用一次。下一次它被触发两次。在此之后,它被触发每导航动作2或3次,一般3

我想这一定是我的事件返回多个元素,因此调用的LoadImage例程多次。所以我把日志语句放在事件和loadimage例程中。事实证明loadimage被正确调用,每次点击只需要一次。

这意味着,似乎$不用彷徨是针对单人的范围内这样做。我惊呆了。

+1

嗯,我看到的一件事是$(imagePreloader).imagesLoaded(function(){...你错过了一个右括号? – Alvin 2011-02-19 00:51:41

+0

对不起,这是我的坏,一个部分粘贴。 。 – Ferdy 2011-02-19 01:01:12

回答

3

您的问题可能是:.imagesLoaded是一个jQuery插件在运行通过页面上的所有图片。如果您要附加负载事件只有imagePreloader,使用

$(imagePreloader).load(function() { 
    ... 
} 

否则,请提供您拨打loadImage()函数的代码。

更新上拇指这就是问题所在,当点击。 $(".photos li a").live('click',...只能在页面加载时调用一次。每次单击拇指时添加一个click处理程序不会删除以前的处理程序。

另一种方法是更改​​代码以$(".photos li a").unbind('click').live('click', ...这将删除之前注册的点击处理程序。

相关问题