【问题标题】:Using Fancybox with Image map将 Fancybox 与图像映射一起使用
【发布时间】:2011-04-03 16:30:41
【问题描述】:

我想知道是否有办法将fancybox 与图像映射一起使用?

    <img src="\path\" usemap="#Map">
    <map name="Map" id="Map" >
      <area shape="poly" coords="0,0,0,328,145,328" href="#" />
      <area shape="poly" coords="0,0,180,0,328,328,142,328" href="#" />        
      <area shape="poly" coords="328,328,328,0,180,0" href="#" />    
    </map>

【问题讨论】:

    标签: jquery html fancybox lightbox


    【解决方案1】:

    所以你想在带有图像映射的fancybox中显示单个图像?

    可以通过几种方式将地图添加到fancybox的图像中,

    例如,您可以在图像加载后将其添加到图像中,图像将使用 id fancybox-img 加载,因此使用 oncomplete() 回调我们可以访问将地图添加到图像:

    HTML:

    <a href="/path/to/large/image" class="fancybox" title="Single image with a map">
        <img src="/path/to/small/image"/>
    </a>
    <map name="Map" id="Map">
        <area shape="poly" coords="0,0,0,328,145,328" href="#" />
        <area shape="poly" coords="0,0,180,0,328,328,142,328" href="#" />        
        <area shape="poly" coords="328,328,328,0,180,0" href="#" />    
    </map>
    

    jQuery:

    $("a.fancybox").fancybox({
        'titleShow': true,
        'titlePosition': 'inside',
        onComplete: function() {
            $('#fancybox-img').attr('usemap', '#Map');
        }
    });
    

    See it working here


    另一种方式是将内容传递给fancybox:

    HTML:

    <img src="/path/to/small/image" />
    <map name="Map" id="Map">
       <area shape="poly" coords="0,0,0,328,145,328" href="#" />
       <area shape="poly" coords="0,0,180,0,328,328,142,328" href="#" />        
       <area shape="poly" coords="328,328,328,0,180,0" href="#" />    
    </map>
    

    jQuery:

    $("img").click(function() {
        var url = $(this).attr('src');
        var content = '<img src="'+url+'" usemap="#Map" />';
        $.fancybox({
            'title'   : 'Single image with a map',
            'titlePosition': 'inside',
            'content' : content
        });
    });
    

    See it working here


    通过执行以下操作可以改进上述内容以支持多个图像和地图:

    HTML:

    <img src="/path/to/small/image" rel="#Map" title="Single image with a map 1" class="fancybox" />
    <br />
    <img src="/path/to/second/small/image" rel="#Map"  title="Single image with a map 2" class="fancybox" />
    <br />
    <img src="/path/to/non/fancybox/image" />
    <br/>
    Try clicking image to enlarge....
    <map name="Map" id="Map">
        <area shape="poly" coords="0,0,0,328,145,328" href="#" />
        <area shape="poly" coords="0,0,180,0,328,328,142,328" href="#" />        
        <area shape="poly" coords="328,328,328,0,180,0" href="#" />    
    </map>
    

    jQuery

    $("img.fancybox").click(function() {
        var url = $(this).attr('src');
        var map = $(this).attr('rel');
        var title = $(this).attr('title'); 
        var content = '<img src="' + url + '" usemap="'+ map + '" />';
        $.fancybox({
            'title': title,
            'titlePosition': 'inside',
            'content': content
        });
    });
    

    See it working here

    注意:我已经在 fancybox 中添加了几个选项,就像标题一样,只是为了说明如何添加选项。我还抓住了地图上的点击,所以它没有打开网址和警报,只是为了向您显示地图正在运行。


    根据 cmets 更新:

    啊,我误会了。在这种情况下,当用户单击地图项时,使用花式框非常简单。最简单的方法是使用 jQuery 选择器$('map &gt; area') 来捕捉对地图区域的任何点击。但是,如果您不希望所有区域都在花式框中打开,最好添加到您的选择器中,例如为您要打开花式框的每个区域指定一个类,然后使用选择器$('map &gt; area.fancybox'):

    HTML:

    <img src="/path/to/image" usemap="#Map" />
    <br />
    Try clicking image map.....
    <map name="Map" id="Map">
        <area shape="poly" coords="0,0,0,328,145,328" href="http://www.google.com" class="fancybox" title="Google" rel="iframe" />
        <area shape="poly" coords="0,0,180,0,328,328,142,328" href="http://farm6.static.flickr.com/5177/5574889454_b0a8c7845b.jpg" class="fancybox" title="EBR 1190 Typhon hits the track" rel="image" />        
        <area shape="poly" coords="328,328,328,0,180,0" href="http://www.ask.com" />
    </map>
    

    jQuery:

    $('map > area.fancybox').click(function(e) {
        e.preventDefault();
        var url = $(this).attr('href');
        var title = $(this).attr('title');
        var type = $(this).attr('rel');
        $.fancybox({
            'title': title,
            'titlePosition': 'inside',
            'href' : url,
            'type' : type
        });
    });
    

    See the example here

    • 所以在上面的例子中,我们使用 jQuery .click() 来捕捉带有类 fancybox 的地图区域上的任何点击(您会注意到 www.ask.com 示例将在窗口中打开,另一个两个在花式盒子中打开)
    • 我们使用.preventDefault() 方法来阻止浏览器像往常一样跟随链接。
    • 接下来获取点击区域的url。
    • 然后获取点击区域的标题(不是真的需要,只是添加以尝试帮助展示如何获取数据)
    • 接下来我使用rel 属性设置类型,这允许您设置您希望fancybox 执行的操作。
    • 现在只需打开包含详细信息的花式框即可。

    所以在这个例子中:

  • 区域 1 将打开一个 fancybox,它是一个包含页面的 iframe。
  • 区域 2 将打开一个包含图片的精美盒子。
  • 区域 3 将正常加载链接中的网站,而不使用 fancybox。
  • 【讨论】:

    • 感谢您的回复,但我认为您错过了我的意思。我的问题是,我想在单击打开fancybox 或lightbox 的任何区域时为图像进行映射(定义图像中的区域)。你找到我了吗?
    • @Mohamed Amgad 我确实误解了你的问题。我已经添加到答案中,这是另一个示例,允许您从图像映射中使用 fancybox - 我已经包含了几个示例,您可以如何使用它。我希望这会有所帮助!
    • 加载图像的最新fancybox句柄似乎是$('.fancybox-image')。
    猜你喜欢
    • 2014-12-12
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 2019-09-21
    相关资源
    最近更新 更多