jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 使用Fancybox与图像映射大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道是否有一种方法来使用图像映射的fancybox
<img src="\path\" usemap="#Map">
    <map name="Map" id="Map" >
      <area shape="poly" coords="0,328,145,328" href="#" />
      <area shape="poly" coords="0,180,142,328" href="#" />        
      <area shape="poly" coords="328,0" href="#" />    
    </map>

解决方法

@H_403_7@ 那么你想在一个带有图像映射的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,328" href="#" />
    <area shape="poly" coords="0,328" href="#" />        
    <area shape="poly" coords="328,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,328" href="#" />
   <area shape="poly" coords="0,328" href="#" />        
   <area shape="poly" coords="328,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','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" 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,'content': content
    });
});

See it working here

注意:我已经添加了几个选项到fancybox,如标题只是为了显示如何添加选项.我也抓住了地图上的点击,所以它没有打开网址和警报只是为了告诉你地图正在工作.

根据评论更新:

啊,我误解了在这种情况下,当用户点击地图项时,使用fancybox是非常简单的.最简单的是使用jQuery选择器$(‘map> area’)捕获地图上某个区域的任何点击.但是,如果您不希望所有区域都在一个fancybox中打开,最好添加到选择器中,例如给每个要打开一个fancybox的区域一个类,然后使用选择器$(‘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,328" href="http://www.google.com" class="fancybox" title="Google" rel="iframe" />
    <area shape="poly" coords="0,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,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,'href' : url,'type' : type
    });
});

See the example here

>所以在上面的例子中,我们使用jQuery .click()捕获类fancybox的地图区域上的任何点击(你会注意到www.ask.com的例子将在窗口中打开,另外两个在一个fancybox).
>我们使用.preventDefault()方法来停止浏览器的链接,就像通常情况一样.
>接下来得到点击的区域的URL.
>然后获取点击的区域的标题(不是真的需要,但只是添加尝试和帮助显示如何获取数据)
>接下来我使用rel属性设置类型,这样可以设置你想要的fancybox做什么.
>现在只需打开fancybox的细节.

所以在这个例子中:>区域1将打开一个fancybox,它是一个包含页面的iframe.>区域2将打开一个带有图像的fancybox.>区域3将正常加载网站链接正常不使用fancybox.

大佬总结

以上是大佬教程为你收集整理的jquery – 使用Fancybox与图像映射全部内容,希望文章能够帮你解决jquery – 使用Fancybox与图像映射所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: