简体   繁体   English

当另一个元素是 hover 时,更改 svg 路径

[英]Change a svg path when another element is hover

I'm posting my first question because I have an average level with web languages and I don't know all the subtleties.我发布我的第一个问题是因为我对 web 语言的平均水平,我不知道所有的微妙之处。 I don't know if I should touch the CSS which was my first idea or if I should do Javascript but I don't know enough about it... I want to change the zones of a map in an svg file when I hover over another element on the page. I don't know if I should touch the CSS which was my first idea or if I should do Javascript but I don't know enough about it... I want to change the zones of a map in an svg file when I hover在页面上的另一个元素上。 I'm already trying to change the fill of the.maptest class when I hover over the "chateauneufbeta" class.当我在“chateauneufbeta”class 上进行 hover 时,我已经在尝试更改 .maptest class 的填充。 What I'm trying to do is below: / What I want to do: when the class chateauneufbeta is hovered over it changes the path of the.maptest class /我想做的是如下:/我想做的是:当class chateauneufbeta悬停在它上面时,它会改变.maptest class的路径/

My map area has two classes: .ZoneVerte and.maptest for more freedom of selection.我的 map 区域有两个类:.ZoneVerte 和.maptest 以提供更多选择自由。

I translated a part of my CSS to help you (I am of French origin ^^)我翻译了我的 CSS 的一部分来帮助你(我是法国人^^)

I use Wordpress for information.我使用 Wordpress 获取信息。 Here is a part of my current CSS and HTML:这是我当前 CSS 和 HTML 的一部分:

 /*Affichage flex de la carte */.location__map-region{ display: flex; margin: 2rem auto; } /*Séparation de chaque région avec une ligne blanche de 1 pixel */.location__map-region path { stroke: #ffffff; stroke-width: 1; transition: .6s fill; } /*What I want to do: when the class chateauneufbeta is hovered over it changes the path of the.maptest class*/.chateauneufbeta:hover ~.maptest { fill: #93c020;important: opacity. 1;0.important: } /*Normal colour of the Châteauneuf territory*/;ZoneVerte{ fill: #93c020. opacity; 0.8: } /*Colour of the Châteauneuf area when it is flown over */:ZoneVerte;hover{ fill: #93c020. opacity; 1.0; }

Part of my HTML code with the list of cities on the side (see the image at the bottom of the post):我的 HTML 代码的一部分,旁边有城市列表(见帖子底部的图片):

 <div class="location__wrapper"><div class="location__content"><ul class="location__list"> <li class="location__list-item"><a data-area-id="1" class="location__list-link" href="https://www.antibes-juanlespins.com/">Antibes Juan-les-Pins</a></li> <li class="location__list-item"><a data-area-id="22" class="location__list-link" href="http://bezaudun.fr/">Bézaudun-les-Alpes</a></li> <li class="location__list-item"><a data-area-id="3" class="location__list-link" href="https://www.biot.fr/">Biot</a></li> <li class="location__list-item"><a data-area-id="21" class="location__list-link" href="https://bouyon.fr/">Bouyon</a></li> <li class="location__list-item"><a data-area-id="15" class="location__list-link" href="http://www.caussols.fr/fr/">Caussols</a></li> <li class="location__list-item chateauneufalpha"><a data-area-id="9" class="location__list-link chateauneufbeta" href="https://www.ville-chateauneuf.fr/">Châteauneuf</a></li> <li class="location__list-item"><a data-area-id="18" class="location__list-link" href="http://www.cipieres.fr/">Cipières</a></li> <li class="location__list-item"><a data-area-id="19" class="location__list-link" href="#">Conségudes</a></li> <li class="location__list-item"><a data-area-id="14" class="location__list-link" href="http://www.courmes.fr/">Courmes</a></li> <li class="location__list-item"><a data-area-id="17" class="location__list-link" href="#">Coursegoules</a></li> <li class="location__list-item"><a data-area-id="13" class="location__list-link" href="https://www.gourdon06.fr/fr">Gourdon</a></li> <li class="location__list-item"><a data-area-id="16" class="location__list-link" href="http://www.greolieres.fr/">Gréolières</a></li> <li class="location__list-item"><a data-area-id="6" class="location__list-link" href="https://www.lacollesurloup.fr/fr">La Colle-sur-Loup</a></li> <li class="location__list-item"><a data-area-id="20" class="location__list-link" href="https://laroqueenprovence.fr/">La Roque-en-Provence</a></li> <li class="location__list-item"><a data-area-id="12" class="location__list-link" href="https://lebarsurloup.fr/">Le Bar-sur-Loup</a></li> <li class="location__list-item"><a data-area-id="10" class="location__list-link" href="https://lerouret.fr/">Le Rouret</a></li> <li class="location__list-item"><a data-area-id="24" class="location__list-link" href="#">Les Ferres</a></li> <li class="location__list-item"><a data-area-id="8" class="location__list-link" href="https://mairie-opio.fr/">Opio</a></li> <li class="location__list-item"><a data-area-id="7" class="location__list-link" href=" https://www.ville-roquefort-les-pins.fr/">Roquefort-les-Pins</a></li> <li class="location__list-item"><a data-area-id="5" class="location__list-link" href="https://www.saint-pauldevence.com/">Saint-Paul de Vence</a></li> <li class="location__list-item"><a data-area-id="11" class="location__list-link" href="https://tourrettessurloup.com/">Tourrettes-sur-Loup</a></li> <li class="location__list-item"><a data-area-id="2" class="location__list-link" href="https://www.ville-valbonne.fr/">Valbonne Sophia Antipolis</a></li> <li class="location__list-item"><a data-area-id="23" class="location__list-link" href="http://www.vallauris-golfe-juan.fr/">Vallauris Golfe-Juan</a></li> <li class="location__list-item"><a data-area-id="4" class="location__list-link" href="https://www.villeneuveloubet.fr/">Villeneuve-Loubet</a></li></ul></div>

The result I expect at the end is the following:我期望的结果如下:

map change color map变色

Here's some simple code that shows how to do what you want.这是一些简单的代码,展示了如何做你想做的事。 I've added comments to the code to describe what each step is doing.我在代码中添加了注释来描述每个步骤在做什么。

 // Find all the <a> elements in the list var items = document.querySelectorAll("UL.location__list LI A"); // To each of them, add an event handler for when the mouse enters its box, and also when it leaves items.forEach(item => { item.addEventListener("mouseover", itemMouseOver); item.addEventListener("mouseout", itemMouseOut); }); function itemMouseOver(evt) { // Get the value of the data-area-id attribute for the <a> we are entering var areaId = evt.target.dataset['areaId']; // Use it to find the right circle, and add the class "active" to its class attaribute document.getElementById(areaId).classList.add("active"); } function itemMouseOut(evt) { // Get the value of the data-area-id attribute for the <a> we are entering var areaId = evt.target.dataset['areaId']; // Use it to find the right circle, and remove the class "active" from its class attaribute document.getElementById(areaId).classList.remove("active"); }
 /* The default style for a circle */ svg circle { fill: lightgreen; } /* the style for when the circle is hovered */ svg circle.active { fill: green; }
 <div class="location__wrapper"> <div class="location__content"> <ul class="location__list"> <li class="location__list-item"><a data-area-id="1" class="location__list-link" href="https://www.antibes-juanlespins.com/">Antibes Juan-les-Pins</a></li> <li class="location__list-item"><a data-area-id="22" class="location__list-link" href="http://bezaudun.fr/">Bézaudun-les-Alpes</a></li> <li class="location__list-item"><a data-area-id="3" class="location__list-link" href="https://www.biot.fr/">Biot</a></li> <li class="location__list-item"><a data-area-id="21" class="location__list-link" href="https://bouyon.fr/">Bouyon</a></li> </ul> </div> </div> <svg width="200" viewBox="0 0 100 100"> <circle id="1" cx="25" cy="25" r="20"/> <circle id="22" cx="75" cy="25" r="20"/> <circle id="3" cx="25" cy="75" r="20"/> <circle id="21" cx="75" cy="75" r="20"/> </svg>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM