HTML图像热区随图片缩放自动调整

灰太狼 2022-04-08 09:29 317阅读 0赞

  思路很简单,就是设置coords对应的坐标按比例缩放就可以了。注意,给出的代码只是页面初始显示时调整一次,再发生变化没有进行调整。

  1. <img src="KnMap.png" alt="知识地图" id="KnMap" style="width:100%;height:auto;" border="0" usemap="#Map">
  2. <map name="Map">
  3. <area shape="rect" coords="146,172,271,248" href="open?id=1000000062">
  4. <area shape="rect" coords="483,240,578,316" href="open?id=1000000064">
  5. <area shape="rect" coords="614,546,709,618" href="open?id=1000000066">
  6. <area shape="rect" coords="60,552,180,623" href="open?id=1000000081">
  7. <area shape="rect" coords="593,740,757,827" href="open?id=1000000061">
  8. <area shape="rect" coords="253,931,624,1091" href="open?id=1000000054">
  9. </map>
  10. <script src="../js/jquery-1.11.1.min.js"></script>
  11. <script type="text/javascript">
  12. function resetHostArea(){
  13. var srcw="960",srch="1429";
  14. var noww=$("#KnMap").prop("clientWidth")*1.0;
  15. //var nowh=$("#KnMap").prop("clientHeight")*1.0;
  16. var scale = noww/srcw;
  17. var areas = $("area");
  18. var coords;
  19. areas.each(function(){
  20. coords = this.coords;
  21. coords = coords.split(",");
  22. coords = Math.round(coords[0]*scale)+","+Math.round(coords[1]*scale)+","+Math.round(coords[2]*scale)+","+Math.round(coords[3]*scale);
  23. this.coords=coords;
  24. });
  25. }
  26. $(function(){
  27. resetHostArea();
  28. });
  29. </script>

发表评论

表情:
评论列表 (有 0 条评论,317人围观)

还没有评论,来说两句吧...

相关阅读

    相关 图像算法

    怎样计算缩放比例? 有四种方法:1.对角线法,以对角线长度为准,其伸长者为放大,缩短者为缩小,其底边并行线即为新尺寸。2.计算器法,先输入欲缩放宽度,按除键,再输入原稿尺

    相关 Glide 图片

    CenterCrop CenterCrop()会缩放图像,完全填充到 ImageView 界限内并且裁剪额外的部分,图像可能不会完整显示。 Glide.with