• vue-lazyload 图片依赖加载


    一. vue lazyload插件:

    插件地址:https://github.com/hilongjw/vue-lazyload   (点击里面的demo进入可以查看使用代码  https://github.com/hilongjw/vue-lazyload#demo)

    demo:http://hilongjw.github.io/vue-lazyload/

    二. 简单使用实例:

    这个插件还是蛮好用的,就是感觉这个插件的开发文档有点太啰嗦了,一股脑把所有的api扩展都罗列出来,源码中并没有可以运行的实例提供。

    其实这个插件做简单使用的话是很简单的,看官方文档的话反而被误导了,可以先按下边的实例实现简单引用,后边再根据开发文档做扩展。

    1. 安装插件:

    npm install vue-lazyload --save-dev

    2. main.js引入插件:

    import VueLazyLoad from 'vue-lazyload'
    Vue.use(VueLazyLoad,{
        error:'./static/error.png',
        loading:'./static/loading.png'
    })

    3. vue文件中将需要懒加载的图片绑定 v-bind:src 修改为 v-lazy 

    <img class="item-pic" v-lazy="newItem.picUrl"/>

     三.功能扩展:

    图片懒加载的简单效果已经实现了,然后就可以按这开发文档的api进行扩展了:

    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

    (想要监听的vue事件)

    默认['scroll']可以省略,

    当插件跟页面中的动画或过渡等事件有冲突是,

    可以尝试其他选项

    ['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

     
    四.vue-lazyload使用中遇到的问题:

    1.使用 vue-lazyload 当需要动态切换图片时,DOM绑定的图片不会变,是需要加个 key,遂加之则图片就可以动态切换了,

    <img v-lazy="ImgSrc" :key="ImgSrc">

     2.

    使用vue-cli脚手架快速生成的框架中, 
    src**同级目录**中有static文件夹 
    src**子文件夹**中有assets文件夹 
    在使用vue-lazyload,设置error或loading属性的图片路径时,

    • 图片在assets文件夹,就需要使用require()进行引入。
    Vue.use(VueLazyload, {
      preLoad: 1.3,
      loading: require('./assets/123.gif'),
      attempt: 1
    })
    • 图片在static文件夹,就可以直接写路径了
    Vue.use(VueLazyload, {
      preLoad: 1.3,
      loading: '../static/123.gif',
      attempt: 1
    })

    vue-lazyload是在main.js文件中引入,不会被webpack进行编译,src中的文件会被webpack编译,包括assets,assets文件夹中的图片地址,会在编译过程中改变。因此vue-lazyload无法正确获得图片地址,就不能显示图片了。

    注:以上文件时综合了几个网络大佬的成果整理而成……

  • 相关阅读:
    Edge 修改使用的默认搜索引擎
    VSCode 插件之
    Visual Studio Code 插件之
    一个随笔
    COGS 2479 偏序 题解
    [NOIP2015] 子串substring 题解
    [CQOI2011]动态逆序对
    树套树三题 题解
    一个随笔
    HEOI 2016 游记
  • 原文地址:https://www.cnblogs.com/bester-ace/p/9450661.html
Copyright © 2020-2023  润新知