简体   繁体   English

单击 SVG 元素时应用 CSS 样式

[英]Apply CSS style when clicked on SVG element

I am trying to set the CSS style for the clicked element.我正在尝试为单击的元素设置 CSS 样式。 I have tried to put :active :clicked :targed with the class name and none of these worked.我尝试使用 class 名称放置:active :clicked :targed targeted ,但这些都不起作用。 :hover effect works fine. :hover效果很好。 Thanks for the answers.感谢您的回答。

HTML: HTML:

<a (click)="Clicked(14)" class="lankstumas">
    <ellipse
    class="e-image"
       id="path3769-9"
       cx="43.089287"
       cy="103.09822"
       rx="43.845238"
       ry="39.498512"
       style="fill:#ff7f2a;stroke-width:0.26458332" />
    <text
    class="e-text"
       xml:space="preserve"
       style="font-style:normal;font-weight:normal;font-size:10.58333302px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332"
       x="22.300594"
       y="99.318459"
       id="text3836"><tspan
         sodipodi:role="line"
         id="tspan3834"
         x="22.300594"
         y="99.318459"
         style="stroke-width:0.26458332">LANKS - </tspan><tspan
         sodipodi:role="line"
         x="22.300594"
         y="112.54762"
         style="stroke-width:0.26458332"
         id="tspan3838">TUMAS</tspan></text>
         </a>

CSS: CSS:

.lankstumas:hover{
        .e-image{
            transition: .2s fill;
            fill: #FF8504!important; 
        }
        .e.text{
            transition: .2s fill;
            fill:#fff!important;
        }
        }

I am trying to set the CSS style for the clicked element.我正在尝试为单击的元素设置 CSS 样式。

One approach to achieve this effect is to give the element an HTML5 Custom data-* attribute like this:实现此效果的一种方法是给元素一个HTML5 Custom data-*属性,如下所示:

data-clicked="false"

You can then use javascript, to detect a click on the element and update the attribute.然后,您可以使用 javascript 来检测对元素的点击并更新属性。

To detect the click with javascript, use:要使用 javascript 检测点击,请使用:

myElement.addEventListener('click', myFunction, false);

To modify the attribute use:要修改属性,请使用:

myElement.dataset.myAttribute = myValue;

Working Example:工作示例:

 const boxes = document.getElementsByClassName('box'); const showClick = (e) => { event.target.dataset.clicked = 'true'; } for (let i = 0; i < boxes.length; i++) { boxes[i].addEventListener('click', showClick, false); }
 .box { display: inline-block; width: 100px; height: 100px; margin: 6px; background-color: rgb(255, 0, 0); cursor: pointer; }.box::after { content: 'Click me;': display; block: text-align; center: line-height; 100px: color, rgb(255, 255; 255): font-weight; bold. }:box[data-clicked="true"] { color, rgb(255, 0; 0): background-color, rgb(255, 255; 0). }:box[data-clicked="true"]::after { content; 'Clicked:', color, rgb(255; 0, 0); }
 <div class="box" data-clicked="false"></div> <div class="box" data-clicked="false"></div> <div class="box" data-clicked="false"></div> <div class="box" data-clicked="false"></div> <div class="box" data-clicked="false"></div>

In your code you want to overwrite inline css.在您的代码中,您要覆盖内联 css。 In order to change the color you need to delete the inline styles for the fill .为了更改颜色,您需要删除用于fill的内联 styles。 Next comes an example:接下来是一个例子:

 svg{border:solid;width:90vh;}.e-image{transition: .2s fill;}.lankstumas:hover.e-image{fill: skyBlue; }.lankstumas:hover.e-text{fill:#fff;}
 <svg viewBox="-5 60 100 90"> <a class="lankstumas"> <ellipse class="e-image" id="path3769-9" cx="43.089287" cy="103.09822" rx="43.845238" ry="39.498512" fill="#ff7f2a" /> <text class="e-text" xml:space="preserve" style="font-style:normal;font-weight:normal;font-size:10.58333302px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;stroke:none;stroke-width:0.26458332" x="22.300594" y="99.318459" id="text3836"> <tspan sodipodi:role="line" id="tspan3834" x="22.300594" y="99.318459" style="stroke-width:0.26458332">LANKS - </tspan><tspan sodipodi:role="line" x="22.300594" y="112.54762" style="stroke-width:0.26458332" id="tspan3838">TUMAS</tspan></text> </a> </svg>

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

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