无法在Safari中应用SVG剪切路径

时间:2018-12-04 23:37:02

标签: html css svg clip-path

我正在尝试为按钮创建一个六边形边框。我的策略是使容器元素比按钮大几个像素,并在两个元素上使用相同的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中正常工作?

Firefox:Button in Firefox Safari:Button in Safari

2 个答案:

答案 0 :(得分:5)

您可以使用clip-path: url()来代替clip-path:polygon() 我用您的点乘以10,然后在x上偏移了15,在y上偏移了5。 另外,由于“边框”几乎看不到,因此我正在缩放按钮。

document.getElementById("button").addEventListener("click", function(){alert("foo"); return false;});
div {
  width: 8rem;
  height: 6.6rem;
  position: relative;
  background-color: #ccc;
  -webkit-clip-path:polygon(40px 5px, 90px 5px, 115px 55px, 90px 105px,40px 105px, 15px 55px);
  clip-path:polygon(40px 5px, 90px 5px, 115px 55px, 90px 105px,40px 105px, 15px 55px);
}

button {
  position: absolute;
  cursor: pointer;
  text-align:center;
  width: 8rem;
  height: 6.6rem;
  -webkit-clip-path:polygon(40px 5px, 90px 5px, 115px 55px, 90px 105px,40px 105px, 15px 55px);
  clip-path:polygon(40px 5px, 90px 5px, 115px 55px, 90px 105px,40px 105px, 15px 55px);
  background-color: #fff;
  border:none;
  transform:scale(.97);
}
<div id="div">
<button id="button">The button</button>
</div>

另一个选择是使用svg元素而不是按钮。同样,对于六角形,我使用的是您的点乘以10。我个人更喜欢此选项。

document.getElementById("button").addEventListener("click", function(){alert("foo"); return false;});
#button{cursor:pointer}
<svg viewBox="-1 -1 102 102" width="6rem" role="button">
 <polygon id="button" points="25 0, 75 0, 100 50, 75 100, 25 100, 0 50" stroke="#ccc" fill="none" pointer-events="all"/>
  <text x="50" y="50" text-anchor="middle" dominant-baseline="middle" font-size="14" pointer-events="none">the Button</text>
</svg>

或者,您可以使用以下几点:

document.getElementById("button").addEventListener("click", function(){alert("foo"); return false;});
#button{cursor:pointer}
<svg viewBox = "-50 -50 100 100" width="6rem" role="button">
  <polygon id="button" fill="none" stroke="#ccc" pointer-events="all" points = "48,0 24,41.57 -24,41.57 -48,0 -24,-41.57 24,-41.57 48,0" />

  <text text-anchor="middle" dominant-baseline="middle" font-size="14" pointer-events="none">the Button</text>
</svg>

答案 1 :(得分:1)

感谢恩卡内塔通过向我展示clip-path: polygon而获得了赏金。由于这需要百分比,因此推出六角形形状非常容易。仍然对基于SVG的路径发生了什么感到好奇,但是对我的原始代码进行的这种简单更改非常有用:

document.getElementById("button").addEventListener("click", function() {
  alert("foo");
  return false;
});
div {
  width: 9rem;
  height: 8rem;
  position: relative;
  clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0% 50%, 25% 0);
  -webkit-clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0% 50%, 25% 0);
  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: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0% 50%, 25% 0);
  -webkit-clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0% 50%, 25% 0);
  background-color: white;
  border: none;
}
<div id="div">
  <button id="button">The button</button>
</div>