All Projects → shuizhubocai → vue-viewload

shuizhubocai / vue-viewload

Licence: MIT License
vue图片懒加载lazyload,依赖vue2.0以上版本。支持图片或者其他资源进入可视区域后加载。

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to vue-viewload

vue-lazy-load-img
vue版本的lazy-load-img图片懒加载插件
Stars: ✭ 31 (-59.74%)
Mutual labels:  vue2, vue-lazyload
v-currency
A plugin for formatting currency for different countries in Vue
Stars: ✭ 24 (-68.83%)
Mutual labels:  vue2
vue-tiny-lazyload-img
🐌 A small size Vue.js directive for lazy loading images using IntersectionObserver API
Stars: ✭ 91 (+18.18%)
Mutual labels:  vue2
Code-VueWapDemo
“Vue教程--Wap端项目搭建从0到1”的源码
Stars: ✭ 19 (-75.32%)
Mutual labels:  vue2
hoc-element-table
📦 A Vue 3.x Table Component built on Webpack 5
Stars: ✭ 26 (-66.23%)
Mutual labels:  vue2
ngx-progressive-image-loader
lazy load img/picture, prevent reflow and seo friendly.
Stars: ✭ 35 (-54.55%)
Mutual labels:  lazyload
security-code
A powerful security code input supports dynamic configuration of the number of input boxes.
Stars: ✭ 63 (-18.18%)
Mutual labels:  vue2
element-ui-saas-extend
基于ElementUI开发的SaaS业务扩展
Stars: ✭ 14 (-81.82%)
Mutual labels:  vue2
blog-frontend
前后端分离实践----基于Vue2.js框架博客前端
Stars: ✭ 32 (-58.44%)
Mutual labels:  vue2
bpit-vue
vue effects component package 🚀
Stars: ✭ 16 (-79.22%)
Mutual labels:  vue2
magento2-catalog-lazy-load
Improve the load time of your Magento 2 categories pages by loading your images on demand with our Lazy Load Extension
Stars: ✭ 56 (-27.27%)
Mutual labels:  lazyload
vue-timeline
a timeline for vue2 and bootstrap3
Stars: ✭ 59 (-23.38%)
Mutual labels:  vue2
huobi-PC
火币桌面客户端,基于electorn-vue开发
Stars: ✭ 56 (-27.27%)
Mutual labels:  vue2
vue-crumbs
a simple and useful breadcrumb for Vue2.js
Stars: ✭ 16 (-79.22%)
Mutual labels:  vue2
v-pip
🖼 Tiny vue wrapper for supporting native picture-in-picture mode.
Stars: ✭ 30 (-61.04%)
Mutual labels:  vue2
vue-pdf-app
VUEjs v2 PDF viewer based on Mozilla's PDFJS
Stars: ✭ 127 (+64.94%)
Mutual labels:  vue2
vue-snippets
Visual Studio Code Syntax Highlighting For Vue3 And Vue2
Stars: ✭ 25 (-67.53%)
Mutual labels:  vue2
v-page
A simple pagination bar, including length Menu, i18n support, based on Vue2.x
Stars: ✭ 85 (+10.39%)
Mutual labels:  vue2
office-fabric
johannes-z.github.io/office-fabric/
Stars: ✭ 12 (-84.42%)
Mutual labels:  vue2
vue2-animate
A port of Animate.css for use with transitions in Vue.js 2.0 / 3.0 and Alpine.js.
Stars: ✭ 1,338 (+1637.66%)
Mutual labels:  vue2

vue-viewload

npm npm npm

vue图片懒加载,依赖vue2.0以上版本。图片或者其他资源进入可视区域后加载。

支持浏览器

Chrome Firefox Safari Opera Edge IE
Latest Latest Latest Latest Latest 11+

例子预览

例子就是/demo目录下的文件

扫码查看例子入口

demo

安装vue-viewload

使用npm在本地安装:

npm install vue-viewload --save-dev

使用vue-viewload

js文件中

//需要引入vue,以及vue-viewload,下面的axios是ajax库,如果不需要可以不引用
import Vue from 'vue'
import VueViewload from 'vue-viewload'
import axios from 'axios'

//使用VueViewload
Vue.use(VueViewload)

new Vue({
    el: '#app',
    data: {
        pic: 'http://pics.sc.chinaz.com/files/pic/pic9/201701/bpic232.jpg',
        list: [
            'http://pics.sc.chinaz.com/files/pic/pic9/201702/zzpic1399.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201612/fpic9875.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201610/fpic8220.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201611/fpic8607.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201611/fpic8745.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201701/zzpic437.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201610/apic23881.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201608/fpic5949.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201608/fpic6419.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201609/fpic7403.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201609/fpic7317.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201605/fpic1376.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201606/apic21195.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201606/apic21465.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201604/apic20040.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201604/fpic914.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201604/fpic873.jpg',
            'http://pics.sc.chinaz.com/files/pic/pic9/201605/fpic1208.jpg'
        ]
    },
    methods: {
        //函数需要返回一个函数
        getAjaxContent: function() {
            return function() {
                axios({
                    method: 'get',
                    url: '../api/data.txt'
                }).then((response) => {
                    this.innerHTML = response.data;
                })
            }
        }
    }
})

html文件中,在要进行懒加载的元素上添加vue指令v-view,值为加载资源的URL或methods方法。需要懒加载的元素请尽量设置宽高样式

<div id="app">

    #给img元素的src赋值,设置图片未加载时显示的图片,可以图片设置为一个loading.gif动态加载图<br/>
    <img src="http://img.zcool.cn/community/0161f656b0663e6ac7256cb052d31a.gif" v-view="pic"><br/>

    #v-view的值不是变量的,值为单引号引起来的资源url地址<br/>
    <img v-view="'http://pics.sc.chinaz.com/files/pic/pic9/201701/bpic232.jpg'" style="height:200px;"><br/>

    #v-view的值是变量,pic值为资源url地址<br/>
    <img v-view="pic" style="height:200px;"><br/>

    #v-view的值是变量,变量值通过遍历list数组得来<br/>
    <img v-view="item" v-for="item in list" style="height:200px;display:block;"><br/>

    #v-view的值是methods方法,一般用在非img元素<br/>
    <div v-view="getAjaxContent()">加载中...</div>

</div>

设置懒加载选项

//options对象是可选的,用来设置懒加载选项
Vue.use(VueViewload, options)

options对象

名称 描述 默认值 值类型
defaultPic 图片未加载完显示的图片 base64的空白图片 String
errorPic 元素为img类型时,图片加载失败显示的图片 base64的空白图片 String
threshold 阀值,用来设置提前多少像素进入可视区域。负值表示提前进入,正值表示延迟进入, 0 Number
effectFadeIn 图片加载完是否淡入显示 false Boolean
callback 资源进入可视区域后执行的回调函数。接收两个参数callback(ele, src),ele是进入可视区域的元素,src是要加载的资源URL。可以用在非img元素进入可视区域加载 new Function Function

比如,设置默认loading.gif图,加载失败图,提前200px加载图片,图片淡入显示,图片进入可视区域执行回调函数:

Vue.use(VueViewload, {
    defaultPic: 'http://img.zcool.cn/community/0161f656b0663e6ac7256cb052d31a.gif',
    errorPic: 'http://a0.att.hudong.com/77/31/20300542906611142174319458811.jpg',
    threshold: -200,
    effectFadeIn: true,
    callback: function(ele, src) {
        ele.style.border = '1px solid red';
        console.log(ele.nodeName + '...' + src);
    }
})
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].