如何在vue-router定义的页面(组件)之间实现淡入淡出效果页面转换?
答案 0 :(得分:140)
用<router-view></router-view>
包裹<transition name="fade"></transition>
并添加以下样式:
.fade-enter-active, .fade-leave-active {
transition-property: opacity;
transition-duration: .25s;
}
.fade-enter-active {
transition-delay: .25s;
}
.fade-enter, .fade-leave-active {
opacity: 0
}
详细答案
假设您已使用vue-cli创建了应用程序,例如:
vue init webpack fadetransition
cd fadetransition
npm install
安装路由器:
npm i vue-router
如果您没有使用vue-cli开发应用程序,请确保以标准方式添加vue路由器:
<script src="/path/to/vue.js"></script>
<script src="/path/to/vue-router.js"></script>
您可以使用例如:https://unpkg.com/vue-router/dist/vue-router.js
CLI已为您创建了一个骨干应用程序,您可以将其添加到。
1)创建页面组件
在Vue中,组件(UI元素)可以嵌套。您的应用中的页面可以使用常规Vue组件制作,该组件被视为该页面中其他组件的根目录。
转到src/
并创建pages/
目录。这些页面根组件(单个页面)将放在此目录中,而实际页面中使用的其他组件可以放在现成的components/
目录中。
为初学者在名为src/pages/Page1.vue
和src/pages/Page2.vue
的文件中创建两个页面。他们的内容将是(分别编辑页码):
<template>
<h1>Page 1</h1>
</template>
<script>
export default {
}
</script>
<style scoped>
</style>
2)设置路由
编辑生成的src/main.js
添加所需的导入:
import VueRouter from 'vue-router'
import Page1 from './pages/Page1'
import Page2 from './pages/Page2'
添加全局路由器用法:
Vue.use(VueRouter)
添加路由器设置:
const router = new VueRouter({
routes: [
{ path: '/page1', component: Page1 },
{ path: '/page2', component: Page2 },
{ path: '/', redirect: '/page1' }
]
})
最后一条路线只是将初始路径/
重定向到/page1
。编辑应用启动:
new Vue({
router,
el: '#app',
render: h => h(App)
})
整个src/main.js
示例位于答案的最后。
3)添加路由器视图
现在设置路由,只是根据路由器丢失页面组件的地方。这是通过将<router-view></router-view>
置于模板中的某个位置来完成的,您需要将其放在src/App.vue
的{{1}}标记中。
整个<template>
示例位于答案的最后。
4)在页面组件之间添加淡入淡出过渡效果
使用src/App.vue
元素包裹<router-view></router-view>
,例如:
<transition name="fade">
Vue将在这里完成工作:它将创建并插入适当的CSS类,从通过效果持续时间指定的名称开始,例如:<template>
<div id="app">
<transition name="fade">
<router-view></router-view>
</transition>
</div>
</template>
。现在在App.vue的部分中定义效果:
.fade-enter-active
就是这样。如果您现在运行该应用,例如使用<style>
.fade-enter-active, .fade-leave-active {
transition-property: opacity;
transition-duration: .25s;
}
.fade-enter-active {
transition-delay: .25s;
}
.fade-enter, .fade-leave-active {
opacity: 0
}
</style>
,它会自动显示带有淡入效果的第1页。如果您将URL重写为/ page2,它将使用淡出和淡入效果切换页面。
有关详细信息,请查看routing和transitions上的文档。
5)可选:添加指向网页的链接。
您可以使用npm run dev
组件添加指向特定网页的链接,例如:
<router-link>
这会自动为链接提供<router-link to="/page1">Page 1</router-link>
<router-link to="/page2">Page 2</router-link>
类,以防它们处于活动状态,但如果您使用的话,也可以指定自定义类。自举:
router-link-active
供参考的文件
的src / main.js:
<router-link class="nav-link" active-class="active" to="/page1">Page 1</router-link>
<router-link class="nav-link" active-class="active" to="/page2">Page 2</router-link>
的src / App.vue:
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App'
import Page1 from './pages/Page1'
import Page2 from './pages/Page2'
Vue.use(VueRouter)
const router = new VueRouter({
routes: [
{ path: '/page1', component: Page1 },
{ path: '/page2', component: Page2 },
{ path: '/', redirect: '/page1' }
]
})
/* eslint-disable no-new */
new Vue({
router,
el: '#app',
render: h => h(App)
})
的src /页/ Page1.vue:
<template>
<div id="app">
<router-link class="nav-link" active-class="active" to="/page1">Page 1</router-link>
<router-link class="nav-link" active-class="active" to="/page2">Page 2</router-link>
<transition name="fade">
<router-view></router-view>
</transition>
</div>
</template>
<script>
export default {
name: 'app'
}
</script>
<style>
.fade-enter-active, .fade-leave-active {
transition-property: opacity;
transition-duration: .25s;
}
.fade-enter-active {
transition-delay: .25s;
}
.fade-enter, .fade-leave-active {
opacity: 0
}
</style>
的src /页/ Page2.vue:
<template>
<h1>Page 1</h1>
</template>
<script>
export default {
}
</script>
<style scoped>
</style>
答案 1 :(得分:0)
还有一个名为vue-page-transition
的即插即用解决方案,可为您提供各种transitions
。 (淡入,翻转,缩放,覆盖等)
1-安装npm软件包:
yarn add vue-page-transition
2-注册插件:
import Vue from 'vue'
import VuePageTransition from 'vue-page-transition'
Vue.use(VuePageTransition)
3-用全局动画包装router-view
:
<vue-page-transition name="fade-in-right">
<router-view/>
</vue-page-transition>
在GitHub上了解更多信息: https://github.com/Orlandster/vue-page-transition