您好,登錄后才能下訂單哦!
這篇文章主要介紹“Vue.js中如何實(shí)現(xiàn)一個(gè)上下滾動(dòng)加載組件”,在日常操作中,相信很多人在Vue.js中如何實(shí)現(xiàn)一個(gè)上下滾動(dòng)加載組件問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Vue.js中如何實(shí)現(xiàn)一個(gè)上下滾動(dòng)加載組件”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
組件代碼
// scrollLoader.vue // 滾動(dòng)加載組件 <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 :> <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)滾動(dòng)條 'minHeight': { type: Number, default: 800 }, }, created(){ }, computed: { realMinHeight(){ return this.minHeight + 30 } }, data() { return { topLoading: false, bottonLoading: false, stopTopLoading: false, //是否停止傳播滾動(dòng)到頂部事件 stopBottonLoading: false, //是否停止傳播滾動(dòng)到底部事件 } }, 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>
到此,關(guān)于“Vue.js中如何實(shí)現(xiàn)一個(gè)上下滾動(dòng)加載組件”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。