2011-05-31 63 views
1

我正在使用一个简单的jquery画廊(JQuery Hover Effect Image Gallery For eCommerce),它的工作原理非常完美。在同一页面上使用jquery画廊两次

在其中一个页面上有两个画廊,我想使用此脚本。我清楚地在Javascript精通延长脚本来支持画廊和如下我真的很感激,如果有人可以看看..

为画廊的代码是:

$(document).ready(function() { 
    $('#thumbs ul li a').hover(
     function() { 
      var currentBigImage = $('#bigpic img').attr('src'); 
      var newBigImage = $(this).attr('rev'); 
      var currentThumbSrc = $(this).attr('rel'); 
      switchImage(newBigImage, currentBigImage, currentThumbSrc); 
     }, 
     function() {} 
    ); 

    function switchImage(imageHref, currentBigImage, currentThumbSrc) { 
     var theBigImage = $('#bigpic img'); 

     if (imageHref != currentBigImage) {  
      theBigImage.fadeOut(250, function() { 
       theBigImage.attr('src', imageHref).fadeIn(250); 

       var newImageDesc = $("#thumbs ul li a img[src='"+currentThumbSrc+"']").attr('alt'); 
       $('p#desc').empty().html(newImageDesc); 
      });   
     }  
    } 
}); 
+0

也发布您的目标标记。然后我可以帮你把它写成可重复使用的插件 – XGreen 2011-05-31 14:31:53

回答

1

我会强烈建议使这个脚本面向对象,所以你可以创建一个画廊对象尽可能多的,但这是一些严重的重构。

但现在,你可以添加一个额外的jQuery选择是这样的:

线路3:

$('#firstgallery ul li a, #secondgallery ul li a').hover(

第23行:

var newImageDesc = $("#firstgallery ul li a img[src='"+currentThumbSrc+"']").attr('alt'); 
var newImageDesc = $("#secondgallery ul li a img[src='"+currentThumbSrc+"']").attr('alt'); 

确保您给画廊正确的ID在你的HTML,我认为这些是div的:

<div id="firstgallery">...thumbnails...</div> 
<div id="secondgallery">...thumbnails...</div> 
3
(function ($) { 

    $.fn.gallerify = function (options) { 

     var settings = { 
      'galleryOption1': 'value', 
      'galleryOption2': 'value' 
     }; 

     return this.each(function() { 
      // If options exist, lets merge them 
      // with our default settings 
      if (options) { 
       $.extend(settings, options); 
      } 

      // Do the stuff your gallery does. without 
      // mentioning specific selectors like #bla or .bla 
      // Only refer to elements relative to $(this) 

     }); 

    }; 
})(jQuery); 


$(function() { 
    $('#thumbs').gallerify({ 
     galleryOption1 : 'makemygallerythebest', 
     galleryOption2 : 'buymeabeer' 
    }); 
}); 
+1

这是一个干净而好的解决方案。我的答案纯粹是实用的,这是一些面向对象的代码! – 2011-05-31 14:41:38

相关问题