Vue.js上下滾動加載組件的實(shí)例代碼
更新時(shí)間:2017年07月17日 14:14:42 作者:doterlin
本篇文章主要介紹了Vue.js上下滾動加載組件的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
由于工作的需要并鑒于網(wǎng)上的vue.js滾動加載方案不合適,自己寫了一個(gè)簡單實(shí)用的。就短短的150行代碼。
組件代碼
// scrollLoader.vue // 滾動加載組件 <style scoped> .container-main {margin: 0 auto; overflow: auto; overflow-x: hidden; padding: 0;} .loading{ width: 100%; height: 40px; position: relative; overflow: hidden; text-align: center; margin: 5px 0 ; color: #999; font-size: 13px;} .loading-icon{color: #707070;}; .loader { font-size: 10px; margin: 8px auto; text-indent: -9999em; width: 24px; height: 24px; border-radius: 50%; background: #999; background: -moz-linear-gradient(left, #999 10%, rgba(255, 255, 255, 0) 42%); background: -webkit-linear-gradient(left, #999 10%, rgba(255, 255, 255, 0) 42%); background: -o-linear-gradient(left, #999 10%, rgba(255, 255, 255, 0) 42%); background: -ms-linear-gradient(left, #999 10%, rgba(255, 255, 255, 0) 42%); background: linear-gradient(to right, #999 10%, rgba(255, 255, 255, 0) 42%); position: relative; -webkit-animation: load3 1s infinite linear; animation: load3 1s infinite linear; } .loader:before { width: 50%; height: 50%; background: #999; border-radius: 100% 0 0 0; position: absolute; top: 0; left: 0; content: ''; } .loader:after { background: #f5f5f5; width: 72%; height: 75%; border-radius: 68%; content: ''; margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; } @-webkit-keyframes load3 { 0% { -webkit-transform: rotate(0deg); transform: rotate(0deg); } 100% { -webkit-transform: rotate(360deg); transform: rotate(360deg); } } @keyframes load3 { 0% { -webkit-transform: rotate(0deg); transform: rotate(0deg); } 100% { -webkit-transform: rotate(360deg); transform: rotate(360deg); } } </style> <template> <div id="scrollLoader-container" class="container-main"> <div class="loading" v-if="topLoading"> <div class="loader">加載中...</div> </div> <div :style="'min-height:' + realMinHeight + 'px; overflow-x:hidden'"> <slot></slot> </div> <div class="loading" v-if="bottonLoading"> <div class="loader">加載中...</div> </div> </div> </template> <script> export default { name: "scroll-loader", props: { //給slot傳一個(gè)最小值,保證一開始能出現(xiàn)滾動條 'minHeight': { type: Number, default: 800 }, }, created(){ }, computed: { realMinHeight(){ return this.minHeight + 30 } }, data() { return { topLoading: false, bottonLoading: false, stopTopLoading: false, //是否停止傳播滾動到頂部事件 stopBottonLoading: false, //是否停止傳播滾動到底部事件 } }, mounted(){ this.listenScroll(); }, methods: { listenScroll(){ var me = this; var topDone = (stopTopLoading) => { me.topLoading = false; if(stopTopLoading) me.stopTopLoading = true; }; var bottonDone = (stopBottonLoading) => { me.bottonLoading = false; if(stopBottonLoading) me.stopBottonLoading = true; }; setTimeout(function(){ var scrollContainer = document.getElementById('scrollLoader-container'); scrollContainer.onscroll = function(){ if(scrollContainer.scrollTop<=0 && !me.stopTopLoading){ if(me.topLoading) return; me.topLoading = true; me.$emit('scroll-to-top', topDone); } if((scrollContainer.offsetHeight + scrollContainer.scrollTop+1 >= scrollContainer.scrollHeight) && !me.stopBottonLoading){ if(me.bottonLoading) return; me.bottonLoading = true; scrollContainer.scrollTop += 40; me.$emit('scroll-to-botton', bottonDone); } } }, 50) }, } } </script>
源碼:https://github.com/doterlin/vue-wxChat
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- vue實(shí)現(xiàn)分頁加載效果
- vue實(shí)現(xiàn)的上拉加載更多數(shù)據(jù)/分頁功能示例
- Vue.js實(shí)現(xiàn)無限加載與分頁功能開發(fā)
- Javascript vue.js表格分頁,ajax異步加載數(shù)據(jù)
- vue.js 表格分頁ajax 異步加載數(shù)據(jù)
- vue element-ui table表格滾動加載方法
- Vue.js 的移動端組件庫mint-ui實(shí)現(xiàn)無限滾動加載更多的方法
- vue中實(shí)現(xiàn)滾動加載更多的示例
- Vue 無限滾動加載指令實(shí)現(xiàn)方法
- vue實(shí)現(xiàn)純前端表格滾動分頁加載
相關(guān)文章
修改el-form-item中的label里面的字體邊距或者大小問題
這篇文章主要介紹了修改el-form-item中的label里面的字體邊距或者大小問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10element修改form的el-input寬度,el-select寬度的方法實(shí)現(xiàn)
有時(shí)候像form表單這樣,頁面的input、select等寬度不一定會是一樣的,可能有些長,有些短,本文就介紹了如何element修改form的el-input寬度,el-select寬度的方法實(shí)現(xiàn),感興趣的可以了解一下2022-02-02vue?cli?局部混入mixin和全局混入mixin的過程
這篇文章主要介紹了vue?cli?局部混入mixin和全局混入mixin的過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05vue獲取當(dāng)前點(diǎn)擊的元素并傳值的實(shí)例
下面小編就為大家分享一篇vue獲取當(dāng)前點(diǎn)擊的元素并傳值的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03使用Vue-cli 中為單獨(dú)頁面設(shè)置背景圖片鋪滿全屏
這篇文章主要介紹了使用Vue-cli 中為單獨(dú)頁面設(shè)置背景圖片鋪滿全屏,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07