简体   繁体   English

CSS - 比类选择器强的第一个子选择器

[英]CSS - first-child selector stronger than class selector

I want to fire a CSS animation by adding a class to an list element that is styled by a first-child selector. 我想通过将类添加到由第一个子选择器设置样式的列表元素来触发CSS动画。 But somehow the current style is not overwritten by the added class. 但不知何故,当前的风格不会被添加的类覆盖。

js fiddle 小提琴

HTML HTML

<ul class="start">
<li id="box" >move up onHover</li>
</ul>
<div id="button">hover</div>

CSS CSS

.start li:first-child{

height:100px;
width:100px;
background: red;
opacity: 1;
transform: translate(0, 190px); 
transition: all 2.0s linear;

}

.change{
 opacity: 1;
 transform: translate(0, 0px);
 }

#button{
width: 80px;
height: 20px;
padding: 4px;
margin-left: 333px;
border:solid 1px black;
background: grey;
cursor: pointer;
color: white;
}

JS JS

    $('#button').hover(function() {
  $('#box').addClass('change');
})

It's not that it's stronger, but targeting the parent and then the LI is more specific, so you have to be just as specific when adding a new class 并不是说它更强大,而是针对父母,然后LI更具体,所以你必须在添加新类时同样具体

.start li.change

FIDDLE 小提琴

.start li:first-child overrides styling of .change .start li:first-child覆盖.change样式

Change 更改

.change{}

to

.start li.change{}

Updated fiddle here. 这里更新了小提琴。

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

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