寫一個Vue loading 插件
作者:imgss
出處:http://www.cnblogs.com/imgss
什么是vue插件?
從功能上說,插件是為Vue添加全局功能的一種機(jī)制,比如給Vue添加一個全局組件,全局指令等; 從代碼結(jié)構(gòu)上說,插件就是一個必須擁有install方法的對象,這個方法的接收的第一個參數(shù)是Vue構(gòu)造函數(shù),還可以接收一個可選的參數(shù),用于配置插件:var myplugin = { install:function(Vue, options){ ... }}
從意義上來說,正如jQuery的$.fn使jQuery有了一個龐大的生態(tài)一樣,Vue的插件機(jī)制使Vue形成了一個生態(tài)系統(tǒng),你可以開發(fā)一個插件給別人復(fù)用。
使用插件
使用一個插件,只要像下面這樣:
Vue.use(myPlugin)
寫一個loading插件
光說不練假把式,接下來寫一個loading插件練練手,這個插件被封裝成一個全局組件,實(shí)現(xiàn)下面的效果:
1 定義接口
我們希望應(yīng)用這個插件的方式如下:
<loading text=’imgss’ duration=’3’></loading>
其中,text用于定義loading動畫顯示的文字,duration定義動畫時間
2 實(shí)現(xiàn)靜態(tài)組件
新建一個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元素;接下來,新建一個html文件:
<html> <head> <meta charset=’utf-8’> <title>插件</title> </head> <body> <div id='app'> <loading text=’imgss’></loading> <loading text=’我是一個粉刷匠’ duration=’2s’></loading> </div> <script src='http://cdn.bootcss.com/vue/2.4.2/vue.js'></script> <script src='http://www.gepszalag.com/bcjs/loading.js'></script> <script> Vue.use(myPlugin); var app = new Vue({ el: ’#app’, data: { } }); </script> </body></html>
這時基本上可以看到一個靜態(tài)效果。
3 加動畫
給每個元素加上一個控制上下的animation
@keyframes move { 0% { margin-top: -10px; border-bottom: 1px solid; } 50% { margin-top: 10px; border-bottom: none; } 100% { margin-top: -10px; } }
除此之外,還有一下其他的公有樣式代碼,利用mounted生命周期函數(shù),動態(tài)生成一個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
以上就是寫一個Vue loading 插件的詳細(xì)內(nèi)容,更多關(guān)于Vue 插件的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. idea導(dǎo)入maven項(xiàng)目的方法2. idea設(shè)置代碼格式化的方法步驟3. 為什么從類內(nèi)部訪問類變量需要“自我”。在Python中?4. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向5. spring boot 若依系統(tǒng)整合Ueditor部署時上傳圖片錯誤問題6. 將Oracle 10g內(nèi)置的安全特性用于PHP7. Ajax實(shí)現(xiàn)異步加載數(shù)據(jù)8. ajax請求添加自定義header參數(shù)代碼9. JSR 224 - Java EE 5 最終草案發(fā)布10. 教你在 IntelliJ IDEA 中使用 VIM插件的詳細(xì)教程
