如何使用Vue.js在选择选项上使用过渡

时间:2018-08-07 06:27:38

标签: javascript css vue.js css-transitions vue-component

关于Vue.js的转换,我有一个快速的问题。

在我的boostrap模板中,我试图基于所选选项再添加一个选择选项下拉菜单。因此,我在第一个选择选项上添加了更改事件。因此,如果我选择“第一项” ,然后更改类并在行中添加下拉菜单,否则将其隐藏。

  

类似的事情:

selectTodo: function(e) {
  let selectValue = e.target.options[e.target.selectedIndex].text

  if (selectValue === 'Learn Vue') {
    this.styleObject.display = 'unset';
    this.col_md = 'form-group col-md-4';
    this.showCropStageList = true;
  }
  else {
    this.showCropStageList = false;
    this.styleObject.display = 'none';
    this.col_md = 'form-group col-md-6';
    this.cropStageList = null;
  }
}

我设法做到了,但我希望在前两个下拉列表中更多更平滑的过渡。

我设法设置了一些fiddle,您可以在其中看到第三选择下拉列表的平滑幻灯片过渡,但是如果我将选择从“ Learn Vue” 更改为其他内容,第三个下拉菜单将其隐藏,但也没有任何动画。

您也可以在下面的代码段中看到它!

new Vue({
  el: "#app",
  data: {
    todos: [
      { id:1 ,text: "Learn JavaScript", done: false },
      { id:2 ,text: "Learn Vue", done: false },
      { id:3 ,text: "Play around in JSFiddle", done: true },
      { id:4 ,text: "Build something awesome", done: true }
    ],
    col_md: 'form-group col-md-6',
    styleObject: {
          display: 'none'
        },
    showCropStageList: false,
  },
  methods: {
  	toggle: function(todo){
    	todo.done = !todo.done
    },
    selectTodo: function(e) {
    let selectValue = e.target.options[e.target.selectedIndex].text
    
        if (selectValue === 'Learn JavaScript') {
          
          this.styleObject.display = 'unset';
          this.col_md = 'form-group col-md-4';
          this.showCropStageList = true;
        }
        else {

          this.showCropStageList = false;
          this.styleObject.display = 'none';
          this.col_md = 'form-group col-md-6';
          this.cropStageList = null;
          
        }
    }
  }
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

.fade-enter {
  opacity: 0;
}

.fade-enter-active {
  transition: opacity 1s;
}

.fade-leave {}

.fade-leave-active {
  transition: opacity 1s;
  opacity: 0;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

li {
  margin: 8px 0;
}

h2 {
  font-weight: bold;
  margin-bottom: 15px;
}

del {
  color: rgba(0, 0, 0, 0.3);
}
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<div class="container">
  <!-- Content here -->
  <div id="app">
    <div class="form-row">
      <transition name="fade" appear>
        <div v-bind:class=col_md>
          <label for="cropType" class="col-form-label-sm font-weight-bold">Select Learn Javascript </label>
          <select class="form-control" v-on:change="selectTodo" id="cropType" v-model="pickedCropType" @change="getCropsByType()">
              <option v-for="(todo, index) in todos" :key="index" :value="todo.id" >
                {{todo.text}}
              </option>
            </select>
        </div>
      </transition>
      <div v-bind:class=col_md>
        <label for="cropCulture" class="col-form-label-sm font-weight-bold">2. Second</label>
        <select class="form-control" id="cropCulture">
     <option v-for="(todo, index) in todos" :key="index" :value="todo.id" >
                {{todo.text}}
              </option>
            </select>
      </div>
      <transition 
      enter-active-class="animated fadeInLeft"
      leave-active-class="animated fadeOutLeft"
      >
        <div class="form-group col-md-4" v-if="showCropStageList" v-bind:class="{styleObject }">
          <label for="cropStage" class="col-form-label-sm font-weight-bold">3. Third</label>
          <select class="form-control" id="cropStage">
                <option v-for="(todo, index) in todos" :key="index" :value="todo.id" >
                {{todo.text}}
              </option>
              </select>
        </div>
      </transition>
    </div>
  </div>
</div>

您可以看到前两个下拉菜单的更改类别,但没有任何过渡。那么,是否有可能为前两个下拉菜单添加一些转换?

编辑:


如您所见,我正在将类从* form-group col-md-6 *更改为* form-group col-md-4 *,我想知道是否有可能从md进行该转换-6到md-4可能会更平滑。

https://jsfiddle.net/Loque/akt0su98/

2 个答案:

答案 0 :(得分:1)

我不知道在Vue中是否有特定的方法可以完成,但是您可以使用CSS魔术轻松地实现所需的功能。事实上,这很简单,您要做的就是在更改的div中添加一个类,例如,类名称为transition,并在CSS中定义如下的transition样式:

.transition {
     transition: all 0.25s linear;
}

您可以找到更新的小提琴here

请注意,您可以根据需要更改计时功能linear和时间0.25s,还可以阅读有关CSS过渡和效果here

的更多信息。

答案 1 :(得分:1)

在您的代码中,您将类从col-md-4直接更新为col-md-6中的selectTodo。这将导致前两个<div>在第3个div完成动画之前占据该行的全宽

解决方案,使用this.$nextTicksetTimeout

添加第三个div时,延迟执行在前两个<div>完成动画之前显示第三个div。

当删除第三个div时,在第三个col-md-6完成动画之前,将<div>延迟应用于前两个<div>

下面是一个示例:( PS :我在这里使用过渡组,因为它对Reusable Transitions会更好)

警告:请在整页演示下方进行测试,否则引导程序会将其视为超小屏幕(将每个<div>放在每行中),尽管我认为效果会很小屏幕还不错。

new Vue({
  el: "#app",
  data: {
    todos: [
      { id:1 ,text: "Learn JavaScript", done: false },
      { id:2 ,text: "Learn Vue", done: false },
      { id:3 ,text: "Play around in JSFiddle", done: true },
      { id:4 ,text: "Build something awesome", done: true }
    ],
    col_md: 'form-group col-md-6',
    styleObject: {
          display: 'none'
        },
    showCropStageList: false,
    pickedCropType: ''
  },
  methods: {
  	getCropsByType: function () {},
  	toggle: function(todo){
    	todo.done = !todo.done
    },
    selectTodo: function(e) {
    	let selectValue = e.target.options[e.target.selectedIndex].text
      if (selectValue === 'Learn Vue') {
        this.col_md = 'form-group col-md-4';
        this.$nextTick(() => { //delay display the third div before first two div finish animation
        	setTimeout(()=>{
            this.styleObject.display = 'unset';
            this.showCropStageList = true;
          }, 500)

        })
        
      }
      else {

        this.showCropStageList = false;
        this.styleObject.display = 'none';
        this.$nextTick(() => { //delay apply `md-6` to first two div before 3rd div finish animation
        	setTimeout(()=>{
          	this.col_md = 'form-group col-md-6';
          }, 1000)
        	
        })
        
        this.cropStageList = null;

      }
    }
  }
})
.sample {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

.fade-item {
  transition: all 0.5s;
  display:inline-block;
}

.fade-enter {
  opacity: 0;
  transform: translateX(-130px);

}

.fade-enter-active {
  transition: all 1.5s;
}

.fade-leave-to {
  transform: translateX(130px);
  opacity: 0;
  flex: 0 0 20%;
}

.fade-leave-active {
  transition: all 1s;
}

#app {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

li {
  margin: 8px 0;
}

h2 {
  font-weight: bold;
  margin-bottom: 15px;
}

del {
  color: rgba(0, 0, 0, 0.3);
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>

<!-- Content here -->
<div id="app">
  <div class="container">
    <div class="sample">
      <transition-group name="fade" tag="div" class="form-row">
        <div v-bind:class="col_md" :key="1" class="fade-item">
          <label for="cropType" class="col-form-label-sm font-weight-bold">Select Learn Javascript </label>
          <select class="form-control" v-on:change="selectTodo" id="cropType" v-model="pickedCropType" @change="getCropsByType()">
              <option v-for="(todo, index) in todos" :key="index" :value="todo.id" >
                {{todo.text}}
              </option>
            </select>
        </div>
      <div v-bind:class="col_md" :key="2" class="fade-item">
        <label for="cropCulture" class="col-form-label-sm font-weight-bold">2. Second</label>
        <select class="form-control" id="cropCulture">
     <option v-for="(todo, index) in todos" :key="index" :value="todo.id" >
                {{todo.text}}
              </option>
            </select>
      </div>
        <div class="form-group col-md-4 fade-item" v-if="showCropStageList" v-bind:class="{styleObject }" :key="3">
          <label for="cropStage" class="col-form-label-sm font-weight-bold">3. Third</label>
          <select class="form-control" id="cropStage">
                <option v-for="(todo, index) in todos" :key="index" :value="todo.id" >
                {{todo.text}}
              </option>
              </select>
        </div>
      </transition-group>
    </div>
  </div>

</div>