gpt4 book ai didi

javascript - 从列表中更改所选元素的 Vue.js 转换

转载 作者:搜寻专家 更新时间:2023-10-30 22:15:00 24 4
gpt4 key购买 nike

我有一个配置文件列表,可以打开“编辑配置文件”屏幕。这个画面是从左边滑进来的。当我选择一个配置文件时,如果已经选择了一个屏幕,我希望它先滑出,更改选择的配置文件数据然后滑入。

现在发生的情况是:当我第一次选择一个元素时,屏幕会滑入。当我更改所选元素时,屏幕会停留并且不会滑出和滑回。

这是一个 gif 来展示它现在的行为:

enter image description here

我的代码是:

View 方法:

editProfile: function (index){
// this.editingProfile = false;
this.setProfile(index);
this.editingProfile = true;

}

HTML View :

        <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%);
}

如何在更改配置文件时使其正确滑出然后再滑回?

最佳答案

我认为我的评论不正确。一种方法是利用 :key和一个 v-if这样你就可以告诉 Vue 在你选择了一个面板的情况下渲染一个面板,然后以这种方式在面板之间转换。你不需要 transition-group那么。

事情是:key是什么告诉 Vue 一切都变了。如果你不这样做,Vue 会尝试尽可能多地回收。请参阅文档:Transitioning Between Elements

When toggling between elements that have the same tag name, youmust tell Vue that they are distinct elements by giving them uniquekey attributes. Otherwise, Vue’s compiler will only replace thecontent of the element for efficiency. Even when technicallyunnecessary though, it’s considered good practice to always keymultiple items within a <transition> component.

考虑下面的最小示例:

const panels = [{
title: "Hello"
},
{
title: "World"
},
{
title: "Foo"
},
{
title: "Bar"
}
];

const app = new Vue({
el: "#app",
data() {
return {
panels,
activePanel: null
};
},
computed: {
titles() {
return this.panels.map(panel => panel.title);
}
},
methods: {
handleTitleClick(idx) {
if (this.activePanel === idx) {
this.activePanel = null;
return;
}
this.activePanel = idx;
}
}
});
body {
margin: 0;
padding: 0;
}

#app {
display: flex;
align-items: stretch;
height: 100vh;
}

#panel-set {
flex: 1 0 70%;
display: flex;
}

#side-panel {
flex: 1 0 30%;
}

.panel {
padding: 1em;
flex: 1 0;
background-color: rgba(0, 0, 0, 0.2);
}

.slide-fade-enter-active,
.slide-fade-leave-active {
transition: transform 500ms ease-in-out;
}

.slide-fade-enter,
.slide-fade-leave-to {
transform: translateX(-100%);
}
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>


<div id="app">
<div id="panel-set">
<transition name="slide-fade" mode="out-in">
<div class="panel" v-if="activePanel !== null" :key="activePanel">
<h2>{{panels[activePanel].title}}</h2>
<p>Lorem ipsum</p>
</div>
</transition>
</div>
<div id="side-panel">
<ul>
<li v-for="(title, idx) in titles" @click="handleTitleClick(idx)">{{title}}</li>
</ul>
</div>
</div>

关于javascript - 从列表中更改所选元素的 Vue.js 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51827316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com