Vue-img-preload

vue-img-preload

預(yù)加載頁(yè)面上的圖片資源,提高用戶體驗(yàn)

效果預(yù)覽

使用方法

瀏覽器中
  1. 下載vue-img-preload插件
npm install vue-img-preload
  1. 在頁(yè)面中引入dist目錄下的preload.min.js文件(確保此時(shí)頁(yè)面已經(jīng)引入vue.js),如
<script src="path/dist/preload.min.js"></script>
  1. 首先創(chuàng)建一個(gè)空的Vue實(shí)例,用于頁(yè)面中的事件監(jiān)聽。調(diào)用Vue.use方法裝載Preload插件晒衩,并傳入配置參數(shù),eachLoaded在每張圖片下載完成后都會(huì)執(zhí)行該方法, allLoaded則在所有圖片都下載完成后執(zhí)行奋构,此處分別觸發(fā)一個(gè)eachload,allload事件拱层,并傳回部分內(nèi)部處理參數(shù)弥臼,如已下載圖片數(shù)量,圖片下載完成后的event對(duì)象舱呻。
   // 非父子組件通信
    var bus = new Vue()
    // 安裝插件
    Vue.use(Preload, {
        eachLoaded: function(msg) {
            bus.$emit('eachload', msg)
        },
        allLoaded: function(msg) {
            bus.$emit('allload'醋火,msg)
        }
    })
  1. 此時(shí)Vue對(duì)象中添加一個(gè)v-preload指令,可以在Vue管理的結(jié)構(gòu)中使用箱吕,<div v-preload="imgs"></div>芥驳,并傳入?yún)?shù)。
// 頁(yè)面結(jié)構(gòu)
<div id="app">
        <div class="spinner" :class="[isLoaded ? 'loading' : '']">
            <div class="rect1"></div>
            <div class="rect2"></div>
            <div class="rect3"></div>
            <div class="rect4"></div>
            <div class="rect5"></div>
        </div>
        <div v-preload="imgs"></div>
        <div class="pics">
            <img v-for="img in imgs" :src="img" alt="" width=250 />
        </div>
    </div>
// 數(shù)據(jù)
 var vm = new Vue({
        el: '#app',
        data: function() {
            return {
                isLoaded: false,
                imgs: [
                    'https://github.com/DiligentYe/some-picture/blob/master/IMG_0923.JPG?raw=true',
                    'https://github.com/DiligentYe/some-picture/blob/master/IMG_3606.JPG?raw=true',
                    'https://github.com/DiligentYe/some-picture/blob/master/IMG_3657.JPG?raw=true',
                    'https://github.com/DiligentYe/some-picture/blob/master/IMG_3715.JPG?raw=true',
                    'https://github.com/DiligentYe/some-picture/blob/master/IMG_3775.JPG?raw=true',
                    'https://github.com/DiligentYe/some-picture/blob/master/IMG_3789.JPG?raw=true',
                    'https://github.com/DiligentYe/some-picture/blob/master/IMG_3791.JPG?raw=true',
                    'https://github.com/DiligentYe/some-picture/blob/master/IMG_3832.JPG?raw=true',
                    'https://github.com/DiligentYe/some-picture/blob/master/IMG_3833.JPG?raw=true',
                    'https://github.com/DiligentYe/some-picture/blob/master/IMG_3910.JPG?raw=true'
                ]
            }
        }
    })

    // 注冊(cè)eachload茬高,allload事件處理函數(shù)兆旬,當(dāng)所有圖片都下載完畢后,通過(guò)控制isLoaded的值怎栽,控制loading類丽猬,從而控制加載動(dòng)畫
    bus.$on('eachload', function(msg) {
        console.log(msg.total)
    })

    bus.$on('allload', function(msg) {
        vm.isLoaded = true
    })

注:可運(yùn)行vue-img-preload/test/web/simple.html查看效果

  1. 上述方法中宿饱,在裝載插件的時(shí)候統(tǒng)一配置eachLoaded,和allLoaded配置項(xiàng)脚祟,如果針對(duì)頁(yè)面中多個(gè)部分分別進(jìn)行配置谬以,此時(shí)需要給v-preload傳入一個(gè)Object參數(shù),其中包含imgs和options屬性由桌,分別對(duì)應(yīng)圖片地址數(shù)組和特定配置項(xiàng)为黎,如下:
infos: {
     imgs: ['https://github.com/DiligentYe/some-picture/blob/master/IMG_0923.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_3606.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_3657.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_3715.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_3775.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_3789.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_3791.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_3832.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_3833.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_3910.JPG?raw=true'],
     options: {
         allLoaded: function(msg) {
             bus.$emit('allload')
         }
     }
 }

datas: {
     imgs: ['https://github.com/DiligentYe/some-picture/blob/master/IMG_4030.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_4056.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_4967.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_4968.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_4969.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_4970.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_4971.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_4972.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_4973.JPG?raw=true', 'https://github.com/DiligentYe/some-picture/blob/master/IMG_4974.JPG?raw=true'],
     options: {
         type: 'order ',
         eachLoaded: function(msg) {
             bus.$emit('eachload', msg)
         },
         allLoaded: function(msg) {
             bus.$emit('allload')
         }
     }
 }

針對(duì)infos,只會(huì)在所有圖片下載完成的時(shí)候觸發(fā)allload行您,而datas铭乾,則設(shè)置了圖片有序下載,并且單張下載完畢和所有圖片下載完畢都會(huì)執(zhí)行相應(yīng)的動(dòng)作娃循。
注:可運(yùn)行vue-img-preload/test/web/index.html查看效果

組件中

大體用法與在頁(yè)面中一致炕檩,具體用法可參考vue-img-preload/test/module/test.js寫法,運(yùn)行結(jié)果可運(yùn)行npm run dev捌斧,啟動(dòng)一個(gè)服務(wù)器笛质,訪問(wèn)對(duì)應(yīng)地址即可。

配置參數(shù)

  1. eachLoaded(function): 每次加載成功圖片的行為骤星,默認(rèn)在控制臺(tái)打印one picture has been loaded经瓷。

  2. allLoaded(function): 所有圖片加載成功的行為,默認(rèn)在控制臺(tái)打印all picture has been loaded洞难。

  3. type(string): 圖片有序/無(wú)序加載舆吮,默認(rèn)為’disorder‘,采用無(wú)序加載队贱,如果type值不為’disorder‘色冀,則采用有序加載。使用有序加載時(shí)柱嫌,建議設(shè)置為‘order’锋恬。

  4. max: 一次加載的照片個(gè)數(shù),默認(rèn)值為1000编丘,當(dāng)圖片數(shù)量小于max時(shí)与学,加載所有圖片;當(dāng)圖片的數(shù)量大于max時(shí)嘉抓,采用分塊加載索守,當(dāng)前塊加載完畢后才會(huì)加載下一塊,每塊的長(zhǎng)度為max

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末抑片,一起剝皮案震驚了整個(gè)濱河市卵佛,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖截汪,帶你破解...
    沈念sama閱讀 222,464評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件疾牲,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡衙解,警方通過(guò)查閱死者的電腦和手機(jī)阳柔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)蚓峦,“玉大人盔沫,你說(shuō)我怎么就攤上這事》阖遥” “怎么了?”我有些...
    開封第一講書人閱讀 169,078評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵拟淮,是天一觀的道長(zhǎng)干茉。 經(jīng)常有香客問(wèn)我,道長(zhǎng)很泊,這世上最難降的妖魔是什么角虫? 我笑而不...
    開封第一講書人閱讀 59,979評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮委造,結(jié)果婚禮上戳鹅,老公的妹妹穿的比我還像新娘。我一直安慰自己昏兆,他們只是感情好枫虏,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,001評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著爬虱,像睡著了一般隶债。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上跑筝,一...
    開封第一講書人閱讀 52,584評(píng)論 1 312
  • 那天死讹,我揣著相機(jī)與錄音,去河邊找鬼曲梗。 笑死赞警,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的虏两。 我是一名探鬼主播愧旦,決...
    沈念sama閱讀 41,085評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼碘举!你這毒婦竟也來(lái)了忘瓦?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,023評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎耕皮,沒(méi)想到半個(gè)月后境蜕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,555評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡凌停,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,626評(píng)論 3 342
  • 正文 我和宋清朗相戀三年粱年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片罚拟。...
    茶點(diǎn)故事閱讀 40,769評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡台诗,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出赐俗,到底是詐尸還是另有隱情拉队,我是刑警寧澤,帶...
    沈念sama閱讀 36,439評(píng)論 5 351
  • 正文 年R本政府宣布阻逮,位于F島的核電站粱快,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏叔扼。R本人自食惡果不足惜事哭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,115評(píng)論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望瓜富。 院中可真熱鬧鳍咱,春花似錦、人聲如沸与柑。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)仅胞。三九已至每辟,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間干旧,已是汗流浹背渠欺。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留椎眯,地道東北人挠将。 一個(gè)月前我還...
    沈念sama閱讀 49,191評(píng)論 3 378
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像编整,于是被迫代替她去往敵國(guó)和親舔稀。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,781評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容