博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
解决v-lazy-image在qq浏览器x5内核下无效
阅读量:6857 次
发布时间:2019-06-26

本文共 2657 字,大约阅读时间需要 8 分钟。

  hot3.png

故障:在安卓qq浏览器中,v-lazy-image始终无法显示。

原因分析:

经调试,发现qq浏览器中IntersectionObserver缺少一个isIntersecting字段,而v-lazy-image插件是根据isIntersecting来显示图片的,故只能借助intersectionRatio判断。

另外还有一点差别就是如果v-lazy-image组件初始宽高为0的话,会导致intersectionRatio始终为0。

解决方案:

基于v-lazy-image源码修改自定义一个vue插件模块,代码见文末。

v-lazy-image使用的时候设置 src-placeholder ,保证v-lazy-image有个初始大小,这里我随便制作了一个 1x1 的空白图片,示例代码如下

main.js 修改

import { VLazyImagePlugin } from './plugins/VLazyImage'// polyfillrequire('intersection-observer')Vue.use(VLazyImagePlugin)

组件:

修改后的插件源码:plugins/VLazyImage

const VLazyImageComponent = {  props: {    src: {      type: String,      required: true    },    srcPlaceholder: {      type: String,      default: ''    },    srcset: {      type: String    },    intersectionOptions: {      type: Object,      default: () => ({})    },    usePicture: {      type: Boolean,      default: false    }  },  inheritAttrs: false,  data: () => ({ observer: null, intersected: false, loaded: false }),  computed: {    srcImage () {      return this.intersected ? this.src : this.srcPlaceholder    },    srcsetImage () {      return this.intersected && this.srcset ? this.srcset : false    }  },  methods: {    load () {      if (this.$el.getAttribute('src') !== this.srcPlaceholder) {        this.loaded = true        this.$emit('load')      }    }  },  render (h) {    let img = h('img', {      attrs: {        src: this.srcImage,        srcset: this.srcsetImage      },      domProps: this.$attrs,      class: {        'v-lazy-image': true,        'v-lazy-image-loaded': this.loaded      },      on: { load: this.load }    })    if (this.usePicture) {      return h('picture', { on: { load: this.load } }, this.intersected ? [ this.$slots.default, img ] : [])    } else {      return img    }  },  mounted () {    if ('IntersectionObserver' in window) {      this.observer = new IntersectionObserver(entries => {        const image = entries[0]        if (image.isIntersecting || image.intersectionRatio) {          this.intersected = true          this.observer.disconnect()          this.$emit('intersect')        }      }, this.intersectionOptions)      this.observer.observe(this.$el)    } else {      console.error(        "v-lazy-image: this browser doesn't support IntersectionObserver. Please use this polyfill to make it work https://github.com/w3c/IntersectionObserver/tree/master/polyfill."      )    }  },  destroyed () {    this.observer.disconnect()  }}export default VLazyImageComponentexport const VLazyImagePlugin = {  install: (Vue, opts) => {    Vue.component('VLazyImage', VLazyImageComponent)  }}

转载于:https://my.oschina.net/cxz001/blog/3039698

你可能感兴趣的文章
There is an overlap in the region chain修复
查看>>
手动配置网卡配置文件ifcfg-eth0
查看>>
重载(初学)
查看>>
《陶哲轩实分析》——给读者的一点建议
查看>>
python IO编程-StringIO和BytesIO
查看>>
线程同步中的锁
查看>>
Ubuntu 14.04安装Matlab 2013b
查看>>
【百度地图API】如何制作“从这里出发”“到这里去”——公交篇
查看>>
服务器请求
查看>>
java实现屏幕截屏功能
查看>>
实验四--恶意代码技术
查看>>
Golang 并发简介
查看>>
操作系统的启动(以Linux为例)
查看>>
课后作业
查看>>
laravel ORM 命令2
查看>>
java笔记javaweb部分
查看>>
浏览器插件 - 通用注入模版JS
查看>>
android布局学习
查看>>
js中createlement和creatTextnode属性
查看>>
DL4NLP——词表示模型(二)基于神经网络的模型:NPLM;word2vec(CBOW/Skip-gram)...
查看>>