vue實(shí)現(xiàn)頁面加載動(dòng)畫效果
我們經(jīng)??吹綌?shù)據(jù)未出現(xiàn)時(shí),頁面中會(huì)有一條提示消息, 頁面正在加載中,如何實(shí)現(xiàn)該效果呢 ,請(qǐng)看下面代碼
<template> <section class="page" v-if="option" :style="{background: option.background,color: option.color||'#fff'}" :class="{'page-before': option.index < currentPage, 'page-after': option.index > currentPage, 'page-current': option.index === currentPage}"> <div :class="{'all-center': option.isCenter}"> <slot></slot> </div> </section> <section class="page" v-else>頁面正在渲染中。。。</section> </template>
有木有感覺很簡(jiǎn)單
下面上點(diǎn)干貨,實(shí)現(xiàn)頁面的動(dòng)畫效果
<template> <nav class="controller"> <button v-if="option.arrowsType" class="prev-btn" :class="{moving:option.arrowsType === 'animate'}" @click="changePage(prevIndex)"></button> <ul v-if="option.navbar"> <li v-for="index in pageNum" @click="changePage(index)" :class="{current:option.highlight && index === currentPage}" :key="'controller-'+index" :data-index="index" class="controller-item"></li> </ul> <button v-if="option.arrowsType" class="next-btn" :class="{moving:option.arrowsType === 'animate'}" @click="changePage(nextIndex)"></button> </nav> </template> <script> export default { name: 'page-controller', props: { pageNum: Number, currentPage: Number, option: { type: Object, default: { arrowsType: 'animate', navbar: true, highlight: true, loop: true //是否開啟滾動(dòng)循環(huán) } } }, methods: { changePage (index) { this.$emit('changePage', index); } }, computed: { nextIndex () { if (this.currentPage === this.pageNum) { if(this.option.loop){ return 1 }else{ return this.pageNum } } else { return this.currentPage + 1; } }, prevIndex () { if (this.currentPage === 1) { if(this.option.loop){ return this.pageNum }else{ return 1 } } else { return this.currentPage - 1; } } }, created () { if (this.option.navbar === undefined) { this.option.navbar = true; } }, mounted () { let _this = this; let timer = null; let start = 0; // 滾輪處理 function scrollHandler (direction) { // 防止重復(fù)觸發(fā)滾動(dòng)事件 if (timer != null) { return; } if (direction === 'down') { _this.changePage(_this.nextIndex); } else { _this.changePage(_this.prevIndex); } timer = setTimeout(function() { clearTimeout(timer); timer = null; }, 300); } // if (Object.hasOwnProperty.call(window,'onmousewheel')) { if (Object.hasOwnProperty.call(window,'onmousewheel')) { // 監(jiān)聽滾輪事件 window.addEventListener('mousewheel',function (event) { // IE/Opera/Chrome let direction = event.wheelDelta > 0 ? 'up':'down'; scrollHandler(direction); },false); } else { window.addEventListener('DOMMouseScroll',function (event) { // Firefox let direction = event.detail > 0 ? 'up':'down'; scrollHandler(direction); },false); } // 移動(dòng)端觸摸事件處理 window.addEventListener('touchstart', function (event) { start = event.touches[0].clientY; }) window.addEventListener('touchmove', function (event) { event.preventDefault(); }) window.addEventListener('touchend', function (event) { let spacing = event.changedTouches[0].clientY - start; let direction; if (spacing > 50) { direction = 'up'; scrollHandler(direction); } else if (spacing < -50) { direction = 'down'; scrollHandler(direction); } }) } } </script> <style scoped> .controller { position: fixed; right: 20px; top: 50%; z-index: 99; } .controller ul { transform: translate3d(0,-50%,0); list-style: none; margin: 0; padding: 0; } .controller-item { cursor: pointer; width: 20px; height: 20px; border-radius: 50%; margin-top: 10px; background-color: rgba(255, 255, 255, 0.3); transition: background-color 0.3s ease 0s; } .controller-item:hover { background-color: rgba(255, 255, 255, 0.7); } .controller-item.current { background-color: rgba(255, 255, 255, 1); } .prev-btn,.next-btn { cursor: pointer; display: block; text-align: center; width: 20px; height: 20px; position: fixed; left: 50%; margin-left: -10px; border: 4px solid #fff; background-color: transparent; outline: none; } .prev-btn { top: 80px; transform: rotate(-45deg); border-bottom-color: transparent; border-left-color: transparent; } .next-btn { bottom: 80px; transform: rotate(45deg); border-top-color: transparent; border-left-color: transparent; } .prev-btn.moving { animation: prev-up-down 0.7s linear 0s infinite; } .next-btn.moving { animation: next-up-down 0.7s linear 0s infinite; } @keyframes next-up-down { 0% { transform: translate3d(0,0,0) rotate(45deg); } 25% { transform: translate3d(0,6px,0) rotate(45deg); } 50% { transform: translate3d(0,0,0) rotate(45deg); } 75% { transform: translate3d(0,-6px,0) rotate(45deg); } 100% { transform: translate3d(0,0,0) rotate(45deg); } } @keyframes prev-up-down { 0% { transform: translate3d(0,0,0) rotate(-45deg); } 25% { transform: translate3d(0,-6px,0) rotate(-45deg); } 50% { transform: translate3d(0,0,0) rotate(-45deg); } 75% { transform: translate3d(0,6px,0) rotate(-45deg); } 100% { transform: translate3d(0,0,0) rotate(-45deg); } } </style>
本文已被整理到了《Vue.js前端組件學(xué)習(xí)教程》,歡迎大家學(xué)習(xí)閱讀。
關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue實(shí)現(xiàn)上拉加載下一頁效果的示例代碼
- vue前端頁面數(shù)據(jù)加載添加loading效果的實(shí)現(xiàn)
- vue實(shí)現(xiàn)滑動(dòng)到底部加載更多效果
- Vue ElementUI this.$confirm async await封裝方式
- vue+el使用this.$confirm,不能阻斷代碼往下執(zhí)行的解決
- Vue+Element-ui彈窗?this.$alert?is?not?a?function問題
- Vue給?elementUI?中的?this.$confirm、this.$alert、?this.$prompt添加按鈕加載效果
相關(guān)文章
vue uniapp 防止按鈕多次點(diǎn)擊的三種實(shí)現(xiàn)方式
最近的項(xiàng)目完成后,在性能優(yōu)化階段需要做按鈕的防止重復(fù)點(diǎn)擊功能,本文主要介紹了vue uniapp 防止按鈕多次點(diǎn)擊的三種實(shí)現(xiàn)方式,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08vue3?reactive響應(yīng)式依賴收集派發(fā)更新原理解析
這篇文章主要為大家介紹了vue3響應(yīng)式reactive依賴收集派發(fā)更新原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Vue?2?和?Vue?3?中?toRefs函數(shù)的不用用法
Vue?是一款流行的JavaScript?框架,用于構(gòu)建用戶界面,在Vue2和?Vue3中,都存在一個(gè)名為toRefs的函數(shù),但其行為在這兩個(gè)版本中有所不同,這篇文章主要介紹了Vue2和Vue3中toRefs的區(qū)別,需要的朋友可以參考下2023-08-08vue.js聲明式渲染和條件與循環(huán)基礎(chǔ)知識(shí)
這篇文章主要為大家詳細(xì)介紹了vue.js聲明式渲染和條件與循環(huán)的基礎(chǔ)知識(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07