gpt4 book ai didi

html - 无法在 Safari 中应用 SVG 剪辑路径

转载 作者:太空宇宙 更新时间:2023-11-04 14:50:30 26 4
gpt4 key购买 nike

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

火狐:Button in Firefox Safari :Button in Safari

最佳答案

您可以使用 clip-path:polygon() 而不是使用 clip-path: url()我已将您的分数乘以 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>

关于html - 无法在 Safari 中应用 SVG 剪辑路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53623016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com