当前位置:   article > 正文

获取鼠标点击图片时候的坐标,以及利用html 中的useMap 和area 实现图片固定位置的点击事件

获取鼠标点击图片时候的坐标,以及利用html 中的useMap 和area 实现图片固定位置的点击事件

一 编写原因

        应项目要求,需要对图片的固定几个位置分别做一个点击事件,响应不同的操作,如下图,需要点击红色区域,弹出不同的提示框:

二 获取点击图片时候的坐标

1. 说明

        实现这以上功能的前提是需要确定需要点击图片的区域坐标,才能实现准确点击。如果不用工具或者代码获取坐标的话,很难拿取到合适的位置。因此,这里就先进行坐标的获取。

2. 获取步骤

        (1) 创建一个html文件,先利用img标签将图片展示在网页上,一定要设置图片(img)的宽高(这里的宽高一定要跟图片响应事件功能代码中的图片宽高一致,切记切记,很重要。)。

        (2) 在img标签中添加一个 ismap 属性(这个属性值默认为true),可以不用赋值。

        (3)这个image外面包一层<a> 标签,或者其他有href 属性的标签(这个是必须的),里面href中的链接可以不写也可以写。

        区别是:如果不写,那么获取的坐标将会显示到当前html链接的后面,如果写了,那获取的坐标会发送到写了的href网页中。

        因此,我这里只想获取坐标,所以,我的href标签为空,不用发送坐标到其他网页。

        (4) 至此代码就写完了。运行代码,展示html页面。如下图:

        (5) 如上图,html展示了图片,这里我们要获取A和B两个位置的坐标。

                ① 获取A坐标:将鼠标放到A上点击一次。可以发现,当前页面的url后面有两个数字,这就是当前鼠标点击的坐标,所以,A点的坐标为(34,9)。

        ② 同样,获取B点坐标,鼠标点击B点,可以发现B的坐标是(108,38)

        综上,A和B的坐标就可以获取到了。这就是坐标获取方式。所以代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h1>获取鼠标点击图片时的坐标</h1>
  5. <!-- 这里的href可以为空 -->
  6. <a href="">
  7. <img style="border: 1px solid red" src="./test.png" width="200" height="400" ismap>
  8. </a>
  9. </body>
  10. </html>

三 实现图片点击不同位置响应不同事件的功能

        这个功能主要是利用html中的useMap属性和 area属性来实现的。步骤如下:

        1. 正常显示图片:

            这里使用到了img 标签,设置图片路径、useMap属性,以及图片的宽、高如果利用上面(第二大点)的方式获取坐标的话,这个宽高一定要跟坐标获取界面的宽高一致,切记切记,这个demo中,上面图片的宽高是(200,400),因此这张图片宽高也是设置为(200,400))。代码如下:

  1. <img style="border: 1px solid blueviolet" src="./test.png" width="200" height="400"
  2. useMap="#setting">

        2.上面一步设置了useMap属性,那么这一步就用来设置图片的点击区域,这里就需要使用到map标签以及area标签。

        (1)定义一个<map>标签,定义name属性为“setting”(这个setting必须是img标签中的useMap属性值)。

        (2)在<map>标签内容中写area标签,并配置shape、coords属性,以及onclick事件。

         shape(形状)和coords(坐标)是配对使用的,不同的shape配置不同的coords参数。如下所示:

               ①  shape为rect(矩形区域),那么coords格式为(x1,y1,x2,y2),x1y1和x2y2分别是矩形左上角和右下角坐标。

                ② shape为 poly(多边形区域),那么coords格式为(x1,y1,x2,y2,x3,y3,x4,y4,....),这些坐标分别是是多边形的顶点坐标。

                ③  shape为circle(圆形区域),那么coords格式为(x1,y1,r),x1y1是圆心坐标,r是半径。

        (3)利用第二大点的方式获取需要的顶点坐标,并配置到area属性中,我这里已经获取出来了,如下图:

A点坐标  39,4;  B点坐标 108,8。

C点坐标  72,290;  D点坐标 35,307;  E点坐标 72,322; F点坐标 110,306。

G点坐标 34,361;  H点坐标 108,389。

配置代码如下:

  1. <map name="setting" }>
  2. <!-- 点击【开始】区域(整个开始方框框起来的地方),网页会弹出【开始】字样-->
  3. <area shape="rect" coords="39,4,108,38" onclick="alert('开始')"/>
  4. <!-- 点击【满意】区域(整个开始方框框起来的地方),网页会弹出【满意】字样-->
  5. <area shape="Poly" coords="72,290,35,307,72,322,110,306" onclick="alert('满意')"/>
  6. <!-- 点击【结束】区域(整个开始方框框起来的地方),网页会弹出【结束】字样-->
  7. <area shape="rect" coords="34,361,108,389" onclick="alert('结束')"/>
  8. </map>

        (4)运行最终结果是,点击【开始】方框,网页弹出“开始”提示信息,点击【满意】区域,网页弹出【满意】提示信息,点击【结束】方框,网页弹出【结束】提示信息。结果如下图:

         备注:因为这张图只涉及了长方形和菱形,那么我们就用rect和poly设置,circle就暂时用不到就不写了,圆形也很简单,知道圆心和半径即可。

四 总结

整个网页代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h1> 点击图片,弹出不同的提示框</h1>
  5. <img style="border: 1px solid blueviolet" src="./test.png" width="200" height="400"
  6. useMap="#setting">
  7. <map name="setting" }>
  8. <!-- 点击【开始】区域(整个开始方框框起来的地方),网页会弹出【开始】字样-->
  9. <area shape="rect" coords="39,4,108,38" onclick="alert('开始')"/>
  10. <!-- 点击【满意】区域(整个开始方框框起来的地方),网页会弹出【满意】字样-->
  11. <area shape="Poly" coords="72,290,35,307,72,322,110,306" onclick="alert('满意')"/>
  12. <!-- 点击【结束】区域(整个开始方框框起来的地方),网页会弹出【结束】字样-->
  13. <area shape="rect" coords="34,361,108,389" onclick="alert('结束')"/>
  14. </map>
  15. </body>
  16. </html>

        

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/49219
推荐阅读
  • 文章浏览阅读1.9k次,点赞128次,收藏115次。超链接标签超链接路径相对路径的用法超链接分类超链接的应用超链接的分类创建HTTP文件下载超链接创建FTP站点访问超链接创建图像链接创建电子邮件超链接创建页面书签链接浮动框架制作浮动框架页面... [详细]

  • pythonselenium报错】selenium.common.exceptions.WebDriverException:Message:三种解决方案!【pythonselenium报错】selenium.common.excepti... [详细]

  • 网页设计题材,DIV+CSS布局制作,HTML+CSS网页设计期末课程大作业,茶文化网站|中华传统文化题材|京剧文化水墨风书画|中国民间年画文化艺术网站|HTML期末大学生网页设计作业HTML:结构CSS:样式在操作方面上运用了html5和... [详细]

  • 前端登录界面网站设计模板--HTML+CSS... [详细]

  • 大家好,我是yma16,本文分享关于前端富文本编辑器原理——从javascripthtml、css开始。富文本编辑器富文本编辑器是指具有格式化文本和图像编辑功能的文本编辑器Contenteditable属性Contenteditable是... [详细]

  • Vue(发音为/vjuː/,类似view)是一款用于构建用户界面的JavaScript框架。它基于标准HTMLCSS和JavaScript构建,并提供了一套声明式的、组件化的编程模型,帮助你高效地开发用户界面。无论是简单还是复杂的界面,Vu... [详细]

  • CSS颜色与背景颜色color属性背景background属性1.背景颜色background-color2.背景图像background-image3.背景图像平铺方式background-repeat4.固定/滚动背景图像backgro... [详细]

  • 本文实现,uniapp微信小程序,把页面内容保存为图片,并且下载到手机上,说实话网上找了很多资料,但是效果不理想,直到看了一个开源项目,我知道可以实现了,本文以开源项目uniapp-wxml-to-canvas为蓝本记录集成的步骤,以供参考... [详细]

  • 表格 表格 表格标记 表格标记-语法 表格属性设置 表格边框样式属性 表格单元格间距、单元格边距属性 表格水平对齐 设置表格行的属性 表格行的属性-设置 设置单元格的属性 设置单元格的属性-单元格跨行、列 表格嵌套课后练习网页标题:计算机报... [详细]

  • 作者:ManishChampaneriGolang可视化库sciter这是来自sciter网站的几句话,sciter桌面UI开发带来了一系列网页技术。网页设计者和开发者可以复用他们的经验和专长来构建看起来现代的桌面应用。多种多样的GUI框架... [详细]

  • 面试必备八股文!内容包含:Java基础、Java框架、Git命令、JVM、多线程、消息队列、微服务、Linux、数据库、Redis缓存、算法、Vue、React、前端性能优化等八股文面试HTML框架八股文十问十答第一期面试HTML框架八股... [详细]

  • 炫酷登录注册界面【超级简单 jQuery+JS+HTML+CSS实现】_html登录页面代码
    炫酷登录注册界面【超级简单jQuery+JS+HTML+CSS】_html登录页面代码html登录页面代码一:源码获取这两天根据需求写了一个比较好看的有动态效果的登录注册切换页面,这里我将源码资源分享给大家,大家可以直接免费下载使用哦,没有... [详细]

  • 嗨,朋友们!准备好开始一段令人兴奋的数字冒险吗?让我们从HTML的神秘世界开始吧。HTML,或者说超文本标记语言,不仅仅是编程的一种语言,它更像是网页的魔法咒语。每当你在网冲浪,浏览各种炫酷的网站时,其实你正在经历HTML的魔法。《HTM... [详细]

  • 现在,想象你在为一款新产品制作介绍页面。你想要清晰地展示产品名称、特点用户评价。DOCTYPEhtml>超级充电宝<body><h<em>2</em>>超级充电宝<p>这款<strong>超级充电宝可以在短短30分钟... <a title="《HTML 简易速速上手小册》第2章:HTML 的标签和元素(2024 最新版)" href="/blog/article/detail/44550" target="_blank">[详细]</a></div><div style="clear: both;"></div></div><div class="article_click rice1" style="width: 140px;"><p class="operation-b-img operation-b-img-active"><i class="img-up txclick" attc="upclick" attn="0"></i><span class="num"> 赞</span></p><p class="operation-b-img operation-b-img-active"><i class="img-down txclick" attc="downclick" attn="0"></i><span class="num">踩</span></p></div></li><li><div class="NewTitle"><a title="article" class="cat" href="/blog/article/list/1" target="_blank">article<i></i></a><h2><a title="【HTML 基础】元素和标签" href="/blog/article/detail/44564" target="_blank">【<em>HTML</em> <em>基础</em>】<em>元素</em>和<em>标签</em></a></h2></div><div class="NewsInfo"><div class="NewsDesc" style="width: 100%; ">这只是<em>HTML</em>中一小部分常见<em>标签</em>的介绍。了解这些<em>基础</em><em>标签</em>是构建网页的第一步。随着你深入学习,你将发现<em>HTML</em>提供了丰富的<em>标签</em>,用于定义文档的结构、内容和样式。继续学习<em>HTML</em>,打造令人印象深刻的Web页面吧!【<em>HTML</em><em>基础</em>】<em>元素</em>和<em>标签</em>文章目... <a title="【HTML 基础】元素和标签" href="/blog/article/detail/44564" target="_blank">[详细]</a></div><div style="clear: both;"></div></div><div class="article_click rice1" style="width: 140px;"><p class="operation-b-img operation-b-img-active"><i class="img-up txclick" attc="upclick" attn="0"></i><span class="num"> 赞</span></p><p class="operation-b-img operation-b-img-active"><i class="img-down txclick" attc="downclick" attn="0"></i><span class="num">踩</span></p></div></li><li><div class="NewTitle"><a title="article" class="cat" href="/blog/article/list/1" target="_blank">article<i></i></a><h2><a title="HTML — 样式 CSS" href="/blog/article/detail/44567" target="_blank"><em>HTML</em> — 样式 <em>CSS</em></a></h2></div><div class="NewsInfo"><div class="NewsDesc" style="width: 100%; "><em>CSS</em>是从<em>HTML</em>4开始使用的,是为了更好的渲染<em>HTML</em>元素而引入的。<em>HTML</em>—样式<em>CSS</em>一.使用    <em>CSS</em>是从<em>HTML</em>4开始使用的,是为了更好的渲染<em>HTML</em>元素而引入的。  ... <a title="HTML — 样式 CSS" href="/blog/article/detail/44567" target="_blank">[详细]</a></div><div style="clear: both;"></div></div><div class="article_click rice1" style="width: 140px;"><p class="operation-b-img operation-b-img-active"><i class="img-up txclick" attc="upclick" attn="0"></i><span class="num"> 赞</span></p><p class="operation-b-img operation-b-img-active"><i class="img-down txclick" attc="downclick" attn="0"></i><span class="num">踩</span></p></div></li><li><div class="NewTitle"><a title="article" class="cat" href="/blog/article/list/1" target="_blank">article<i></i></a><h2><a title="http:/222.212.79/app/zscd.html,emscripten/src/shell.html at 1.29.12 · emscripten-core/emscripten · G..." href="/blog/article/detail/46259" target="_blank"><em>http</em>:/222.2<em>12</em>.79/app/<em>zscd</em>.<em>html</em>,<em>emscripten</em>/src/shell.<em>html</em> at <em>1.29</em>.<em>12</em> · <em>emscripten</em>-core/<em>emscripten</em> · G...</a></h2></div><div class="NewsInfo"><div class="NewsDesc" style="width: 100%; ">version="1.1"id="Layer_1"x="0px"y="0px"width="296px"height="78px"viewBox="420<em>12</em>0100170"enable-background="new00900400"xm... <a title="http:/222.212.79/app/zscd.html,emscripten/src/shell.html at 1.29.12 · emscripten-core/emscripten · G..." href="/blog/article/detail/46259" target="_blank">[详细]</a></div><div style="clear: both;"></div></div><div class="article_click rice1" style="width: 140px;"><p class="operation-b-img operation-b-img-active"><i class="img-up txclick" attc="upclick" attn="0"></i><span class="num"> 赞</span></p><p class="operation-b-img operation-b-img-active"><i class="img-down txclick" attc="downclick" attn="0"></i><span class="num">踩</span></p></div></li><li><div class="NewTitle"><a title="article" class="cat" href="/blog/article/list/1" target="_blank">article<i></i></a><h2><a title="HTML-表单" href="/blog/article/detail/47688" target="_blank"><em>HTML</em>-<em>表单</em></a></h2></div><div class="NewsInfo"><div class="NewsDesc" style="width: 100%; ">概念:一个包含交互的区域,用于收集用户提供的数据。示例代码:2.常用<em>表单</em>控件1.文本输入框2.密码输入框3.单选框4.复选框5.下拉框6.隐藏域7.提交按钮8.重置按钮9.普通按钮10.文本域3.禁用<em>表单</em>控件给<em>表单</em>控件的标签设置disabl... <a title="HTML-表单" href="/blog/article/detail/47688" target="_blank">[详细]</a></div><div style="clear: both;"></div></div><div class="article_click rice1" style="width: 140px;"><p class="operation-b-img operation-b-img-active"><i class="img-up txclick" attc="upclick" attn="0"></i><span class="num"> 赞</span></p><p class="operation-b-img operation-b-img-active"><i class="img-down txclick" attc="downclick" attn="0"></i><span class="num">踩</span></p></div></li><li><div class="NewTitle"><a title="article" class="cat" href="/blog/article/list/1" target="_blank">article<i></i></a><h2><a title="HTML-CSS笔记_0424_,~ghj dyuouup" href="/blog/article/detail/48522" target="_blank"><em>HTML</em>-<em>CSS</em>笔记<em>_</em>0424<em>_</em>,~<em>ghj</em> <em>dyuouup</em></a></h2></div><div class="NewsInfo"><div class="NewsDesc" style="width: 100%; ">2、文档声明3、注释和标签属性4、实体5、meta便签5、语义化标签6、语义化标签7、语义化标签8、list标签9、超链接10、相对路径11、超链接12、images图片标签13、内联框架14、音频播放<em>CSS</em>二、<em>CSS</em>基础1、<em>CSS</em>简介外部... <a title="HTML-CSS笔记_0424_,~ghj dyuouup" href="/blog/article/detail/48522" target="_blank">[详细]</a></div><div style="clear: both;"></div></div><div class="article_click rice1" style="width: 140px;"><p class="operation-b-img operation-b-img-active"><i class="img-up txclick" attc="upclick" attn="0"></i><span class="num"> 赞</span></p><p class="operation-b-img operation-b-img-active"><i class="img-down txclick" attc="downclick" attn="0"></i><span class="num">踩</span></p></div></li><li><div class="NewTitle"><a title="article" class="cat" href="/blog/article/list/1" target="_blank">article<i></i></a><h2><a title="2018-04-21-linux-sources-list html-url、隐藏滚动条_s36ncqqh.xyz" href="/blog/article/detail/48608" target="_blank">2018-04-21-<em>linux</em>-<em>sources</em>-<em>list</em> <em>html</em>-url、隐藏滚动条_<em>s36ncqqh</em>.xyz</a></h2></div><div class="NewsInfo"><div class="NewsDesc" style="width: 100%; ">titlelayoutcategoriestagsexcerptKaliLinux之软件安装、卸载、更新和修改更新源postLinuxLinux安装软件Linux更新源Linux系统软件安装、卸载、更新与修改更新源使用Linux系统,与Wi... <a title="2018-04-21-linux-sources-list html-url、隐藏滚动条_s36ncqqh.xyz" href="/blog/article/detail/48608" target="_blank">[详细]</a></div><div style="clear: both;"></div></div><div class="article_click rice1" style="width: 140px;"><p class="operation-b-img operation-b-img-active"><i class="img-up txclick" attc="upclick" attn="0"></i><span class="num"> 赞</span></p><p class="operation-b-img operation-b-img-active"><i class="img-down txclick" attc="downclick" attn="0"></i><span class="num">踩</span></p></div></li></ul><div class="list_tools_top">相关标签</div><div class="list_tools_box"><ul><li><a title="html" rel="nofollow" href="/s?w=html" target="_self">html</a></li><li><a title="前端" rel="nofollow" href="/s?w=前端" target="_self">前端</a></li><li><a title="html5" rel="nofollow" href="/s?w=html5" target="_self">html5</a></li><li><a title="学习" rel="nofollow" href="/s?w=学习" target="_self">学习</a></li><li><a title="网页制作" rel="nofollow" href="/s?w=网页制作" target="_self">网页制作</a></li><li><a title="python" rel="nofollow" href="/s?w=python" target="_self">python</a></li><li><a title="selenium" rel="nofollow" href="/s?w=selenium" target="_self">selenium</a></li><li><a title="开发语言" rel="nofollow" href="/s?w=开发语言" target="_self">开发语言</a></li><li><a title="javascript" rel="nofollow" href="/s?w=javascript" target="_self">javascript</a></li><li><a title="css" rel="nofollow" href="/s?w=css" target="_self">css</a></li><li><a title="dreamweaver" rel="nofollow" href="/s?w=dreamweaver" target="_self">dreamweaver</a></li><li><a title="登录表单" rel="nofollow" href="/s?w=登录表单" target="_self">登录表单</a></li><li><a title="登录" rel="nofollow" href="/s?w=登录" target="_self">登录</a></li><li><a title="富文本编辑器" rel="nofollow" href="/s?w=富文本编辑器" target="_self">富文本编辑器</a></li><li><a title="vue.js" rel="nofollow" href="/s?w=vue.js" target="_self">vue.js</a></li><li><a title="vue" rel="nofollow" href="/s?w=vue" target="_self">vue</a></li><div style="clear: both;"></div></ul></div><div class="list_tools_top"></div></div></div></div><style type="text/css"></style><link rel="stylesheet" href="https://cdn.wpsshop.cn/public/blog/css/phone.css?v=13352890" type="text/css"><script type="text/javascript"></script></div></div></div></div></div><!--row_1_b100_1695--></div><style type="text/css"> /* footer css */ .index006-cover-page-foot { background-color: #F7F7F7; } </style><link rel="stylesheet" href="https://cdn.wpsshop.cn/public/ads/css/ads.css?v=12890" type="text/css"><script type="text/javascript" src="https://cdn.wpsshop.cn/public/ads/js/ads.js?v=1890" ></script><div class="cppui-row-1_100 cppui-row-view-1 row_1_100 ui-draggable" style="display: block;"><div class="cppui-column-1_100-0-1 column-view-com cppui-column-1_100-0-view-1 ui-sortable"><div class="drag-item-show-view-1 footermsg_view_pic ui-draggable" style="display: block;"><!--组件最外层要添加 drag-item-hide-view-1 --><div class="index006-cover-page-foot drag-item-hide-view-1"><p class="copyright">Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。 </p><div style="background-color:#F7F7F7; text-align:center; height:20px;padding-top:5px;"><a target="_blank" href="https://beian.mps.gov.cn/#/query/webSearch" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img src="https://cdnimages.wpsshop.cn/57public/skin/index/default/ui/images/beian_ghs.png" style="float:left;">  <p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;">闽ICP备14008679号</p></a>  <a href="https://www.wpsshop.cn/xml/article/detail/49219.xml" target="_blank" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img src="https://cdn.wpsshop.cn/public/blog/images/site.png" style="float:left;"></a>   <a href="/xml/w/g007/article/detail/new.xml" target="_blank" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img src="https://cdn.wpsshop.cn/public/blog/images/site.png" style="float:left;"></a>   <a href="/site.xml" target="_blank" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img src="https://cdn.wpsshop.cn/public/blog/images/site.png" style="float:left;"></a></div></div></div></div><!--row_1_b100_hcsoft_1693--></div><!--tjcode0088--><script type="text/javascript">var domain = document.domain;var hr=encodeURIComponent(window.location.href+'=='+document.referrer); document.write('<script src="https://ad.wpsshop.cn/admin.appflux?s=ad_flu_ind&fluxuserauto=yes&hr='+hr+'&p57weburl='+domain+'&p57usercode1='+domain+'&u=Vy1RJQhqAXUEMQNnAm0HaVVu&i='+p57ref("id")+'&r='+escape(document.referrer)+'" language="JavaScript"><\/script>');function p57ref(id){var reg=new RegExp("(^|&)"+id+"=([^&]*)(&|$)");var ref=window.location.search.substr(1).match(reg);if(ref!=null)return unescape(ref[2]);return null;} </script><!--tjcode0088--></body></html>