【发布时间】:2020-12-09 07:47:26
【问题描述】:
我正在尝试仅使用 HTML 和 CSS 绘制由六边形组成的板。 这是一个 React 应用程序。
我的问题是这些六边形的“hitbox”是矩形的。有没有办法让我的onClick hitbox 完全适合我的六边形?
React.JS 代码:
<div className={`land${getClassNames(landState)}`}>
<svg
viewBox="0 0 100 100"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
>
<polygon points="50 1 95 25 95 75 50 99 5 75 5 25"
onClick={() => handleClick(index, selectLand, landState)} />
</svg>
CSS
.land {
float: left;
margin-left: -5px;
margin-bottom: -32px;
width: 140px;
}
.land svg {
width: 100%;
}
.land.isVacant svg {
fill : #e0e0e0;
}
.land.isOccupiedByPlayer1 svg {
fill : #90caf9;
}
.land.isOccupiedByPlayer2 svg {
fill : #ef9a9a;
}
.active svg:hover {
cursor: pointer;
}
编辑:
在polygone 上移动onClick 给了我一个更好的结果,但仍然不完美和奇怪。我在下图中突出显示了不可点击的区域。我不明白这个。
.board {
float: left; width: 1200px;
}
.land-row {
clear: left;
}
.land-row.three {
margin-left: 135px;
}
.land-row.four {
margin-left: 67px;
}
.land {
float: left;
margin-left: -5px;
margin-bottom: -32px;
width: 140px;
}
.land svg {
width: 100%;
height: 100%;
}
.land.isVacant svg {
fill : #e0e0e0;
}
.land.isOccupiedByPlayer1 svg {
fill : #90caf9;
}
.land.isOccupiedByPlayer2 svg {
fill : #ef9a9a;
}
.active svg:hover {
cursor: pointer;
}
**EDIT:**<br>
<div id="root"><div class="App"><div class="board"><div class="land-row three"><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div></div><div class="land-row four"><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div><div class="land isOccupiedByPlayer1"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div></div><div class="land-row"><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div></div><div class="land-row four"><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div><div class="land isOccupiedByPlayer1"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div></div><div class="land-row three"><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div><div class="land isActive isVacant"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div><div class="land isOccupiedByPlayer2"><svg viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg"><polygon points="50 1 95 25 95 75 50 99 5 75 5 25"></polygon></svg></div></div></div></div></div>
【问题讨论】:
-
与@ksav 相同,我无法重现您所描述的内容。您测试过哪个浏览器,哪个版本?是否有其他 CSS 规则应用于
<svg>元素?删除filter属性后,点击行为是否会改变? -
感谢您的宝贵时间,我更新了我的问题。你有同样的结果吗?
-
我刚测试过,只有当我点击的六边形下面有一个六边形时才会这样做...会不会是另一个元素妨碍了?
-
@Boubou,是的,可以有一些不可见的元素。请从开发者窗口的浏览器中复制您的代码(但不是您的 React.JS 代码)。
-
@Bharata 我添加了一个带有 html 和 css 的代码 sn-p。我还尝试删除 margin-bottom: -32px 规则,没有它它工作得很好(尽管元素没有按我想要的方式放置。所以我知道一个元素妨碍了我,但不知道如何避免它...
标签: javascript html reactjs svg