Nuxt的路由動(dòng)畫(huà)效果案例
路由的動(dòng)畫(huà)效果,也叫作頁(yè)面的更換效果。Nuxt.js提動(dòng)兩種方法為路由提動(dòng)動(dòng)畫(huà)效果,一種是全局的,一種是針對(duì)單獨(dú)頁(yè)面制作。
全局路由動(dòng)畫(huà)
全局動(dòng)畫(huà)默認(rèn)使用page進(jìn)行設(shè)置,例如現(xiàn)在我們?yōu)槊總€(gè)頁(yè)面都設(shè)置一個(gè)進(jìn)入和退出時(shí)的漸隱漸現(xiàn)的效果。我們可以先在根目錄的assets/css下建立一個(gè)main.css文件。
/assets/css/main.css
.page-enter-active,.page-leave-active{
transition: opacity 2s;
}
.page-enter,.page-leave-active{
opacity: 0;
}
然后在nuxt.config.js里加入一個(gè)全局的css文件就可以了。
module.exports = {
/*
** Headers of the page
*/
head: {
title: 'delnuxt',
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: 'Nuxt.js project' }
],
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
],
},
css:['~assets/css/normailze.css','~assets/css/main.css'],
/*
** Customize the progress bar color
*/
loading: { color: '#3B8070' },
/*
** Build configuration
*/
build: {
/*
** Run ESLint on save
*/
extend (config, { isDev, isClient }) {
if (isDev && isClient) {
config.module.rules.push({
enforce: 'pre',
test: /\.(js|vue)$/,
loader: 'eslint-loader',
exclude: /(node_modules)/
})
}
}
}
}
這時(shí)候在頁(yè)面切換的時(shí)候就會(huì)有2秒鐘的動(dòng)畫(huà)效果了,但是你會(huì)發(fā)現(xiàn)一些頁(yè)面時(shí)沒(méi)有效果,這是因?yàn)槟銢](méi)有使用<nuxt-link>組件來(lái)制作跳轉(zhuǎn)鏈接。你需要進(jìn)更改。
比如改成如下:
<nuxt-link :to="{name:'news',params:{newsId:3306}}">NEWS</nuxt-link>
改過(guò)之后你就會(huì)看到有動(dòng)畫(huà)效果了。
單獨(dú)設(shè)置頁(yè)面動(dòng)效
想給一個(gè)頁(yè)面單獨(dú)設(shè)置特殊的效果時(shí),我們只要在css里改變默認(rèn)的page,然后在頁(yè)面組件的配置中加入transition字段即可。例如,我們想給about頁(yè)面加入一個(gè)字體放大然后縮小的效果,其它頁(yè)面沒(méi)有這個(gè)效果。
在全局樣式assets/main.css中添加以下內(nèi)容。
.test-enter-active,.test-leave-active{
transition: all 2s;
font-size: 12px;
}
.test-enter,.test-leave-active{
opacity: 0;
font-size: 40px;
}
然后在about/index.vue組件中設(shè)置
<script>
export default {
transition:'test'
}
</script>
補(bǔ)充知識(shí):vue-ssr框架nuxt填坑
Nuxt.js 1.0.0 初始化與依賴包安裝
vue init nuxt/started
npm install
npm run dev
npm run dev 報(bào)錯(cuò)
> nuxt-temp@1.0.0 dev E:\MaYunProject\nuxt-temp
> nuxt
E:\MaYunProject\nuxt-temp\node_modules\nuxt\dist\nuxt.js:79
async function promiseFinally(fn, finalFn) {
^^^^^^^^
SyntaxError: Unexpected token function
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (E:\MaYunProject\nuxt-temp\node_modules\nuxt\index.js:17:20)
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "G:\\node\\node.exe" "G:\\node\\node_modules\\npm\\bin\\npm-cli.js" "run" "dev"
npm ERR! node v6.11.4
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! nuxt-temp@1.0.0 dev: `nuxt`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nuxt-temp@1.0.0 dev script 'nuxt'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the nuxt-temp package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! nuxt
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs nuxt-temp
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls nuxt-temp
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! E:\MaYunProject\nuxt-temp\npm-debug.log
解決錯(cuò)誤
將node 升級(jí)到 node8.12.0
升級(jí)到nuxt-edge Nuxt.js 2.0
1、運(yùn)行 npm run dev報(bào)錯(cuò)
ERROR Failed to compile with 1 errors Module build failed (from ./node_modules/eslint-loader/index.js): TypeError: Cannot read property 'eslint' of undefined at Object.module.exports (.../node_modules/eslint-loader/index.js:148:18) You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file.
2、修正錯(cuò)誤:編輯nuxt.conf.js文件并將其更改為
- module.exports = {
+ export default {
// ...
build: {
/*
** Run ESLint on save
*/
- extend (config, { isDev, isClient }) {
- if (isDev && isClient) {
+ extend (config, { isDev }) {
+ if (isDev && process.client) {
config.module.rules.push({
enforce: 'pre',
test: /\.(js|vue)$/,
loader: 'eslint-loader',
exclude: /(node_modules)/
})
}
}
}
}
3、 重啟服務(wù),打開(kāi)瀏覽器并訪問(wèn):http://localhost:3000/。
以上這篇Nuxt的路由動(dòng)畫(huà)效果案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
教你使用vue-cli快速構(gòu)建的小說(shuō)閱讀器
這篇文章主要介紹了vue-cli構(gòu)建的小說(shuō)閱讀器,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
解決vue router組件狀態(tài)刷新消失的問(wèn)題
這篇文章主要介紹了vue router組件狀態(tài)刷新消失的問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
vue中el-table前端如何導(dǎo)出excel數(shù)據(jù)表格
這篇文章主要介紹了vue中el-table前端如何導(dǎo)出excel數(shù)據(jù)表格,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
vue2結(jié)合echarts實(shí)現(xiàn)一個(gè)地圖的效果
這篇文章主要介紹了vue2結(jié)合echarts實(shí)現(xiàn)一個(gè)地圖的效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家何用vue和echarts實(shí)現(xiàn)一個(gè)地圖有一定的幫助,感興趣的朋友一起看看吧2024-03-03
Vue3封裝組件完整實(shí)例(帶回調(diào)事件)
Vue.js已成為現(xiàn)代Web開(kāi)發(fā)中不可或缺的技術(shù)之一,雖然Vue.js的一些基礎(chǔ)概念和語(yǔ)法比較易學(xué),但深入挖掘Vue.js的核心概念和功能需要更多的實(shí)踐,下面這篇文章主要給大家介紹了關(guān)于Vue3封裝組件(帶回調(diào)事件)的相關(guān)資料,需要的朋友可以參考下2023-06-06
前端記錄輸入框的歷史輸入記錄實(shí)現(xiàn)步驟(輸入框數(shù)據(jù)記憶功能)
這篇文章主要介紹了如何使用localStorage來(lái)記錄每個(gè)輸入框的歷史輸入記錄,并在用戶輸入時(shí)動(dòng)態(tài)更新這些記錄,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03
解決Vite無(wú)法分析出動(dòng)態(tài)import的類型,控制臺(tái)出警告的問(wèn)題
這篇文章主要介紹了解決Vite無(wú)法分析出動(dòng)態(tài)import的類型,控制臺(tái)出警告的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

