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

标签 javascript vue.js vuejs2 css-transitions

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

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

这是一个 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, you must tell Vue that they are distinct elements by giving them unique key attributes. Otherwise, Vue’s compiler will only replace the content of the element for efficiency. Even when technically unnecessary though, it’s considered good practice to always key multiple 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/

相关文章:

javascript - 为什么在我的 JS 嵌套数组中得到 "arr[i] is undefined"?

vue.js - 我无法在我的 vuejs 应用程序中显示 fontawesome 图标

vue.js - 在 q 表列中显示从字段而不是字段本身派生的值

javascript - Vue 2 - 监听滚动事件

css - Vis Network + Vuetify Tabs - 网络不会填满屏幕

javascript - 跨域 XHR 到 https 地址 (SSL) 在 Firefox 中失败(在 Chrome 中工作)

javascript - 任意泛型的 typescript map

javascript - 如何更改 JSLint 版本中的缩进 2015-06-22

javascript - VueJS更改特定行的字体而不是所有行

javascript - html页面打印时如何控制溢出内容?