如何制作具有鼠标悬停高亮效果的响应式图像映射?

huangapple go评论66阅读模式
英文:

How to make responsive imagemap WITH highlight on mouseover?

问题

I need to make responsive imagemap with highlight when the mouse is over the specific part of the image. Can it be done (and how) using ONLY JavaScript, html and CSS ? I found some threads here but only for the part with the responsive imagemap and they used some jQuery plugins. Please, help 如何制作具有鼠标悬停高亮效果的响应式图像映射?

I used image-map.net but that didnt resolve the problem.

<section>
  <div>
    <img id="Image-Maps-Com-image-maps-2023-03-03-153207" src="https://www.linkpicture.com/q/A1C-PBOBiFL._AC_SL1500.jpg" width="1396" height="940" orgWidth="1396" orgHeight="940" usemap="#image-maps-2023-03-03-153207" alt="" />
    <map name="image-maps-2023-03-03-153207" id="ImageMapsCom-image-maps-2023-03-03-153207">
      <area shape="rect" coords="1394,938,1396,940" alt="Image Map" style="outline:none;" title="Image Map" href="brazil.html" />
      <area alt="" title="" href="brazil.html" shape="poly" coords="477,473,499,484,509,501,539,513,561,522,551,552,538,590,512,597,478,593,452,600,437,596,428,581,418,571,394,545,384,521,389,501,397,477,401,462,412,449,443,455" style="outline:none;" target="_self" onmouseover="function highlight() {
        'some code here for highlighting'
      }" />
    </map>
  </div>
</section>
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  overflow: hidden;
  min-height: 100%;
  min-width: 100%;
}

section img {
  position: fixed;
  flex-shrink: 0;
  width: 100vw;
  height: 100vh;
}

(请注意,JavaScript 部分未提供,需要您自行添加以实现鼠标悬停时的高亮效果。)

英文:

I need to make responsive imagemap with highlight when the mouse is over the specific part of the image. Can it be done (and how) using ONLY JavaScript, html and CSS ? I found some threads here but only for the part with the responsive imagemap and they used some jQuery plugins. Please, help 如何制作具有鼠标悬停高亮效果的响应式图像映射?

I used image-map.net but that didnt resolve the problem.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  overflow: hidden;
  min-height: 100%;
  min-width: 100%;
}

section img {
  position: fixed;
  flex-shrink: 0;
  width: 100vw;
  height: 100vh;
}

<!-- language: lang-html -->

&lt;section&gt;
  &lt;div&gt;
    &lt;img id=&quot;Image-Maps-Com-image-maps-2023-03-03-153207&quot; src=&quot;https://www.linkpicture.com/q/A1C-PBOBiFL._AC_SL1500.jpg&quot; width=&quot;1396&quot; height=&quot;940&quot; orgWidth=&quot;1396&quot; orgHeight=&quot;940&quot; usemap=&quot;#image-maps-2023-03-03-153207&quot; alt=&quot;&quot; /&gt;
    &lt;map name=&quot;image-maps-2023-03-03-153207&quot; id=&quot;ImageMapsCom-image-maps-2023-03-03-153207&quot;&gt;
            &lt;area shape=&quot;rect&quot; coords=&quot;1394,938,1396,940&quot; alt=&quot;Image Map&quot; style=&quot;outline:none;&quot; title=&quot;Image Map&quot; href=&quot;brazil.html&quot; /&gt;
            &lt;area  alt=&quot;&quot; title=&quot;&quot; href=&quot;brazil.html&quot; shape=&quot;poly&quot; coords=&quot;477,473,499,484,509,501,539,513,561,522,551,552,538,590,512,597,478,593,452,600,437,596,428,581,418,571,394,545,384,521,389,501,397,477,401,462,412,449,443,455&quot; style=&quot;outline:none;&quot; target=&quot;_self&quot;
            onmouseover=&quot;function highlight(){
            &#39;some code here for highlighting&#39;}&quot;    /&gt;
            &lt;/map&gt;
&lt;/section&gt;

<!-- end snippet -->

答案1

得分: 2

你最好的选择可能是使用SVG。您可以使其可访问,响应式,并且可以轻松使用CSS进行样式设计。

如何创建响应式和可访问的SVG地图:

  1. 将SVG的viewBox设置为图像的自然宽度和高度(例如,如果您的地图宽度为1396,高度为940,那么将viewBox设置为viewBox="0 0 1396 940")。
  2. 将SVG的高度和宽度都设置为100%,以便填充放置它的容器并保持其宽高比。
  3. 确保将图像标签的高度和宽度属性都设置为100%,以便SVG中的图像填充SVG的viewBox
  4. 在图像后面添加一个<polygon />,这样在渲染时它就位于地图图像的上方。
  5. points属性设置为图像地图中区域多边形的相同值。
  6. 将多边形的fill属性设置为transparent,以便可以进行悬停操作。
  7. 在JavaScript中使用onclick属性或事件监听器创建点击处理程序。
  8. 使用CSS进行样式设计。
  9. 要使多边形可访问,添加一个<title>并将其和多边形包装在自己的组内(例如:
<g>
  <title id="title" lang="en">点击访问关于巴西的页面</title>
  <polygon ... />
</g>

工作示例:

<main>
  <h1>将鼠标悬停/点击巴西</h1>
  <svg viewBox="0 0 1396 940" width="100%" height="100%">
    <image x="0" y="0" width="100%" height="100%" xlink:href="https://www.linkpicture.com/q/A1C-PBOBiFL._AC_SL1500.jpg" />
    <polygon onclick="gotolink('brazil.html')" points="477,473,499,484,509,501,539,513,561,522,551,552,538,590,512,597,478,593,452,600,437,596,428,581,418,571,394,545,384,521,389,501,397,477,401,462,412,449,443,455" fill="transparent" stroke-miterlimit="10" />
  </svg>
</main>
英文:

Your best bet would probably be an SVG. You can make it accessible, responsive and you can style it easily with CSS.

How to create a responsive and accessible SVG map:

  1. Set the SVG viewBox to the natural width and height of your image (e.g. your map had a width of 1396 and a height of 940, so I set the viewBox to viewBox=&quot;0 0 1396 940&quot;).
  2. Set the SVG height and width to 100% so that it fills whatever container you put it in and retain its aspect ratio.
  3. Make sure to set the image tag height and width properties both to 100% so that the image in the SVG fills the SVG viewBox.
  4. Add a &lt;polygon /&gt; after the image so it is on top of the map image when rendered.
  5. Set the points property to the same values as your area poly in your image map.
  6. Set the fill property of the poly to transparent so that it is hoverable.
  7. Use an onclick property or event listener in your JavaScript to create the click handler.
  8. Use CSS to style it as you want.
  9. To make the polygon accessible add a &lt;title&gt; and wrap both it and the polygon in its own group (&lt;g&gt;), such as:
&lt;g&gt;
  &lt;title id=&quot;title&quot; lang=&quot;en&quot;&gt;Click to visit a page about Brazil&lt;/title&gt;
  &lt;polygon ... /&gt;
&lt;/g&gt;

Working example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function gotolink(link) {
  // use window.location or similar to go to the link
  console.log(link);
}

<!-- language: lang-css -->

main {
  max-width: 60rem;
  margin: auto;
}

polygon:hover {
  stroke: hsl(320 80% 50%);
  stroke-width: 2px;
  fill: hsl(320 80% 50% /0.5);
}

<!-- language: lang-html -->

&lt;main&gt;
  &lt;h1&gt;Hover over/click on Brazil&lt;/h1&gt;
  &lt;svg viewBox=&quot;0 0 1396 940&quot; width=&quot;100%&quot; height=&quot;100%&quot;&gt;
    &lt;image x=&quot;0&quot; y=&quot;0&quot; width=&quot;100%&quot; height=&quot;100%&quot; xlink:href=&quot;https://www.linkpicture.com/q/A1C-PBOBiFL._AC_SL1500.jpg&quot; /&gt;
    &lt;polygon onclick=&quot;gotolink(&#39;brazil.html&#39;)&quot; points=&quot;477,473,499,484,509,501,539,513,561,522,551,552,538,590,512,597,478,593,452,600,437,596,428,581,418,571,394,545,384,521,389,501,397,477,401,462,412,449,443,455&quot; fill=&quot;transparent&quot; stroke-miterlimit=&quot;10&quot; /&gt;
  &lt;/svg&gt;
&lt;/main&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月3日 22:42:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定