gpt4 book ai didi

javascript - 如何影响 onmouseover 事件上的 previousSibling 样式。 CSS 或 JavaScript

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

我使用内联 SVG 创建了 map 。每个区域在悬停时亮起(用 CSS 解决)。我想要实现的是,当相应的地区名称悬停时,该地区也应该亮起。

我无法使用 CSS3 实现此目的,因为它没有 previousSibling 选择器。我也不能更改标记结构,因此地区位于标题之后。为什么?因为后面是正文分区。 (使用 z-index 也没有用。)

然后我希望找到一个使用 Javascript 的解决方案,使用 onmouseover 事件来触发一个函数,该函数将当前悬停文本的 previousSibling 更改其样式。

我的部分标记:

<path class="district-map" d="M264.741,21.661,323.1,155.235l179.3,85.439L496.992,21.661Z" transform="translate(-21 7)"/>
<text onmouseover="myFunction()" class="district-text" transform="translate(349 99)">
<tspan x="0" y="0">District 5</tspan>
</text>

这里是 CSS:

path.district-map {
fill: green;
}
path.district-map:hover {
fill: white;
}

最后是 Javascript:

function myFuntion() {
var district = this.previousSibling;
district.style.fill = "#fff";
}

Link to my codepen

我在这里缺少什么,什么不正常?(顺便说一句,我是 JavaScript 的完全菜鸟)

感谢您的意见。

完整片段:

<html>
<head>
<style>
svg {
width: 100%;
height: 100%;
max-width: 500px;
max-height: 500px;
display: block;
margin: auto;
}
</style>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 500 500">
<defs>
<style>
path.district-map {
fill: #00ff90;
transition: fill 0.2s;
}
path.district-map:hover {
fill: #fff;
}
.district-text {
fill: #5b5b5b;
font-size: 20px;
font-family: SegoeUI, Segoe UI;
}
.container {
fill: #d1d1d1;
}
</style>
<script>
function myFuntion() {
var district = this.previousSibling;
district.style.fill = "#fff";
}
</script>
</defs>
<rect class="container" width="500" height="500"/>

<path class="district-map" d="M264.741,21.661,323.1,155.235l179.3,85.439L496.992,21.661Z" transform="translate(-21 7)"/>
<text class="district-text" onmouseover = "myFunction()" transform="translate(349 99)"><tspan x="0" y="0">District 5</tspan></text>

<path class="district-map" d="M51.745,21.661V149.82l108.9,95.668L272.563,94.465l-28.279-72.8Z" transform="translate(-21 7)"/>
<text class="district-text" onmouseover = "myFunction()" transform="translate(87 112)"><tspan x="0" y="0">District 4</tspan></text>

<path class="district-map" d="M51.745,179.3l100.481,81.227L131.167,289.41l178.1,187.124H51.745Z" transform="translate(-21 7)"/>
<text class="district-text" onmouseover = "myFunction()" transform="translate(75 414)"><tspan x="0" y="0">District 3</tspan></text>

<path class="district-map" d="M429,217.81l75.812,43.321v210.59l-181.107,3.61L297.232,442.84Z" transform="translate(-21 7)"/>
<text class="district-text" onmouseover = "myFunction()" transform="translate(374 379)"><tspan x="0" y="0">District 2</tspan></text>

<path class="district-map" d="M284,109.507,155.235,285.2,291.817,427.8l125.752-216-107.1-49.338Z" transform="translate(-21 7)"/>
<text class="district-text" onmouseover = "myFunction()" transform="translate(226 272)"><tspan x="0" y="0">District 1</tspan></text>
</svg>
</body>

最佳答案

只需使用 SVG 将区域 + 文本组合/包装在一起 <g>元素 ( more about it ),而不是更改 CSS 以对组悬停使用react:

<html>
<head>
<style>
svg {
width: 100%;
height: 100%;
max-width: 500px;
max-height: 500px;
display: block;
margin: auto;
}
</style>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 500 500">
<defs>
<style>
path.district-map {
fill: #00ff90;
transition: fill 0.2s;
}
g:hover path.district-map {
fill: #fff;
}
.district-text {
fill: #5b5b5b;
font-size: 20px;
font-family: SegoeUI, Segoe UI;
}
.container {
fill: #d1d1d1;
}

</style>
</defs>
<rect class="container" width="500" height="500"/>

<g>
<path class="district-map" d="M264.741,21.661,323.1,155.235l179.3,85.439L496.992,21.661Z" transform="translate(-21 7)"/>
<text class="district-text" transform="translate(349 99)"><tspan x="0" y="0">District 5</tspan></text>
</g>

<g>
<path class="district-map" d="M51.745,21.661V149.82l108.9,95.668L272.563,94.465l-28.279-72.8Z" transform="translate(-21 7)"/>
<text class="district-text" transform="translate(87 112)"><tspan x="0" y="0">District 4</tspan></text>
</g>

<g>
<path class="district-map" d="M51.745,179.3l100.481,81.227L131.167,289.41l178.1,187.124H51.745Z" transform="translate(-21 7)"/>
<text class="district-text" transform="translate(75 414)"><tspan x="0" y="0">District 3</tspan></text>
</g>

<g>
<path class="district-map" d="M429,217.81l75.812,43.321v210.59l-181.107,3.61L297.232,442.84Z" transform="translate(-21 7)"/>
<text class="district-text" transform="translate(374 379)"><tspan x="0" y="0">District 2</tspan></text>
</g>

<g>
<path class="district-map" d="M284,109.507,155.235,285.2,291.817,427.8l125.752-216-107.1-49.338Z" transform="translate(-21 7)"/>
<text class="district-text" transform="translate(226 272)"><tspan x="0" y="0">District 1</tspan></text>
</g>

</svg>
</body>
</html>

Updated PEN .

The <g> SVG element is a container used to group other SVG elements.

关于javascript - 如何影响 onmouseover 事件上的 previousSibling 样式。 CSS 或 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52852058/

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