国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

寫一個Vue loading 插件

瀏覽:87日期:2022-11-02 18:01:25

作者:imgss

出處:http://www.cnblogs.com/imgss

什么是vue插件?

從功能上說,插件是為Vue添加全局功能的一種機制,比如給Vue添加一個全局組件,全局指令等; 從代碼結(jié)構(gòu)上說,插件就是一個必須擁有install方法的對象,這個方法的接收的第一個參數(shù)是Vue構(gòu)造函數(shù),還可以接收一個可選的參數(shù),用于配置插件:

var myplugin = { install:function(Vue, options){ ... }}

從意義上來說,正如jQuery的$.fn使jQuery有了一個龐大的生態(tài)一樣,Vue的插件機制使Vue形成了一個生態(tài)系統(tǒng),你可以開發(fā)一個插件給別人復(fù)用。

使用插件

使用一個插件,只要像下面這樣:

Vue.use(myPlugin)

寫一個loading插件

光說不練假把式,接下來寫一個loading插件練練手,這個插件被封裝成一個全局組件,實現(xiàn)下面的效果:

寫一個Vue loading 插件

1 定義接口

我們希望應(yīng)用這個插件的方式如下:

<loading text=’imgss’ duration=’3’></loading>

其中,text用于定義loading動畫顯示的文字,duration定義動畫時間

2 實現(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.intensediesel.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>

到這里,插件基本完成,看一下效果:

寫一個Vue loading 插件

demo

代碼

codepen

以上就是寫一個Vue loading 插件的詳細(xì)內(nèi)容,更多關(guān)于Vue 插件的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 教育| 资中县| 综艺| 临夏市| 台江县| 台南市| 华亭县| 开鲁县| 宝鸡市| 杂多县| 获嘉县| 上饶县| 固始县| 察隅县| 疏勒县| 岳西县| 桐乡市| 扎囊县| 个旧市| 高阳县| 乌鲁木齐市| 牙克石市| 河间市| 安图县| 伊春市| 天全县| 宣汉县| 泗水县| 葵青区| 遂川县| 信宜市| 名山县| 大丰市| 桓仁| 蕉岭县| 古浪县| 林州市| 新干县| 道真| 蓬安县| 东光县|