【问题标题】:By using "onmouseover" is it possible to stack a transparent png on a map?通过使用“onmouseover”是否可以在地图上堆叠透明 png?
【发布时间】:2015-08-08 03:45:10
【问题描述】:

很抱歉打扰了一些可能完成起来很愚蠢的事情,但我被困住了。 我已经尝试了很多方法来让“鼠标悬停”在地图图像上堆叠一张透明图片,但我做错了。 我已经放弃了使用 PNG 图像的可能性,我使用 JPG 图片来制作我的地图,但结果很差,因为我正在制作一个巨大的国家地图,并且压缩 8 位 PNG 可以节省很多在这种情况下的空间,与压缩的 JPG 相比,后者丑陋且尺寸更大(至少在这种情况下)。 顺便说一句,这里是一个示例地图的代码,使用 Javascript 来调整它的大小。 请注意,JS 在小提琴中不起作用,但它对我的原始地图起作用。我不得不把它留下来说明清楚,因为我真的需要它,任何可能的解决方案都应该考虑到它。

代码如下:

<area id="planA" alt="" title="planA" href="#" shape="rect" coords="184,122,408,322" style="outline:none;" target="_self"  onmouseover="if(document.images) document.getElementById('choices-map-img').src= 'http://www.clker.com/cliparts/a/5/8/e/1298546227709322759png-transparency.png';" onmouseout="if(document.images) document.getElementById('choices-map-img').src= 'http://blogs-images.forbes.com/jacobmorgan/files/2015/03/Choice.jpg';"  />

<area id="planB" alt="" title="planB" href="#" shape="rect" coords="572,125,796,318" style="outline:none;" target="_self"  onmouseover="if(document.images) document.getElementById('choices-map-img').src= 'http://www.clker.com/cliparts/a/5/8/e/1298546227709322759png-transparency.png';" onmouseout="if(document.images) document.getElementById('choices-map-img').src= 'http://blogs-images.forbes.com/jacobmorgan/files/2015/03/Choice.jpg';"  />
</map>

Here's the fiddle with JS.

总之,我想看到的是背景图像上的骰子,透明效果很好。 非常感谢任何帮助。

【问题讨论】:

  • 正如@TomMon-Tom 发布的那样,使用 png 来获得透明度。 JPG 也没有 alpha 通道,所以你不可能逃脱。
  • 也许描述有误导性,但我知道我应该使用 PNG 图像,问题是鼠标悬停在完全切换图像,而正如我所问的,我需要它在顶部是透明的背景。 :/ 你知道我该怎么做吗?
  • 试试小提琴,它在 JPEG 之上使用 PNG,但图片是用这种方法交换的。如果你知道如何把它叠在以前的上面,我会很高兴听到的。就是这样。
  • @friends.call.memcyborg 好的,现在得到你想要的了。所以我想我知道这是怎么可能的。

标签: javascript html css transparency onmouseover


【解决方案1】:

PNG 是一种可行的方法,因为它们可以包含 Alpha 通道的数据,这与任何其他图像格式不同。为什么它们需要是 JPG?您说自己 8 位 PNG 在某些情况下会节省空间。如果是我,我会将它们更改为 PNG。

【讨论】:

  • 这不能回答问题。 也许描述有误导性,但我知道我应该使用 PNG 图像,问题是鼠标悬停在完全切换图像时,正如我所问的,我需要它在背景之上是透明的。 :/ 你知道我应该怎么做才能让它工作吗?否则,请在回答之前仔细阅读...
【解决方案2】:

这可以通过将图像一个接一个地堆叠来完成。我还需要 JQuery 库来使用鼠标悬停和鼠标悬停。

DEMO

$( "#planA" ).mouseover(function() {
    $("#plan_a").css("display","block");
    $("#plan_a").css("width","1000");    
  })
  .mouseout(function() {
     $("#plan_a").css("display","none");
  });

$( "#planB" ).mouseover(function() {
    $("#plan_b").css("display","block");
    $("#plan_b").css("width","1000");    
  })
  .mouseout(function() {
     $("#plan_b").css("display","none");
  });
.main
{
	display:block;
	width:100%;
}

.main img
{
	float:left;
	left:0px;
	position:absolute;
	width:1000;
}

#plan_a, #plan_b
{
	display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="main">
	<img id="main_img" src="http://blogs-images.forbes.com/jacobmorgan/files/2015/03/Choice.jpg" usemap="#choices" alt=""/>
	<map name="choices" id="choices-id">
		<area id="planA" alt="" title="planA" href="#" shape="rect" coords="184,122,408,322" style="outline:none;" target="_self" />
		<area id="planB" alt="" title="planB" href="#" shape="rect" coords="572,125,796,318" style="outline:none;" target="_self" /> 
 	</map> 
	<img id="plan_a" src="http://www.clker.com/cliparts/a/5/8/e/1298546227709322759png-transparency.png" usemap="#choices" />
	<img id="plan_b" src="http://www.fnordware.com/superpng/pnggradHDrgba.png" usemap="#choices" />
</div>

注意:图像的堆叠可以有多种方式,我的就是这个。去 任何你喜欢的适合你的东西。

【讨论】:

  • 这没有考虑到小提琴中的 JS(你删除了它)它可以工作,好的,但是没有调整大小,它已经解决了一半。 :/ 上面写着 “任何可能的解决方案都应该考虑到它。” :P
  • 顺便说一句,没关系,我可以尝试自己解决这个问题 ^_^" 非常感谢您的努力,我真的很感激 :)
  • 我通过在 css 和 javascript 中添加 100% 来解决大小调整问题。现在很完美,我不知道如何感谢你,你太棒了。 :D 你应该在个人资料的某处放置一个捐赠按钮。 ;)
  • 很高兴,您发现它很有用。
猜你喜欢
  • 2016-08-20
  • 2014-03-08
  • 2012-02-07
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多