Vue 2.x 转换不适用于 Html 标签 ID CSS
Vue 2.x Transitions not working with Html Tag Id CSS
只是好奇这是否是 vue 的预期行为,或者可能有一些我没有看到的潜在 CSS 逻辑?
- 当使用 class 选择器
设置默认 'right' 属性 时,以下 Vue 转换有效
.theBoxClass { right: 100px;}
但使用 css ID 选择器设置时不会
#theBoxId { right: 100px;}
CodePen
https://codepen.io/PhilipNameHere/pen/xxwBWxw
代码
<div id="background"></div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="vue">
<transition name="slide">
<div id="theBoxId" class="box" v-if="show">ID</div>
</transition>
<transition name="slide">
<div class="theBoxClass box" v-if="show">Class</div>
</transition>
<button v-on:click="onShowClick">CLICK ME TWICE</button>
</div>
body {
margin: 0;
padding:20px;
}
button
{
background:red;
padding:30px;
color:white;
font-weight:bold;
}
.box{
background:gray;
width:100px;
position:fixed;
height:100px;
text-align:center;
color:white;
font-size:20px;
line-height:100px;
}
.theBoxClass
{
top:20px;
right:100px;
}
#theBoxId
{
top:220px;
right:100px;
}
.slide-enter-active ,
.slide-leave-active {
transition: right 1s ease-out;
}
.slide-enter,
.slide-leave-to {
right:-100px;
}
new Vue({
el: "#vue",
data: {
show: false
},
methods: {
onShowClick: function() {
this.show = !this.show;
}
}
});
改变
.slide-enter,
.slide-leave-to {
right:-100px;
}
到
.slide-enter,
.slide-leave-to {
right:-100px !important;
}
使其与 ID 一起工作,因为 ID 选择器的 specificity 多于 class 选择器:
The following list of selector types increases by specificity:
• Type selectors (e.g., h1) and pseudo-elements (e.g., ::before).
• Class selectors, attributes selectors and pseudo-classes.
• ID selectors (e.g., #example).
只是好奇这是否是 vue 的预期行为,或者可能有一些我没有看到的潜在 CSS 逻辑?
- 当使用 class 选择器 设置默认 'right' 属性 时,以下 Vue 转换有效
.theBoxClass { right: 100px;}
但使用 css ID 选择器设置时不会
#theBoxId { right: 100px;}
CodePen https://codepen.io/PhilipNameHere/pen/xxwBWxw
代码
<div id="background"></div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="vue">
<transition name="slide">
<div id="theBoxId" class="box" v-if="show">ID</div>
</transition>
<transition name="slide">
<div class="theBoxClass box" v-if="show">Class</div>
</transition>
<button v-on:click="onShowClick">CLICK ME TWICE</button>
</div>
body {
margin: 0;
padding:20px;
}
button
{
background:red;
padding:30px;
color:white;
font-weight:bold;
}
.box{
background:gray;
width:100px;
position:fixed;
height:100px;
text-align:center;
color:white;
font-size:20px;
line-height:100px;
}
.theBoxClass
{
top:20px;
right:100px;
}
#theBoxId
{
top:220px;
right:100px;
}
.slide-enter-active ,
.slide-leave-active {
transition: right 1s ease-out;
}
.slide-enter,
.slide-leave-to {
right:-100px;
}
new Vue({
el: "#vue",
data: {
show: false
},
methods: {
onShowClick: function() {
this.show = !this.show;
}
}
});
改变
.slide-enter,
.slide-leave-to {
right:-100px;
}
到
.slide-enter,
.slide-leave-to {
right:-100px !important;
}
使其与 ID 一起工作,因为 ID 选择器的 specificity 多于 class 选择器:
The following list of selector types increases by specificity:
• Type selectors (e.g., h1) and pseudo-elements (e.g., ::before).
• Class selectors, attributes selectors and pseudo-classes.
• ID selectors (e.g., #example).