可点击的图像区域 - 即使屏幕更改大小为html

问题描述 投票:0回答:2

我正在努力学习如何用HTML创建一个简单的网站。目前我已经创建了一个背景图像,图像上有多个形状。我希望图像的不同部分是可点击的链接。我理解如何找到坐标并使用图像映射,但是当我更改屏幕大小时,可点击链接不起作用。如何使可点击区域适用于不同的屏幕尺寸?

body, html {
        height: 100%;
        margin: 0;
    }
    
    .bg {
        background-position: left;
        background-repeat: no-repeat;
        background-size: 100% 100%;
    }
    <div class="bg"></div>
    <body>
    <img src="https://cdn.pixabay.com/photo/2018/01/24/18/05/background-3104413_1280.jpg" width="100%" height="100%" usemap="workmap" class="bg">
    <map name="workmap">
        <area target="_blank" alt="Game1" title="Game1" href="game1.html" coords="243,133,79" shape="circle">
        <area target="_blank" alt="Game2" title="Game2" href="game2.html" coords="870,147,680,33" shape="rect">
        <area target="_blank" alt="Game3" title="Game3" href="game3.html" coords="889,379,80" shape="circle">
        <area target="_blank" alt="CS" title="CS" href="https://code.org/educate/csp " coords="770,671,73" shape="circle">
        <area target="_blank" alt="Game4" title="Game4" href="game4.html" coords="163,587,214,492,267,473,335,483,377,603,327,631,249,658,211,641" shape="poly">
    </map>

谢谢!

html css image coordinates imagemap
2个回答
1
投票

Why the <map> approach is not the best way:

在HTML页面中使用HTML图像<map> / <area>系统有许多缺点。最值得注意的是,当图像本身(应该)基于客户端屏幕大小可伸缩和动态时,需要将与图像相关的可点击元素调整为任何大小的显示的过程,这里根本不存在。

由于HTML <map>元素的大小和大小都是绝对的,因此它们不适用于动态大小的内容(width:80%等)。

What can you do instead?

有几个选择。你可以找到一些Javascript systems,它会在检测到窗口(重新)大小时动态调整<map>区域边界的大小。这些当然会增加一些开销以及JS膨胀到页面加载。

或等待它,有一个鼓声即将来临......你能听到吗?

USE SVGs

是的 - 可缩放矢量图形是 未来 目前关于图像映射点击没有Javascript开销,你可以在their wikion MDN上阅读它们。

因此,使用SVG可以导入标准图像格式(例如JPG等),然后使用锚点和可点击元素覆盖它,您可以使用类似CSS的样式设置样式,这比旧的<map>提供了更多的支持和可能性语法,例如淡入淡出,悬停,混合和模糊,由于用户交互,在任何设定点,在任何大小的屏幕上发生在静态图像上。

Show me How!

强烈建议this tutorial在HTML图像元素上制作SVG可点击区域地图。


(链接突出显示用于说明目的)

#backing {
	vertical-align: middle;
	margin: 0;
	overflow: hidden;
}
#backing svg { 
	display: inline-block;
	position: absolute;
	top: 0; left: 0;
}
    <figure id='backing'>
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1280 504" preserveAspectRatio="xMinYMin meet" >
    	<image width="1280" height="504" xlink:href="https://cdn.pixabay.com/photo/2018/01/24/18/05/background-3104413_1280.jpg">
    	</image>
   <a xlink:href="game1.html"><circle cx="243" cy="133" r="79" fill="#fff" opacity="0.15" /></a>
  <a xlink:href="game2.html"><rect x="870" y="147" width="680" height="33" fill="#fff" opacity="0.25"/></a>
  <a xlink:href="game3.html"><circle cx="889" cy="379" r="80" fill="#fff" opacity="0.1"/></a>
  <a xlink:href="https://code.org/educate/csp"><circle cx="770" cy="671" r="73" fill="#fff" opacity="0.2"/></a>
  <a xlink:href="game4.html"><polygon id="test" points="163,587 214,492 267,473 335,483 377,603 327,631 249,658 211,641" fill="#fff" opacity="0.3"/></a>
    	</svg>
      </figure>

1
投票

我同意@Martin。这里最好的选择是SVG。您的SVG可能如下所示:(我正在使用您的坐垫。)

*{margin:0;padding:0;}
svg{width:100vh;border:1px solid;}
svg *{fill:rgba(0, 255, 255, .4)}
<svg viewBox="0 0 1800 1800">
   <image xlink:href="https://cdn.sstatic.net/Sites/stackoverflow/img/404.svg" width="100%"  />
  <a xlink:href="https://stackoverflow.com"><circle cx="243" cy="133" r="79" /></a>
  <a xlink:href="https://stackoverflow.com"><rect x="870" y="147" width="680" height="33" /></a>
  <a xlink:href="https://stackoverflow.com"><circle cx="889" cy="379" r="80" /></a>
  <a xlink:href="https://stackoverflow.com"><circle cx="770" cy="671" r="73" /></a>
  <a xlink:href="https://stackoverflow.com"><polygon id="test" points="163,587 214,492 267,473 335,483 377,603 327,631 249,658 211,641" /></a>
  
</svg>     
© www.soinside.com 2019 - 2024. All rights reserved.