响应式图像地图
我有一个响应式 html 布局中的现有图像映射。图像根据浏览器大小进行缩放,但图像坐标显然是固定的像素大小。我可以使用哪些选项来调整图像映射坐标的大小?
I have an existing image map in a responsive html layout. Images scale according to browser size, but the image coordinates are obviously fixed pixel sizes. What options do I have to resize the image map coordinates?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
对于响应式图像地图,您需要使用插件:
https://github。 com/stowball/jQuery-rwdImageMaps (不再维护)
或
https://github.com/davidjbradshaw/imagemap-resizer
http://www.howtocreate.co.uk/tutorials/html/imagemaps
此页面还用于测试浏览器是否实现
http://home.comcast.net/~urbanjost/IMG/resizeimg3.html
For responsive image maps you will need to use a plugin:
https://github.com/stowball/jQuery-rwdImageMaps (No longer maintained)
Or
https://github.com/davidjbradshaw/imagemap-resizer
http://www.howtocreate.co.uk/tutorials/html/imagemaps
And also this page for testing whether browsers implement
http://home.comcast.net/~urbanjost/IMG/resizeimg3.html
您还可以使用 SVG 代替图像映射。有教程介绍如何执行此操作。下面的示例是从 此 jsfiddle 导入的。
You can also use SVG instead of an image map. There is a tutorial on how to do this. Sample below, imported from this jsfiddle.
我找到了一种非 JS 的方法来解决这个问题。除非您更改边框半径以形成圆形或椭圆形区域,否则命中区域将是矩形的。
首先,确保您的图像位于相对定位的 div 中。然后将图像放入这个div中,这意味着它将占据div中的所有空间。最后,在图像下方的主 div 内添加绝对定位的 div,并使用顶部、左侧、宽度和高度的百分比来获得链接点击区域所需的大小和位置。
我发现当你第一次工作时,最简单的方法是给 div 一个黑色背景颜色(最好有一些 alpha 淡入淡出,这样你就可以看到下面的链接内容),并使用浏览器中的代码检查器来实时调整百分比,这样您就可以得到正确的结果。
这是您可以使用的基本大纲。通过使用百分比进行所有操作,您可以确保所有元素都保持与图像缩放相同的相对大小和位置。
将此代码与 Chrome 中的代码检查器或您选择的浏览器一起使用,并调整百分比(您可以使用小数百分比更准确),直到框恰到好处。当您准备使用它时,还要选择
透明
的背景颜色
,因为您希望点击区域不可见。I found a no-JS way to address this. The hit areas will be rectangular unless you change the border-radius to make circular or oval areas.
First of all, make sure your image is in a div that's relatively positioned. Then put the image inside this div, which means it'll take up all the space in the div. Finally, add absolutely positioned div's under the image, within the main div, and use percentages for top, left, width, and height to get the link hit areas the size and position you want.
I find it's easiest to give the div a black background color (ideally with some alpha fading so you can see the linked content underneath) when you're first working, and to use a code inspector in your browser to adjust the percentages in real time, so that you can get it just right.
Here's the basic outline you can work with. By doing everything with percentages, you ensure the elements all stay the same relative size and position as the image scales.
Use this code with your code inspector in Chrome or your browser of choice, and adjust the percentages (you can use decimal percentages to be more exact) until the boxes are just right. Also choose a
background-color
oftransparent
when you're ready to use it since you want your hit areas to be invisible.响应式图像地图 jQuery 插件 作者:Matt Stow
Responsive Image Maps jQuery Plugin by Matt Stow
我遇到了一个解决方案,它根本不使用图像映射,而是使用绝对定位在图像上的锚标记。唯一的缺点是热点必须是矩形的,但优点是该解决方案不依赖 Javascript,只依赖 CSS。您可以使用一个网站来生成锚点的 HTML 代码: http://www .zaneray.com/responsive-image-map/
我将图像和生成的锚标记放在相对定位的 div 标记中,并且在窗口大小调整和我的手机上一切都完美运行。
I ran across a solution that doesn't use image maps at all but rather anchor tags that are absolutely positioned over the image. The only drawback would be that the hotspot would have to be rectangular, but the plus is that this solution doesn't rely on Javascript, just CSS. There is a website that you can use to generate the HTML code for the anchors: http://www.zaneray.com/responsive-image-map/
I put the image and the generated anchor tags in a relatively positioned div tag and everything worked perfectly on window resize and on my mobile phone.
以下方法非常适合我,所以这是我的完整实现:
The following method works perfectly for me, so here's my full implementation:
David Bradshaw 编写了一个很好的小库来解决这个问题。它可以与或不与 jQuery 一起使用。
此处可用:https://github.com/davidjbradshaw/imagemap-resizer
David Bradshaw wrote a nice little library that solves this problem. It can be used with or without jQuery.
Available here: https://github.com/davidjbradshaw/imagemap-resizer
考虑使用此图像映射器
https://imagemapper.noc.io/#/ | github | 替代链接
它基于 SVG,响应灵敏,并且
有很好的向导来构建地图,
对于页面来说足够简单因为它不使用库
consider to use this image mapper
https://imagemapper.noc.io/#/ | github | alternative link
its SVG based and its responsive and
it have good wizards to build the map
its simple enough for the page because it use no library
为我工作(记得在代码中更改 3 件事):
previousWidth(图像的原始大小)
map_ID(图像地图的 ID)
img_ID (图像的 ID)
HTML:
Javascript:
JSFiddle: http://jsfiddle.net/p7EyT/154/
Working for me (remember to change 3 things in code):
previousWidth (original size of image)
map_ID (id of your image map)
img_ID (id of your image)
HTML:
Javascript:
JSFiddle: http://jsfiddle.net/p7EyT/154/
查看 Github 上的 image-map 插件。它既可以与普通 JavaScript 一起使用,也可以作为 jQuery 插件使用。
查看演示。
Check out the image-map plugin on Github. It works both with vanilla JavaScript and as a jQuery plugin.
Check out the demo.
我遇到了同样的要求,我想显示响应式图像地图,它可以根据任何屏幕尺寸调整大小,重要的是,我想突出显示该坐标。
所以我尝试了很多可以根据屏幕尺寸和事件调整坐标大小的库。我得到了最好的解决方案(jquery.imagemapster.min.js),它几乎适用于所有浏览器。我还将它与创建图像映射的 Summer Plgin 集成。
希望对某人有帮助。
I come across with same requirement where, I wants to show responsive image map which can resize with any screen size and important thing is, i want to highlight that coordinates.
So i tried many libraries which can resize coordinates according to screen size and event. And i got best solution(jquery.imagemapster.min.js) which works fine with almost all browsers. Also i have integrated it with Summer Plgin which create image map.
Hope help it to someone.
对于那些不想使用 JavaScript 的人,这里有一个图像切片示例:
http://codepen.io /anon/pen/cbzrK
当您缩放窗口时,小丑图像将相应缩放,并且当缩放时,小丑的鼻子仍保持超链接。
For those who don't want to resort to JavaScript, here's an image slicing example:
http://codepen.io/anon/pen/cbzrK
As you scale the window, the clown image will scale accordingly, and when it does, the nose of the clown remains hyperlinked.
这取决于,我认为你可以使用 jQuery 按比例调整范围。
顺便问一下,为什么要使用图像映射?不能使用缩放 div 或其他元素吗?
It depends, you can use jQuery to adjust the ranges proportionally I think.
Why do you use an image map by the way? Can't you use scaling divs or other elements for it?
类似于奥兰的回答:
https://stackoverflow.com/a/32870380/462781
与 Chris 的代码相结合:
https://stackoverflow.com/a/12121309/462781
如果区域适合网格,您可以通过以下方式覆盖区域:透明图片使用以百分比为单位的宽度来保持其纵横比。
您可以使用百分比形式的保证金。此外,“空间”图像可以在第三级 div 内彼此相邻放置。
Similar to Orland's answer here:
https://stackoverflow.com/a/32870380/462781
Combined with Chris' code here:
https://stackoverflow.com/a/12121309/462781
If the areas fit in a grid you can overlay the areas by transparent pictures using a width in % that keep their aspect ratio.
You can use a margin in %. Additionally "space" images can be placed next to each other inside a 3rd level div.
由于某种原因,这些解决方案都不适合我。我在使用转换方面取得了最大的成功。
For some reason none of these solutions worked for me. I've had the best success using transforms.
我已经创建了 Tom Bisciglia 建议的解决方案的 JavaScript 版本。
我的代码允许您使用普通的图像映射。您所要做的就是加载几行 CSS 和几行 JS,然后...BOOM...您的图像地图具有悬停状态并且完全响应!魔法对吧?
I have created a javascript version of the solution Tom Bisciglia suggested.
My code allows you to use a normal image map. All you have to do is load a few lines of CSS and a few lines of JS and... BOOM... your image map has hover states and is fully responsive! Magic right?
响应宽度&&高度
responsive width && height