路由器上的Vue转换-但转换会影响特定的html元素



我已经实现了VUE js的页面转换。我手动这样做是因为我找不到如何使用VUES转换来做到这一点。

(我使用的是vue-js的网格单元框架-我添加了一个自定义的App.vue页面-它应该允许网格单元的转换像正常的vue-js转换一样(

我觉得我所做的是膨胀的用例,所以想看看是否有人知道如何使用vue转换来实现这一点。

#1
Users click component (which has a @click - triggering a this.$router.push() to the route)
#2
A div pops over the screen in the color of that component, creating a nice fade to hide the transition
#3
On the new page, another div identical to the transition one, now exits the screen. 

我在这里有这个作品供参考,只需点击客户即可(请尽量不要对我进行太多评判,它仍在开发中(-https://wtwd.ninjashotgunbear.com/


我的方法:

Index.html

每个组件都是一个SectionTitle,当用户单击其中一个组件时,它们$emit带有该页面数据的特定对象(例如要路由到的页面的颜色和名称(-这就是下面的@routeChange="reRoute($event)

<template>
<Layout>
<div class="navs" v-for="section in sections" :key="section.sectionTitle">
<!-- On click delay for screen to come ove top -->
<!-- router to be put here -->
<SectionTitle :data="section" @routeChange="reRoute($event)"/> <<<< COMPONENT that $emits on click
</div>

<!-- fullpage div to slide in and cover up no leave transition -->
<div class="leaveScreen"></div>  <<<<< DIV that covers the screen 
</Layout>
</template>

这触发了我的方法,该方法在UI视图上移动div并创建转换效果:

methods:{
reRoute(value){
console.log(value)
// 1) animate the loading screen
let screen = document.querySelector('.leaveScreen');
screen.style.cssText=`background: ${value.backgroundColor}; left: 0%`;
// 2) re-route the page
setTimeout(()=>{
this.$router.push(value.sectionLink)
}, 700)
}
}

DIV的CSS:

.leaveScreen {
position: absolute;
top: 0;
bottom: 0;
left: -100%;
width: 100%;
z-index: 11;
// background color added by the fn reRoute()
transition: all 0.7s;

}

在页面上,我使用挂载的钩子从用户视图中删除div(与我在上面添加的方式相同,但不同。

mounted(){
let screen = document.querySelector('.fadeOutScreen');
// set timeout works to delay 
setTimeout(()=>{
screen.style.cssText='left: 100%;'
},700)

}

如果您知道如何在更干净的代码中/或通过使用VUES转换属性来做到这一点,那么我们非常欢迎您的帮助。我想VUE会有一个具体的方法来做到这一点,但还没有找到。

提前感谢-W

如果将.leave-screen封装在transition元素中,则可以执行以下操作:

new Vue({
el: "#app",
data: {
leaveScreen: false
}
})
body {
margin: 0;
}
.click-me {
cursor: pointer;
font-size: 30px;
}
.leave-screen {
position: absolute;
height: 100vh;
width: 100vw;
top: 0;
background-color: rgb(0, 0, 0);
}
.leave-screen-enter-active,
.leave-screen-leave-active {
background-color: rgba(0, 0, 0, 1);
transform: translateX(0);
transition: all 1s ease-in-out;
}
.leave-screen-leave-to,
.leave-screen-enter {
background-color: rgba(0, 0, 0, 0);
transform: translateX(-100%);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div @click="leaveScreen = true" class="click-me">
Click Me
</div>
<transition name="leave-screen">
<div v-if="leaveScreen" class="leave-screen" @click="leaveScreen = false"></div>
</transition>
</div>


.leave-screen-enter-active.leave-screen-leave-active定义了转换期间元素的状态。

.leave-screen-leave-to是元素离开的状态(令人惊讶(,而.leave-screen-enter是元素进入之前的状态。

在元素本身上设置的样式是过渡的开始/结束位置(取决于过渡是进入还是离开(。


Vue的定义:

  1. v-enter:输入的启动状态。在插入元素之前添加,在插入元素之后删除一个框架
  2. v-enter-active:进入的活动状态。在整个进入阶段应用。在插入元素之前添加,在过渡/动画结束时删除。此类可用于定义进入过渡的持续时间、延迟和缓和曲线
  3. v-leave-active:休假活动状态。在整个离职阶段应用。在触发离开过渡时立即添加,在过渡/动画结束时删除。此类可用于定义离开过渡的持续时间、延迟和缓和曲线
  4. v-leave-to:仅在2.1.8+版本中可用。休假结束状态。在触发离开过渡后添加一帧(同时移除v-leave(,在过渡/动画完成时移除

最新更新