【发布时间】:2019-05-06 11:23:09
【问题描述】:
我正在尝试为按钮创建六边形边框。我的策略是使容器元素比按钮大几个像素,并在两个元素上使用相同的clip-mask。大小的差异应该会产生边框效果。这适用于 Firefox 和 Chrome,但不适用于 Safari。
document.getElementById("button").addEventListener("click", function(){alert("foo"); return false;});
div {
width: 9rem;
height: 8rem;
position: relative;
clip-path: url("#hexagon");
-webkit-clip-path: url("#hexagon");
background-color: #e2e3e5;
}
button {
position: absolute;
cursor: pointer;
top: 2px;
left: 2px;
padding: calc(8rem * 0.1) calc(9rem * 0.2);
width: calc(9rem - 4px);
height: calc(8rem - 4px);
clip-path: url("#hexagon");
-webkit-clip-path: url("#hexagon");
background-color: white;
border: none;
}
<div id="div">
<button id="button">The button</button>
</div>
<svg width="0" height="0" xmlns="http://www.w3.org/2000/svg">
<defs>
<clipPath id="hexagon" clipPathUnits="objectBoundingBox">
<polygon points=".25 0, .75 0, 1 .5, .75 1, .25 1, 0 .5"/>
</clipPath>
</defs>
</svg>
如果没有-webkit-clip-path,边框在 Safari 中是一个矩形。添加后,生成的六边形的宽度比其他浏览器中的要大得多。看来剪辑路径覆盖的按钮比应有的要多得多。有什么办法可以解决这个问题,让它在 Safari 中运行良好?
【问题讨论】:
标签: html css svg safari clip-path