html - 如何用PNG图像制作圆形按钮?

标签 html css

我正在构建一个包含大量图片的网站。这个概念是一个星系,所以你可以想象我有许多圆形行星,我想让它们成为可点击的按钮。

这些行星是具有透明背景的 PNG 格式,我希望可点击区域仅是非透明区域(即圆形)。但是,我还没有找到可行的解决方案。

我也试过在图片上面放一个透明圆圈,然后放<a href>在透明圆圈上而不是图像上,但这似乎也不起作用。

更糟糕的是我有重叠的图像,这可能会导致我发现某些解决方案不起作用。例如,我有两个或三个重叠的图像,我希望它们都是一个按钮(链接到不同的页面)(并且我在其背景中有另一个图像)所以我不知道如果我点击交叉点会发生什么这些按钮。

我尝试过的一些解决方案是:

http://jsfiddle.net/josedvq/Jyjjx/45/

http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_areamap

http://jsfiddle.net/DsW9h/

http://bryanhadaway.com/how-to-create-circles-with-css/

我的代码片段:

HTML

<div>
<a href="~/SomePage">
    <img draggable="false" class="AIcon" src="~/Content/Aicon.png" id="AI">
</a>
</div>

CSS

.AIcon{
position:absolute; left: 50%; top: 40%; width: 2.5%; height:5%; opacity: 1;
-webkit-animation: AAAIcon .5s linear 0s 1 normal forwards running;
}

@-webkit-keyframes AAAIcon {
0%  {left: 50%; top: 40%; width: 2.5%; height:5%; opacity: 0; z-index:4;}
100%  {left: 78%; top: 20%; width: 32%; height:32%; opacity: 1; z-index:4;} 
}

就像现在一样,图片的整个正方形内都可以点击,包括透明区域,但并不是所有区域都可以点击(图片中有一些小块是不可点击的)。

这让我抓狂。任何指示都会非常有帮助。

最佳答案

你可以通过三种方式做到这一点:

1- 在下面的代码片段中,我在第一个月亮的图像 div 中使用了一个 css 圆圈。

2- 或者,在第二个月亮上将圆放在 div:after 上得到相同的结果。

3- 第三种方法与第二种方法完全相反:创建一个透明圆圈并让月亮图像位于 :after 上。

第一种和第三种方法允许您使用月亮作为 onclick javascript 鼠标事件的链接。红色元素设置为 pointer-events: none; 因此它对卫星的悬停没有影响。

body {
margin:0px;
background: black;
overflow: hidden;
}

#circle1 {  
  width: 200px;
  height: 200px;
  background: purple;
  -moz-border-radius: 100px;
  -webkit-border-radius: 100px;
  border-radius: 100px;
  cursor: pointer;
  opacity: 0.2;
}

#image1 {
  display: inline-block;
  width: 200px;
  height: 200px;
  position: relative;
  background: url('http://i.imgur.com/YAWvTuu.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

#image2 {
  display: inline-block;
  width: 200px;
  height: 200px;
  position: relative;
  background: url('http://i.imgur.com/YAWvTuu.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

#image2:after {
  content:"";
  display: inline-block;
  width: 200px;
  height: 200px;
  background: orange;
  -moz-border-radius: 100px;
  -webkit-border-radius: 100px;
  border-radius: 100px;
  cursor: pointer;
  opacity: 0.2;
}

#inactive {
  background: tomato;
  position:absolute;
  top:50px;
  left: 50px;
  height:50px;
  width: 400px;
  pointer-events: none;
  opacity: 0.9;
}

#third {
  position:absolute;
  display: inline-block;
  width: 200px;
  height: 200px;
  background: transparent;
  -moz-border-radius: 100px;
  -webkit-border-radius: 100px;
  border-radius: 100px;
  cursor: pointer;
}

#third::after {
content: url('http://i.imgur.com/YAWvTuu.png');
cursor: auto;
pointer-events: none;
}
<div id="image1" alt=image><div id="circle1" onClick="window.location.href = 'http://www.google.com'"></div></div>
<div id="image2" alt=image></div><div id=third class="circle" alt=image onClick="window.location.href = 'http://www.google.com'"></div>
<div id=inactive></div>

关于html - 如何用PNG图像制作圆形按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35760356/

相关文章:

javascript - ASP MVC 中的自定义是/否单选按钮

javascript - 带有水平和垂直滚动的固定主题的表格

html - 如何在 Safari 中打开 phonegap 子浏览器中显示的链接?

javascript - 是否可以创建当鼠标悬停在 <p> 标记内的文本上时出现的工具提示?

html - 在边框后面显示文本

javascript - 如何通过点击缩略图显示/隐藏大图?

javascript - 使用javascript动态控制多个潜水风格的风格,使用类

html - < A > TAG 悬停时出现奇怪的问题

php - 如何使用 CSS [Img Inside] 有效地将列表的各个部分居中

javascript - 如果用户输入 HTML5 URL,AngularJS 返回 404,如果重定向到同一 View ,则工作正常