一个Vue.js 2.0指令来检测图像加载

Vue.ImagesLoaded 是一个Vue.js 2.0指令,用于检测图像何时加载或确认损坏。它允许在图像加载后触发回调,并与vue.isotope集成以实现图像加载时的布局更新。提供多种使用方式,包括直接使用、带参数和修饰符的使用。通过ImagesLoaded实例,可以访问每个图像的LoadingImage信息,包括元素和加载状态。
摘要由CSDN通过智能技术生成

Vue.ImagesLoaded (Vue.ImagesLoaded)

A Vue.js 2.0 directive to detect when images have been loaded, based on imagesLoaded

一个Vue.js 2.0指令,用于根据imagesLoaded检测何时加载了图像

This directive allows to get a callback when children images are loaded in a container element. Plays nicely with vue.isotope to allow re-layout when images are loaded.

当子图像加载到容器元素中时,此伪指令允许获取回调。 与vue.isotope配合使用,可以在加载图像时重新布局。

VueImagesLoaded

典型用法 (Typical usage)

<div v-images-loaded:on.progress="imageProgress">
	<div v-for="element in list">
		<img :src="element.src">
	</div>
</div>
import imagesLoaded from 'vue-images-loaded'

export default {
    directives: {
        imagesLoaded
    },
    methods: {
        imageProgress(instance, image ) {
        const result = image.isLoaded ? 'loaded' : 'broken';
        console.log( 'image is ' + result + ' for ' + image.img.src );
    }

同位素实例 (Isotope Example)

<isotope ref="cpt" :options='getOptions()' v-images-loaded:on.progress="layout" :list="list">
    <div v-for="element in list" :key="element.id"  @click="selected=element">
        {{element.name}}
        <br>
        {{element.id}}
        <img :src="element.src" alt="Not found">
    </div>
</isotope>
import imagesLoaded from 'vue-images-loaded'

export default {
    directives: {
        imagesLoaded
    },
    methods: {
        layout () {
            this.$refs.cpt.layout('masonry');
        }     
    }

API (API)

使用指令 (Using directive)

  • When used without argument nor modifiers:

    当不带参数或修饰符使用时:

<div v-images-loaded:"loaded">

Directive value:

指令值:

function loaded(instance){
    //...
}

loaded is a Function triggered after all images have been either loaded or confirmed broken. Function parameter: ImagesLoaded instance

加载是在所有图像都已加载或已确认损坏后触发的Function 。 函数参数:ImagesLoaded实例

  • When used with on argument but no modifiers:

    on参数一起使用但不带修饰符时:

<div v-images-loaded.on:"listener">

Directive value:

指令值:

listener:{
    done(instance){
        //...
    },
    fail(instance){
        //...
    }
}

listener is an Object containing callback functions. Function should be named and will received arguments as described in Imagesloaded

侦听器是一个包含回调函数的Object 。 函数应被命名,并将如Imagesloaded中所述接收参数

  • When used with on argument and modifier:

    on参数和修饰符一起使用时:

<div v-images-loaded.on.progress:"callback">

Directive value:

指令值:

function callback(instance, img){
    //...
}

callback is a Function triggered acording to modifier name always, done, fail, progress. Function parameter: ImagesLoaded instance, and image information for progess only.

回调是根据修改器名称alwaysdonefailprogress触发的Function 。 功能参数:ImagesLoaded实例,对图像信息progess只。

ImagesLoaded实例 (ImagesLoaded instance)

  • Properties:

    特性:

    • imagesLoaded.images

      imagesLoaded.images

      Array of LoadingImage instances for each image detected

      每个检测到的图像的LoadingImage实例的Array

LoadingImage实例 (LoadingImage instance)

  • Property:

    属性:

    • LoadingImage.img

      LoadingImage.img

      Image - The img element

      Image -img元素

    • LoadingImage.isLoaded

      LoadingImage.isLoaded

      Boolean - true when the image has succesfully loaded

      Boolean -成功加载图像时为true

安装 (Installation)

  • Available through bower and npm:

    可通过bower和npm获得:

npm install vue-images-loaded --save
bower install vue.ImagesLoaded -save
  • 对于模块 (For Modules)
    // ES6
    import imagesLoaded from 'vue-images-loaded'
    ...
    export default {
          directives: {
              imagesLoaded,
          }
      ...
    
    // ES5
    var imagesLoaded = require('vue-images-loaded')
  • 对于<script>包括 (For <script> Include)

    Just include vueimagesloaded.js after imagesloaded.pkgd.min.js script.

    仅在imagesloaded.pkgd.min.js脚本之后包含vueimagesloaded.js

翻译自: https://vuejsexamples.com/a-vue-js-2-0-directive-to-detect-images-loading/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值