javascript – Vue.js在从列表中更改所选元素时进行转换
作者:互联网
我有一个配置文件列表,可以打开“编辑配置文件”屏幕.这个屏幕从左边滑入.当我选择一个配置文件时,如果已经选择了一个屏幕,我希望它首先滑出,更改所选的配置文件数据,然后滑入.
现在发生的事情是:当我第一次选择一个元素时,屏幕会滑入.当我更改所选元素时,屏幕会停留,不会滑出并重新进入.
这是一个gif来展示它现在的表现如何:
我的代码是:
Vue方法:
editProfile: function (index){
// this.editingProfile = false;
this.setProfile(index);
this.editingProfile = true;
}
Html查看:
<transition name="fade" mode="out-in">
<div v-if="editingProfile" id="edit-profile">
<input placeholder="Profile Name" v-model="synced.profiles[synced.selectedProfile].name">
</div>
</transition>
CSS:
.fade-enter-active, .fade-leave-active {
transition: all .2s;
/* transform:translateX(0); */
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
opacity: 0;
transform:translateX(-100%);
}
如何在更改配置文件时将其正确滑出然后再返回?
解决方法:
在代码中显示一个配置文件后,根本原因是v-if =“editingProfile”始终为true.
一个解决方案首先将其设置为false,然后将其设置为.$nextTick将其再次设置为true.但是你必须将this.editingProfile = true放在一个setTimeout中,延迟时间=转换时间.否则,幻灯片效果将被覆盖.
如下演示:
new Vue({
el: '#emit-example-simple',
data() {
return {
editingProfile: false,
synced : {
profiles: [{'name':'A'}, {'name':'B'}, {'name':'C'}],
selectedProfile: 0
},
}
},
methods: {
editProfile: function (index){
this.editingProfile = !this.editingProfile
this.$nextTick(() => {
setTimeout(()=> {
this.synced.selectedProfile = index
this.editingProfile = true
}, 1200)
})
}
}
})
.fade-enter-active, .fade-leave-active {
transition: all 1.2s;
/* transform:translateX(0); */
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
opacity: 0;
transform:translateX(-100%);
border: 1px solid white;
}
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="emit-example-simple">
<button @click="editProfile(0)">Profile 1</button>
<button @click="editProfile(1)">Profile 2</button>
<button @click="editProfile(2)">Profile 3</button>
<transition name="fade" mode="out-in">
<div v-if="editingProfile" id="edit-profile">
<input style="border: 5px solid red;" placeholder="Profile Name" v-model="synced.profiles[synced.selectedProfile].name">
</div>
</transition>
</div>
或者您可以考虑使用如下简单演示的组转换:
new Vue({
el: '#emit-example-simple',
data() {
return {
editingProfile: false,
profileContainers: [true, false],
synced : {
profiles: [{'name':'A'}, {'name':'B'}, {'name':'C'}],
selectedProfile: 0
},
}
},
methods: {
editProfile: function (index){
this.synced.selectedProfile = index
this.profileContainers = this.profileContainers.map((x)=>!x)
}
}
})
.list-items-enter-active {
transition: all 1.2s;
}
.list-items-leave-active {
transition: all 1.2s;
}
.list-items-enter, .list-items-leave-to /* .fade-leave-active below version 2.1.8 */ {
opacity: 0;
transform:translateX(-100%);
border: 1px solid white;
}
.list-item {
display: inline-block;
border: 6px solid red;
}
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="emit-example-simple">
<button @click="editProfile(0)">Profile 1</button>
<button @click="editProfile(1)">Profile 2</button>
<button @click="editProfile(2)">Profile 3</button>
<transition-group name="list-items" tag="p">
<div v-for="(item, index) in profileContainers" :key="index" v-if="item">
<input style="border: 5px solid red;" placeholder="Profile Name" v-model="synced.profiles[synced.selectedProfile].name">
</div>
</transition-group>
</div>
标签:javascript,css-transitions,vue-js,vuejs2 来源: https://codeday.me/bug/20190607/1194305.html