Vue 2.x过渡不适用于HTML标签ID CSS

只是好奇这是否是vue的预期行为,或者也许我没有看到一些基本的CSS逻辑?

  • 当使用类选择器设置默认的“ 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;
    }
  }
});
评论
  • tqui
    tqui 回复

    改变中

    .slide-enter,
    .slide-leave-to  { 
       right:-100px;
    }
    

    .slide-enter,
    .slide-leave-to  { 
       right:-100px !important;
    }
    

    Makes it work with the ID because ID selectors have greater specificity than class selectors:

    以下选择器类型列表根据具体情况而增加:      •键入选择器(例如h1)和伪元素(例如::: before)。      •类选择器,属性选择器和伪类。      •ID选择器(例如,#example)。