【问题标题】:Vue 2.x Transitions not working with Html Tag Id CSSVue 2.x 过渡不适用于 Html 标签 ID CSS
【发布时间】:2020-09-10 16:33:35
【问题描述】:

只是好奇这是 vue 的预期行为,还是我没有看到一些底层 CSS 逻辑?

  • 当使用类选择器设置默认的“right”属性时,以下 Vue 转换有效

.theBoxClass { right: 100px;}

但在使用 css ID 选择器设置时不会

#theBoxId { right: 100px;}

代码笔 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;
    }
  }
});

【问题讨论】:

    标签: html css vue.js vuejs2 transition


    【解决方案1】:

    变化

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

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

    使其与 ID 一起使用,因为 ID 选择器的 specificity 大于类选择器:

    以下选择器类型列表按具体情况增加:

    • 类型选择器(例如 h1)和伪元素(例如 ::before)。

    • 类选择器、属性选择器和伪类。

    • ID 选择器(例如,#example)。

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 1970-01-01
      • 2022-09-28
      • 2015-01-02
      • 2015-11-01
      • 2021-08-09
      • 2014-02-16
      • 2019-02-22
      • 2016-02-15
      相关资源
      最近更新 更多