Vue 使用map标签与area标签 给图片添加点击事件

        <div class="img-content" @click.capture="handelClickParent">
            <img class="img" @load='imgOnError()' src="https://f.yunzhengshou.com/upload/helpdoc/wx-program-resources/house-bg.png" alt="房源" border="0" usemap="#Map" />
            <map name="Map" id="Map">
              <area id="1001" shape="rect" coords="766,2095,938,2499" href="javascript:void(0)" @click="handlerFloor('1幢')" />
              <area id="1002" shape="rect" coords="351,1791,499,2151" href="javascript:void(0)" @click="handlerFloor('2幢')" />
              <area id="1003" shape="rect" coords="1086,1735,1345,2291" href="javascript:void(0)" @click="handlerFloor('4幢')" />
              <area id="1004" shape="rect" coords="203,1227,355,1647" href="javascript:void(0)" @click="handlerFloor('6幢')" />
              <area id="1005" shape="rect" coords="299,1091,531,1419" href="javascript:void(0)" @click="handlerFloor('10幢')" />
                <area id="1006" shape="rect" coords="622,1055,734,1363" href="javascript:void(0)" @click="handlerFloor('13幢')" />
              <area id="1007" shape="rect" coords="846,1231,1026,1695" href="javascript:void(0)" @click="handlerFloor('9幢')" />
              <area id="1008" shape="rect" coords="1305,1187,1545,1419" href="javascript:void(0)" @click="handlerFloor('11幢')" />
            </map>
        </div>
  • cooreds属性:定义相关区域的坐标, 和shape属性搭配使用
  • shape属性:定义了相关区域的形
    • 默认值(default):规定全部区域
    • rect: 规定相关区域为矩形, cooreds属性值为(x1,y1,x2,y2),其中x1,y1为左上角的坐标,x2,y2为右下角的坐标;
    • circle: 规定相关区域为圆形, cooreds属性值为(x,y,radius),其中x,y为圆形的中心坐标,radius为圆形的半径;
    • poly: 规定先关区域为多边形, cooreds属性值为(x1,y1,x2,y2,x3,y3......xn,yn),规定了多边形各个顶点的坐标,由于浏览器会自动闭合多边形,所以尾部坐标不必与第一个坐标相等。
  • 对于如何测量出 cooreds属性值, 我使用的是PS
    • 首先用 PS打开图片, 选择窗口勾选信息选项


      image.png
  • 将鼠标放在需要定位的点上, 以矩形为例, 则是 左上和右下两个点的坐标(注意在测量前PS的测量工具单位需调整为像素)


    image.png
  • 根据屏幕大小的变化, 图片尺寸发生变化, cooreds 位置进行对应的变化 (Vue 中将该方法放到 mounted 生命周期函数中)
              // 图片加载完成触发
                imgOnError() {
                    $(window).resize(function () {
                        var $img = $('.img');
                        var width = $img.width();
                        var height = $img.height();
                        var naturalWidth = $img.get(0).naturalWidth;
                        var naturalHeight = $img.get(0).naturalHeight;
                        console.log(naturalWidth);
                        console.log(naturalHeight);
                        $("#Map area").each(function () {
                            var coords = $(this).attr("coords").split(",");
                            var points = $(this).attr('points');
                            
                            if (!!points) {
                                coords = points.split(",");
                            } else {
                                $(this).attr('points', coords);
                            }
                                
                            for (i = 0; i < coords.length; i += 2) {
                                var x = coords[i];
                                var y = coords[i + 1];
                                coords[i] = parseInt(parseInt(x) * width / naturalWidth);
                                coords[i + 1] = parseInt(parseInt(y) * height / naturalHeight);
                            }
                            $(this).attr("coords", coords.join(","));
                        })
                    }).trigger('resize');
                },
Tip: 对cooreds 位置进行计算之前一定要保证图片加载完成
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 需求 通过监听点击图片的某个范围绑定事件。 如何设置html热区? 利用img结合map和area标签锁定热区 H...
    Shaelyn_阅读 2,744评论 0 0
  • 导航栏 一:点击图片然后进行映射进行你想要的操作 二:如何知道你点击位置图片的XY坐标? 一:点击图片然后进行映射...
    前端切图仔阅读 1,875评论 0 0
  • HTML <map>元素是使用<area>元素定义一个图像地图(点击链接)。 在页面中需要有一个img元素,且该i...
    hui_mamba阅读 1,137评论 0 0
  • area 这个标签也非常有意思,它的作用是为图片提供点击热区,可以自己规定一张图的哪些区域可点击,且点击后跳转的链...
    涅槃快乐是金阅读 1,154评论 0 2
  • 标签定义图像映射中的区域(注:图像映射指得是带有可点击区域的图像)。即在图片中映射可点击的区域area标签总是嵌套...
    Pretty_Boy阅读 210评论 0 0