使用Fancybox和图像映射

Moh*_*gad 12 html jquery lightbox fancybox

我想知道是否有办法将fancybox与Image贴图一起使用?

    <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>
Run Code Online (Sandbox Code Playgroud)

Sco*_*ler 23

那么你想在带有图像映射的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>
Run Code Online (Sandbox Code Playgroud)

jQuery的:

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

看到它在这里工作


另一种方法是将内容传递给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>
Run Code Online (Sandbox Code Playgroud)

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
    });
});
Run Code Online (Sandbox Code Playgroud)

看到它在这里工作


通过这样做,可以改进以上内容以支持多个图像和地图:

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>
Run Code Online (Sandbox Code Playgroud)

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
    });
});
Run Code Online (Sandbox Code Playgroud)

看到它在这里工作

注意:我在fancybox中添加了几个选项,比如标题只是为了展示如何添加选项.我也抓住了地图上的点击,因此它没有打开网址,只是为了向您显示地图正在运行.


根据评论更新:

啊,我误解了.在这种情况下,当用户点击地图项时,使用fancybox非常简单.最简单的方法是使用jQuery选择器$('map > area')捕获地图区域的任何点击.但是,如果你不希望所有区域都在fancybox中打开,那么最好添加到你的选择器,例如给你想要打开fancybox的每个区域一个类,然后使用选择器$('map > 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>
Run Code Online (Sandbox Code Playgroud)

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
    });
});
Run Code Online (Sandbox Code Playgroud)

请参阅此处的示例

  • 因此,在上面的示例中,我们使用jQuery .click()来捕获具有类fancybox的地图区域上的任何点击(您将注意到www.ask.com示例将在窗口中打开,其他两个在fancybox中打开).
  • 我们使用该.preventDefault()方法像往常一样在链接后停止浏览器.
  • 接下来点击该区域的网址.
  • 然后获取点击区域的标题(不是真的需要,但只是添加以尝试并帮助显示如何获取数据)
  • 接下来,我使用rel属性设置类型,这允许您设置您希望fancybox执行的操作.
  • 现在只需打开带有详细信息的fancybox即可.

所以在这个例子中:

  • 区域1将打开一个fancybox,它是一个包含页面的iframe.
  • 区域2将打开一个带有图像的fancybox.
  • 区域3将正常加载链接中的网站,而不是使用fancybox.