【问题标题】:Jquery selecting images in a link in and out of a table then adding to fancyboxJquery在表格的链接中选择图像,然后添加到fancybox
【发布时间】:2012-05-27 05:12:35
【问题描述】:

当用户单击链接/缩略图时,我有一些数据库生成的 html 包含图像,我希望这些图像显示在花式框中

如果缩略图在表格中,我想将它们分组到一个简单的图片库中,例如示例 here

如果它只是某个文本中图像的链接,我希望它在幻想框中显示为单个图像,就像“不同效果”之一一样

但是,如果链接只是链接,那么它们应该不受影响

我对 jquery 选择器有点麻烦,谁能指出错误

是否还可以在fancybox 中为图片添加缩放功能,而无需链接到另一个更大版本的图片?

    <!DOCTYPE html>
    <html>
    <head>
        <title>fancyBox - Fancy jQuery Lightbox Alternative | Demonstration</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

        <!-- Add jQuery library -->
        <script type="text/javascript" src="../lib/jquery-1.7.2.min.js"></script>

        <!-- Add mousewheel plugin (this is optional) -->
        <script type="text/javascript" src="../lib/jquery.mousewheel-3.0.6.pack.js"></script>

        <!-- Add fancyBox main JS and CSS files -->
        <script type="text/javascript" src="../source/jquery.fancybox.js?v=2.0.6"></script>
        <link rel="stylesheet" type="text/css" href="../source/jquery.fancybox.css?v=2.0.6" media="screen" />

        <!-- Add Button helper (this is optional) -->
        <link rel="stylesheet" type="text/css" href="../source/helpers/jquery.fancybox-buttons.css?v=1.0.2" />
        <script type="text/javascript" src="../source/helpers/jquery.fancybox-buttons.js?v=1.0.2"></script>

        <!-- Add Thumbnail helper (this is optional) -->
        <link rel="stylesheet" type="text/css" href="../source/helpers/jquery.fancybox-thumbs.css?v=1.0.2" />
        <script type="text/javascript" src="../source/helpers/jquery.fancybox-thumbs.js?v=1.0.2"></script>

        <!-- Add Media helper (this is optional) -->
        <script type="text/javascript" src="../source/helpers/jquery.fancybox-media.js?v=1.0.0"></script>

        <script type="text/javascript">
       var iTabNum=0;
        $(document).ready(function() {

          $('a > img').each(function(){

            if($(this).parent('table'))
              {
              var TabID;
                  if (typeof($(this).parent('table').attr('id')) == "undefined" )
                     {
                      iTabNum ++;
                      $(this).parent('table').attr('id', "img" + TabID + iTabNum);
                     }
                  else
                     {
                       TabID = "img" + $(this).parent('table').attr('id'); 
                     }
                $(this).closest().attr('class', "fancybox");
                $(this).closest().attr(TabID + '-group', "gallery" );

              }
            else
              {
              $(this).attr('class', "fancybox-effects-d");

              }


        });

            $('.fancybox').fancybox({
          overlay : {
                css : {
        //  hover > img{width:100px;height:100px;z-index:1000;position:relative;top:-50px;left:-50px;},
                    'background-color' : '#eee'
                }
          }});

        $(".fancybox-effects-d").fancybox({
                    padding: 0,

                    openEffect : 'elastic',
                    openSpeed  : 150,

                    closeEffect : 'elastic',
                    closeSpeed  : 150,

                    closeClick : true,

                    helpers : {
                        overlay : null
                    }
                });          
    });

      </script>
      </head>
      <body>
            <div>
              <table>
                <tr>
                  <td><a  href="1_b.jpg" title="Lorem ipsum dolor sit amet"><img src="1_s.jpg" alt="" /></a>
                  </td>
                  <td><a  href="2_b.jpg" title="Etiam quis mi eu elit temp"><img src="2_s.jpg" alt="" /></a>
                  </td>
                </tr>
                <tr>
                  <td><a  href="3_b.jpg" title="Cras neque mi, semper leon"><img src="3_s.jpg" alt="" /></a></td>


                  <td><a  href="4_b.jpg" title="Sed vel sapien vel sem uno"><img src="4_s.jpg" alt="" /></a></td>

                </tr>
              </table>
            </div>
            <div>
                <h3>Etiam quis mi eu elit</h3>
                <p>
                    Lorem ipsum dolor sit amet,  <a href="http://fancyapps.com/fancybox/">www.fancyapps.com/fancybox/</a> consectetur adipiscing elit. Etiam <a href="5_b.jpg" title="Lorem ipsum dolor sit amet, consectetur adipiscing elit">Image</a> quis mi eu elit tempor facilisis id et neque. 
                </p>
        </div>

      </body>
    </html>

【问题讨论】:

  • 那么,你不能事先添加fancybox 类吗?
  • 不幸的是,并非没有执行复杂的 SQL 语句来更新存储在数据库中的 HTML
  • 关于“添加缩放功能”,fancybox 按钮助手包括一个按钮,当图像被缩小以适合视口时,可以查看图像的实际大小
  • 你指的是尺寸切换吗?

标签: jquery fancybox


【解决方案1】:

我希望我理解你的问题,但基本上你想要的是:

  • 为每个表格分配不同的ID,以便该表格内的图像(带缩略图)链接属于同一个图库。

  • 在以下条件下,任何指向图像的链接都应获得class="fancybox-effects-d"class="fancybox"

    • fancybox-effects-d 用于指向表格之外的图像的链接和/或指向没有缩略图的图像的链接,无论它们是否在表格内部。
    • fancybox 和相同的 rel 属性链接到图像inside 一个表(rel 属性应该匹配表的 ID 并且不同于其他表)和 with 缩略图.
    • 其他链接(不是图片)无论是否在表格内都应该不受影响。

因此,如果您有多个table,则每个table 应该是这样的

<table id="img1"></table>
<table id="img2"></table>

那么table 的每个外部链接应该如下所示:

<a class="fancybox-effects-d" href="image01.jpg"><img src="image01thumb.jpg" alt="" /></a>
<a class="fancybox-effects-d" href="image02.jpg">image 02</a>
<a href="http://example.com">link somewhere else</a>

table内部链接应如下所示:

<table id="img1">
 <a class="fancybox" rel="img1" href="image03.jpg"><img src="image03thumb.jpg" alt="" /></a>
 <a class="fancybox" rel="img1" href="image04.jpg"><img src="image04thumb.jpg" alt="" /></a>
 <a class="fancybox-effects-d" href="image05.jpg">image 05</a>
 <a href="http://example2.com">another link</a>
</table>
<table id="img2">
 <a class="fancybox" rel="img2" href="image06.jpg"><img src="image06thumb.jpg" alt="" /></a>
 <a class="fancybox" rel="img2" href="image07.jpg"><img src="image07thumb.jpg" alt="" /></a>
 <a class="fancybox-effects-d" href="image08.jpg">image 08</a>
 <a href="http://example3.com">another link</a>
</table>

如果以上所有内容都正确,那么您可以简化您的脚本,例如:

$(document).ready(function() {
 // assign a different ID to each table
 $('table').each(function(i){
  $(this).attr('id', 'img'+(i+1));
 });
 // go through all <a> tags
 $('a').each(function(i){
  // get the parent tag name
  var parentTag = $(this).parents()[0].nodeName;
  // detect if link has thumbnail
  var hasThumb = $(this).children().html();
  // detect if the link targets to an image, otherwise ignored
  if(this.href.match(/\.(jpe?g|gif|png|bmp)((\?|#).*)?$/i)){
   // since the parent of an element inside a table is (should be) "TD" 
   // then evaluate if the link is inside a table
   // AND it has a thumbnail
   if(parentTag == "TD" && hasThumb != null){
    // get the parent table's ID
    var TabID = $(this).closest("table").attr("id");
    // set the class and rel attributes
    $(this).attr({"class":"fancybox","rel": TabID});
   }else{
    // if link to an image has not a thumbnail AND/OR is outside a table
    $(this).addClass("fancybox-effects-d");
   }
  }
 }); // each
}); // ready

您可以调整脚本以满足您的需求。

顺便说一句,您的一个fancybox 脚本的正确语法应该是:

 $('.fancybox').fancybox({
  helpers : {
   css : {'background-color' : '#eee'}
  }
 });

(您省略了helpers 选项)

【讨论】:

    猜你喜欢
    • 2011-08-14
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多