寫一個(gè)Vue loading 插件
作者:imgss
出處:http://www.cnblogs.com/imgss
什么是vue插件?
從功能上說,插件是為Vue添加全局功能的一種機(jī)制,比如給Vue添加一個(gè)全局組件,全局指令等; 從代碼結(jié)構(gòu)上說,插件就是一個(gè)必須擁有install方法的對象,這個(gè)方法的接收的第一個(gè)參數(shù)是Vue構(gòu)造函數(shù),還可以接收一個(gè)可選的參數(shù),用于配置插件:var myplugin = { install:function(Vue, options){ ... }}
從意義上來說,正如jQuery的$.fn使jQuery有了一個(gè)龐大的生態(tài)一樣,Vue的插件機(jī)制使Vue形成了一個(gè)生態(tài)系統(tǒng),你可以開發(fā)一個(gè)插件給別人復(fù)用。
使用插件
使用一個(gè)插件,只要像下面這樣:
Vue.use(myPlugin)
寫一個(gè)loading插件
光說不練假把式,接下來寫一個(gè)loading插件練練手,這個(gè)插件被封裝成一個(gè)全局組件,實(shí)現(xiàn)下面的效果:
1 定義接口
我們希望應(yīng)用這個(gè)插件的方式如下:
<loading text=’imgss’ duration=’3’></loading>
其中,text用于定義loading動(dòng)畫顯示的文字,duration定義動(dòng)畫時(shí)間
2 實(shí)現(xiàn)靜態(tài)組件
新建一個(gè)loading.js文件:
let myPlugin = { install: function (Vue, options) { Vue.component(’loading’, { props: { text:{ type:String }, duration:{ type:String, default:’1s’//默認(rèn)1s } }, data: function() { return {}; }, template: ` <div class=’wrapper’> <div class=’loading’> <span style=’width:20px’ v-for=’char in text’>{{char}}</span> </div> </div> ` });
這里模板的作用在于,將輸入的字符串轉(zhuǎn)換成組成字符串的字符構(gòu)成的span元素;接下來,新建一個(gè)html文件:
<html> <head> <meta charset=’utf-8’> <title>插件</title> </head> <body> <div id='app'> <loading text=’imgss’></loading> <loading text=’我是一個(gè)粉刷匠’ duration=’2s’></loading> </div> <script src='http://cdn.bootcss.com/vue/2.4.2/vue.js'></script> <script src='http://www.cgvv.com.cn/bcjs/loading.js'></script> <script> Vue.use(myPlugin); var app = new Vue({ el: ’#app’, data: { } }); </script> </body></html>
這時(shí)基本上可以看到一個(gè)靜態(tài)效果。
3 加動(dòng)畫
給每個(gè)元素加上一個(gè)控制上下的animation
@keyframes move { 0% { margin-top: -10px; border-bottom: 1px solid; } 50% { margin-top: 10px; border-bottom: none; } 100% { margin-top: -10px; } }
除此之外,還有一下其他的公有樣式代碼,利用mounted生命周期函數(shù),動(dòng)態(tài)生成一個(gè)style標(biāo)簽,將css代碼插到文檔中:
mounted: function () { var cssFlag = false; return function () { if (cssFlag) { return; } var head = document.querySelector(’head’); var style = document.createElement(’style’); style.type = ’text/css’; style.innerText = ` .wrapper{ display: flex; justify-content: center; } .loading { display: flex; text-align: center; padding-top: 30px; height: 50px; justify-content: space-between; } .loading span { margin-top: 0; animation: ease infinite move; display: block; } @keyframes move { 0% { margin-top: -10px; border-bottom: 1px solid; } 50% { margin-top: 10px; border-bottom: none; } 100% { margin-top: -10px; } }`; head.appendChild(style); cssFlag = true; }; }(),
然后通過控制span的animation-delay來模擬曲線:
<span :style=’{ width: '20px', animationDuration: duration.indexOf('s') === -1 ? duration + 's' : duration , animationDelay: parseInt(duration)/text.length*index +'s' }’ v-for=’char,index in text’> {{char}} </span>
到這里,插件基本完成,看一下效果:
demo
代碼
codepen
以上就是寫一個(gè)Vue loading 插件的詳細(xì)內(nèi)容,更多關(guān)于Vue 插件的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python 批量下載bilibili視頻的gui程序2. ThinkPHP5 通過ajax插入圖片并實(shí)時(shí)顯示(完整代碼)3. WML語言的基本情況4. React優(yōu)雅的封裝SvgIcon組件示例5. ajax post下載flask文件流以及中文文件名問題6. 使用css實(shí)現(xiàn)全兼容tooltip提示框7. ASP中解決“對象關(guān)閉時(shí),不允許操作?!钡脑幃悊栴}……8. AJAX實(shí)現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺(tái)】9. el-table表格動(dòng)態(tài)合并相同數(shù)據(jù)單元格(可指定列+自定義合并)10. 利用CSS3新特性創(chuàng)建透明邊框三角
