[英]Some CSS styles not working on SVG
I am working on this fiddle : https://jsfiddle.net/thatOneGuy/x2pxx92e/6/ 我正在研究这个小提琴: https : //jsfiddle.net/thatOneGuy/x2pxx92e/6/
I have this code for mouseover and out events : 我有以下代码用于mouseover和out事件:
d3.select('#circleSVG').on('mouseover', function(d) {
console.log('mouseover')
d3.select(this).classed('testCircle', true)
console.log(this)
}).on('mouseout', function(){
d3.select(this).classed('testCircle', false)
})
The testCircle
class looks like so : testCircle
类如下所示:
.testCircle{
fill: orange;
opacity:0.25;
}
But the only style that gets brought through on this class is the opacity. 但是,此类中唯一采用的样式是不透明度。 It doesn't change the fill.
它不会改变填充。 Any idea why ?
知道为什么吗?
The problem is basically how the CSS selectors works. 问题基本上是CSS选择器的工作方式。
Basically an id selector (#) is more specific than a class selector (.). 基本上,id选择器(#)比类选择器(。)更具体。 So the "fill: orange" property in the class selector (.testCircle) is not being applied because the id selector (#testCircle) is more specific and also have a fill property.
因此,类选择器(.testCircle)中的“ fill:orange”属性未应用,因为id选择器(#testCircle)更具体,并且也具有fill属性。 On the other hand, the opacity property is working because the id selector doesn't specify that property.
另一方面,opacity属性起作用,因为id选择器未指定该属性。
To fix this you can add "!important" as follow: 要解决此问题,您可以添加“!important”,如下所示:
.testCircle{
fill: orange !important;
opacity:0.25;
}
Or even better, make your selector more specific: 甚至更好的是,使选择器更具体:
#circleSVG.testCircle{
fill: orange !important;
opacity:0.25;
}
Why do you use JS to accomplish that? 您为什么使用JS来做到这一点? You can use only css.
您只能使用CSS。
#circleSVG {
fill: red;
stroke: green;
}
#circleSVG:hover {
fill: orange;
opacity: 0.25;
}
https://jsfiddle.net/x2pxx92e/11/ https://jsfiddle.net/x2pxx92e/11/
You are looking to change a class, but you also have an ID to define the svg color, so it's better to change the color of the ID when it's hover: 您想更改一个类,但也有一个ID来定义svg颜色,因此最好将ID悬停时更改其颜色:
#circleSVG:hover{
fill: orange;
opacity:0.25;
}
To change the color by the ID, you can use 要通过ID更改颜色,您可以使用
element = document.getElementById(id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.