zoukankan      html  css  js  c++  java
  • vue图片懒加载

    vue-lazyload

    npm引入:npm i vue-lazyload -S
    CDN引入:[https://unpkg.com/vue-lazyload/vue-lazyload.js](https://unpkg.com/vue-lazyload/vue-lazyload.js)

    使用:

    main.js:

    import Vue from 'vue'
    import App from './App.vue'
    import VueLazyload from 'vue-lazyload'
     
    Vue.use(VueLazyload)
     
    // or with options
    Vue.use(VueLazyload, {
      preLoad: 1.3,
      error: 'dist/error.png',
      loading: 'dist/loading.gif',
      attempt: 1
    })
     
    new Vue({
      el: 'body',
      components: {
        App
      }
    })
    

    template:

    <ul>
      <li v-for="img in list">
        <img v-lazy="img.src" >
      </li>
    </ul>
    

    Constructor Options

    keydescriptiondefaultoptions
    preLoad proportion of pre-loading height 1.3 Number
    error src of the image upon load fail 'data-src' String
    loading src of the image while loading 'data-src' String
    attempt attempts count 3 Number
    listenEvents events that you want vue listen for ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend', 'touchmove'] Desired Listen Events
    adapter dynamically modify the attribute of element { } Element Adapter
    filter the image's listener filter { } Image listener filter
    lazyComponent lazyload component false Lazy Component
    dispatchEvent trigger the dom event false Boolean
    throttleWait throttle wait 200 Number
    observer use IntersectionObserver false Boolean
    observerOptions IntersectionObserver options { rootMargin: '0px', threshold: 0.1 } IntersectionObserver
    silent do not print debug info true

    Desired Listen Events

    Vue.use(VueLazyload, {
      preLoad: 1.3,
      error: 'dist/error.png',
      loading: 'dist/loading.gif',
      attempt: 1,
      // the default is ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend']
      listenEvents: [ 'scroll' ]
    })
    

    Methods

    Event Hook

    vm.Lazyload.on(event, callback) vm.Lazyload.off(event, callback) vm.Lazyload.once(event, callback)

    on Listen for a custom events loading, loaded, erroronce Listen for a custom event, but only once. The listener will be removed once it triggers for the first time.
    $off Remove event listener(s).

    vm.Lazyload.on

    Example
    vm.$Lazyload.$on('loaded', function ({ bindType, el, naturalHeight, naturalWidth, $parent, src, loading, error }, formCache) {
      console.log(el, src)
    })
    

    vm.Lazyload.once

    Example
    vm.$Lazyload.$once('loaded', function ({ el, src }) {
      console.log(el, src)
    })
    

    vm.Lazyload.off

    If only the event is provided, remove all listeners for that event

    Example
    function handler ({ el, src }, formCache) {
      console.log(el, src)
    }
    vm.$Lazyload.$on('loaded', handler)
    vm.$Lazyload.$off('loaded', handler)
    vm.$Lazyload.$off('loaded')
    

    Performance

    this.$Lazyload.$on('loaded', function (listener) {
      console.table(this.$Lazyload.performance())
    });
    
    this.$Lazyload.$once('loaded', function (listener) {
      console.table(this.$Lazyload.performance())
    })
    

    [原文具体api链接:](https://www.npmjs.com/package/vue-lazyload](https://www.npmjs.com/package/vue-lazyload)



    作者:O蚂蚁O
    链接:https://www.jianshu.com/p/d451dc1b5232
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    从搭eclipse环境到导入maven工程
    基于jquery的多选下拉列框再次更改样式和交互
    BootStrap的typeahead使用过程中遇到的问题
    Vue webapp项目通过HBulider打包原生APP
    微信相机
    前端小新手,记录项目中不懂的问题
    判断pdf、word文档、图片等文件类型(格式)、大小的简便方法
    JavaScript学习笔记(一)——Map、Set与iterable
    oracle nvl函数
    mybaits中主键自动生成并返回主键
  • 原文地址:https://www.cnblogs.com/sexintercourse/p/13084830.html
Copyright © 2011-2022 走看看