【问题标题】:Jquery lightbox doesnt work with AdGalleryJquery 灯箱不适用于广告画廊
【发布时间】:2011-12-13 08:37:52
【问题描述】:

我正在使用 AdGallery 创建图片库(使用此插件:http://coffeescripter.com/2009/07/ad-gallery-a-jquery-gallery-plugin/)。

还有 Jquery 灯箱:http://leandrovieira.com/projects/jquery/lightbox/

现在我想要这个,当用户点击大图像时,会出现一个灯箱。于是我在jquery.ad-gallery.js中修改了几行代码:

来自

if(image.link) {
          var link = $('<a rel="lightbox" href="'+ image.link +'" target="_blank"></a>');
          link.append(img);
          img_container.append(link);
        } else {
         img_container.append(img);
        }

if(image.link) {
          var link = $('<a rel="lightbox" href="'+ image.link +'" target="_blank"></a>');
          link.append(img);
          img_container.append(link);
        } else {
         var link = $('<a href="'+ image.image +'" rel="lightbox" class="lightbox"></a>');
         link.append(img);
          img_container.append(link);
        }

但是当我点击大图时,什么都没有发生。

我的 html 中确实有这些代码:

$(function() {
   $('#gallery a').lightBox();
});

我错过了什么?

【问题讨论】:

    标签: jquery plugins jquery-plugins gallery lightbox


    【解决方案1】:

    代替:

    $(function() {
    $('#gallery a').lightBox();
    });
    

    用途:

    $(function() {
    $('a.lightbox').each(function () { $(this).lightBox(); });
    });
    

    这可以防止灯箱向 div 中的其他图形显示“下一个”选项并导致奇怪的结果,并防止灯箱附加到缩略图上。

    因为ad-gallery会在你更换图片的时候刷新ad-image div的内容,所以还要添加

    $('a.lightbox').each(function () { $(this).lightBox(); });
    

    在 jquery.ad-gallery.js 中的 _showWhenLoaded: 函数的末尾,以便在每个图像被选中时将 lighbox 事件附加到每个图像。

    【讨论】:

      【解决方案2】:

      我自己很难做到这一点。

      想法是幻灯片中的主图像弹出到大型灯箱弹出窗口。

      要解决的问题是:

      lightbox 会查找现有元素并创建一个图像数组,但在这里我们一次只能处理一个动态项目。 我们希望灯箱找到的动态元素是动态的,所以我们需要使用一些动态的jQuery方法而不是默认的方法。 我们希望灯箱知道幻灯片中的所有图像,即使它只会找到一个主图像。 我们希望灯箱能够找到与图像关联的文本。

      解决:

      确保将大弹出窗口的路径放在拇指的 longdesc 属性中,这样广告库会将它们放在幻灯片图像周围的 href 属性中,而 lightBox 会找到它们

      使用广告库中的回调为每张幻灯片调用灯箱加载。

              $(function() {
      
                  gallery1 = $('#gallery1').adGallery( 
      
                               { 
                                  callbacks: 
                                      {afterImageVisible: 
                                         function(){
      
                      //lightBox loading stuff here
      
                      dynamically find the title text and put it into the title of the link so lightBox can use it
                      if( this.images[ this.current_index ].title )
                      {
                              $(document).find( '.ad-image a' ).attr('title', this.images[ this.current_index ].title ); 
                      }
      
                      //use jQuery find to get the dynamic element then apply lightBox to it
                     $(document).find( '#gallery1 .ad-image a' ).lightBox({ imageArray: aImagesFullSizeLightBox, fixedNavigation: true });
      
                      //note, the "imageArray: aImagesFullSizeLightBox". This was my code addition to lightBox, which allowed me to tell lightBox what the imageArray is rather than trying to find them itself. You need to construct this array beforehand (see example below) in the correct format for lightBox to use, and you need to make code changes to lightBox to tell it to use it.
      
      
      
                                                } 
                                   }  
                            });
      
          }
      
      ///////////////////////////////////////////////////////////////////////////////
      
      //example array to pass to lightBox, make sure the above function can see it
      
      var aImagesFullSizeLightBox = [];
      
      aImagesFullSizeLightBox.push( new Array( '/images/bigPopupImage1.jpg','The Title Here 1') );
      
      aImagesFullSizeLightBox.push( new Array( '/images/bigPopupImage2.jpg','The Title Here 2') );
      
      
      //////////////////////////////////////////////////////////////////////////////////////
      
      //code changes needed to lightBox (this was done to version 0.5 which may be old now!)
      
      $.fn.lightBox = function(settings) {
      
              //addition to support next and backs without lightbox taking control of   clicks on thumbs 3/6/2012 Gordon Rouse
              if( settings.imageArray )
                  settings.setImagesExternally = true;
              else
                  settings.setImagesExternally = false;
      
      /////ALSO////////////////////////////////////////////////////////////
      
      
      function _start(objClicked,jQueryMatchedObj) {
      
                  $('embed, object, select').css({ 'visibility' : 'hidden' });
      
                  _set_interface();
      
                  // Unset total images in imageArray
                  //addition to support next and backs without lightbox taking control of clicks on thumbs - Gordon Rouse
                  if ( !settings.setImagesExternally )
                      settings.imageArray.length = 0;
      
                  //settings.imageArray.length = 0;  //undo line for above!
      
                  settings.activeImage = 0;
      
                              //here we stop lighBox trying to load the images it found
                  if (!settings.setImagesExternally )
                  {
                      if ( jQueryMatchedObj.length == 1 ) {
                          settings.imageArray.push(new Array(objClicked.getAttribute('href'),objClicked.getAttribute('title')));
                      } else {
                          // Add an Array (as many as we have), with href and title atributes, inside the Array that storage the images references        
                          for ( var i = 0; i < jQueryMatchedObj.length; i++ ) {
                              settings.imageArray.push(new Array(jQueryMatchedObj[i].getAttribute('href'),jQueryMatchedObj[i].getAttribute('title')));
                          }
                      }
                  }
      
      ///////////////////////////////////////////////////////
      

      这里有一个工作示例:

      http://www.vikingkayaks.co.nz/shop/kayaks?product=1

      但请注意,此示例中还有其他复杂的内容,因此我尝试提炼上面描述中的重要部分。

      【讨论】:

        猜你喜欢
        • 2016-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-13
        • 2019-03-25
        • 1970-01-01
        相关资源
        最近更新 更多