简体   繁体   English

在HTML / CSS中分层PNG?

[英]Layering PNG in HTML/CSS?

I want to make an interactive map of the world using HTML, CSS, and JS. 我想使用HTML,CSS和JS制作一个交互式的世界地图。 Using SVG was probably the recommended way to go, but to be honest I have no experience in utilizing it. 推荐使用SVG,但老实说,我没有使用SVG的经验。 Plus editing paths for doing minor changes is a pain. 加上编辑路径以进行较小的更改是很痛苦的。 I really prefer exporting each country as a PNG image, placing them in HTML, and JS will handle all the interaction. 我真的更喜欢将每个国家/地区都导出为PNG图像,并以HTML格式放置,而JS将处理所有互动。

It's really easy in concept. 概念上真的很容易。 The problem however is that how to layer these different PNGs so that each countries will be on its specific place? 但是,问题在于如何对这些不同的PNG进行分层,以便每个国家都位于其特定位置? If you use two <img src=... it would either put the images into a column or a row: 如果使用两个<img src=... ,它将把图像放入一列或一行:

This is how it should be 这应该是这样 应该如何

But this is how it will show up in HTML 但这就是它在HTML中的显示方式 在此处输入图片说明

or this 或这个 在此处输入图片说明

  • Are there any ways I could stack these images together without putting them into a column or row? 有什么方法可以将这些图像堆叠在一起而无需将它们放入列或行中?
  • If not, is it possible to track a cursor's position within an image? 如果不是,是否可以在图像中跟踪光标的位置? If putting the countries individually on a PNG won't work, I'm planning to export the whole map of the world into one PNG and then monitor what is the cursor's position when clicking the image so I could get the specific country the user clicked on. 如果将各个国家/地区分别放在PNG上不起作用,我打算将整个世界地图导出到一个PNG中,然后在单击图像时监视光标的位置,以便获得用户单击的特定国家/地区上。 Is that possible? 那可能吗?

Forgive me for being too naive of this. 原谅我太天真了。 Thanks a ton! 万分感谢!

For this case, I would use position:absolute on the images, and use the left, right, top, bottom properties to align them how you like. 对于这种情况,我将在图像上使用position:absolute ,并使用left,right,top,bottom属性将其对齐。 Just be sure to set position:relative to your parent div. 只要确保将position:relative对于您的父div设置即可。

If this is not possible for whatever reason, you can use the transform property to set the horizontal and vertical position. 如果由于某种原因无法实现,则可以使用transform属性设置水平和垂直位置。

As @rotateDev said, absolute positioning each images with proper offset is the best solution if you are decided to go with PNG images. 正如@rotateDev所说,如果决定使用PNG图像,则以正确的偏移量绝对定位每个图像是最好的解决方案。 But clicking on each country will definitely be an issue because of the random shapes of the countries. 但是,由于每个国家/地区的形状随机,因此点击每个国家/地区绝对是一个问题。

A solution for this is implement countries as background image of individual <DIV> and place a label inside the div with country name. 解决方案是将国家/地区作为单个<DIV>背景图像,并在div内放置带有国家/地区名称的标签。 Only this labels will be clickable. 仅此标签将是可单击的。

HTML Sample HTML样本

<div class="country gernany">
   <a href="#">Germany</a>
</div>
<div class="country france">
   <a href="#">France</a>
</div>
<div class="country italy">
   <a href="#">Italy</a>
</div>

CSS sample: CSS示例:

.germany{
   .background-image: url(germany.png);
}
.germany.active{
   .background-image: url(germany-active.png);
}

We can use JavaScript to append some class to the <DIV> on the hover or click of the label so we can highlight the country on hover of the label. 我们可以使用JavaScript 将某些类添加到 悬停或单击标签上 <DIV> 上,以便在标签悬停时突出显示国家/地区。

<script>
$('.country').click(function(){
   $(this).addClass('active');
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM