├── .gitignore ├── LICENSE ├── README.md ├── lazyload.gif ├── package.json └── src ├── app.js ├── app.json ├── app.wxss ├── libs └── lazyload.js ├── pages └── index │ ├── index.js │ ├── index.wxml │ └── index.wxss ├── project.config.json └── utils └── util.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | 63 | .idea/ 64 | src/project.config.json 65 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 邹世杰 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lazyload 2 | 性能更优越的小程序懒加载库 3 | 4 | 5 | ![enter image description here](https://raw.githubusercontent.com/jayZOU/lazyload/master/lazyload.gif) 6 | 7 | ## install 8 | [链接下载](https://raw.githubusercontent.com/jayZOU/lazyload/master/src/libs/lazyload.js) 9 | 10 | ## Usage 11 | ```javascript 12 | //page.js 13 | import lazyLoad from '../../libs/lazyload'; 14 | let lazyload; 15 | Page({ 16 | data: { 17 | img: [] 18 | }, 19 | onLoad: function () { 20 | 21 | lazyload = new lazyLoad(this, { 22 | classNote: '.item-', //循环节点class 23 | initNum: 5, //初始化展示多少个节点 24 | limit: 5 //每次加载多少个节点 25 | }) 26 | }, 27 | 28 | onReady: function () { 29 | lazyload.observe(); 30 | }, 31 | }) 32 | ``` 33 | 初始化完成之后会在data添加`__LAZT_LOAD_COUNT`字段,用来标识当前加载到第几张图,业务方自行判断节点挂载,例如 34 | ```html 35 | //page.wxml 36 | 37 | 38 | 39 | 40 | 41 | 42 | ``` 43 | -------------------------------------------------------------------------------- /lazyload.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jayZOU/lazyload/c193f4a67b553bb06dae6b4c84d3c2a1029f88c9/lazyload.gif -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lazyload", 3 | "version": "0.1.1", 4 | "description": "性能更优越的小程序懒加载库", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/jayZOU/lazyload.git" 12 | }, 13 | "keywords": [ 14 | "lazyload" 15 | ], 16 | "author": "jayzou", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/jayZOU/lazyload/issues" 20 | }, 21 | "homepage": "https://github.com/jayZOU/lazyload#readme" 22 | } 23 | -------------------------------------------------------------------------------- /src/app.js: -------------------------------------------------------------------------------- 1 | //app.js 2 | App({ 3 | onLaunch: function () { 4 | 5 | } 6 | }) -------------------------------------------------------------------------------- /src/app.json: -------------------------------------------------------------------------------- 1 | { 2 | "pages":[ 3 | "pages/index/index" 4 | ], 5 | "window":{ 6 | "backgroundTextStyle":"light", 7 | "navigationBarBackgroundColor": "#fff", 8 | "navigationBarTitleText": "WeChat", 9 | "navigationBarTextStyle":"black" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/app.wxss: -------------------------------------------------------------------------------- 1 | /**app.wxss**/ 2 | .container { 3 | height: 100%; 4 | display: flex; 5 | flex-direction: column; 6 | align-items: center; 7 | justify-content: space-between; 8 | padding: 200rpx 0; 9 | box-sizing: border-box; 10 | } 11 | -------------------------------------------------------------------------------- /src/libs/lazyload.js: -------------------------------------------------------------------------------- 1 | export default class LazyLoad{ 2 | constructor(context, opt = {}){ 3 | this.page = context; 4 | 5 | this.classNote = opt.classNote || 'item-'; 6 | this.initNum = opt.initNum || 5; 7 | this.limit = opt.limit || 5; 8 | 9 | this.intersectionObserver = {}; 10 | 11 | this.page.setData({ 12 | __LAZT_LOAD_COUNT: this.initNum 13 | }) 14 | 15 | if(!this.isSupport()) console.error('wx.createIntersectionObserver is not a function') 16 | 17 | } 18 | 19 | observe(){ 20 | if(!this.isSupport()) return; 21 | const that = this; 22 | this.intersectionObserver = wx.createIntersectionObserver(); 23 | 24 | this.intersectionObserver.relativeToViewport({bottom: 100}).observe(this.classNote + this.page.data.__LAZT_LOAD_COUNT, (res) => { 25 | if(res.boundingClientRect.top > 0){ 26 | that.intersectionObserver.disconnect() 27 | that.page.setData({ 28 | __LAZT_LOAD_COUNT: that.page.data.__LAZT_LOAD_COUNT + that.limit 29 | }) 30 | that.observe(); 31 | } 32 | }) 33 | } 34 | 35 | isSupport(){ 36 | return !!wx.createIntersectionObserver 37 | } 38 | } -------------------------------------------------------------------------------- /src/pages/index/index.js: -------------------------------------------------------------------------------- 1 | //index.js 2 | //获取应用实例 3 | const app = getApp() 4 | 5 | 6 | import lazyLoad from '../../libs/lazyload'; 7 | 8 | let lazyload; 9 | 10 | Page({ 11 | data: { 12 | img: ["https://images.freeimages.com/images/small-previews/851/poppies-1369329.jpg", "https://images.freeimages.com/images/small-previews/53b/montenegro-sky-1370598.jpg", "https://images.freeimages.com/images/small-previews/6d5/lake-at-the-cottage-1372381.jpg", "https://images.freeimages.com/images/small-previews/e5f/pink-lotus-1396744.jpg", "https://images.freeimages.com/images/small-previews/b45/spring-colours-1375388.jpg", "https://images.freeimages.com/images/small-previews/773/koldalen-4-1384902.jpg", "https://images.freeimages.com/images/small-previews/6e4/cemetary-1387449.jpg", "https://images.freeimages.com/images/small-previews/bf2/fields-1-1370990.jpg", "https://images.freeimages.com/images/small-previews/950/serious-grasshopper-1-1056340.jpg", "https://images.freeimages.com/images/small-previews/002/two-gerbers-1408523.jpg", "https://images.freeimages.com/images/small-previews/c53/yellowstone-river-1361768.jpg", "https://images.freeimages.com/images/small-previews/256/spring-1376144.jpg", "https://images.freeimages.com/images/small-previews/e51/tokyo05-2-1447803.jpg", "https://images.freeimages.com/images/small-previews/0db/tropical-bird-1390996.jpg", "https://images.freeimages.com/images/small-previews/901/butterfly-dress-1520606.jpg", "https://images.freeimages.com/images/small-previews/ffa/water-lilly-1368676.jpg", "https://images.freeimages.com/images/small-previews/fec/sunset-rays-1391805.jpg", "https://images.freeimages.com/images/small-previews/176/mansion-in-the-canyon-1575490.jpg", "https://images.freeimages.com/images/small-previews/48d/marguerite-1372118.jpg", "https://images.freeimages.com/images/small-previews/99b/sunflowers-3-1393020.jpg", "https://images.freeimages.com/images/small-previews/9b6/among-giants-1375605.jpg", "https://images.freeimages.com/images/small-previews/03e/wild-flowers-1628445.jpg", "https://images.freeimages.com/images/small-previews/fb3/grass-1379193.jpg", "https://images.freeimages.com/images/small-previews/e71/frog-1371919.jpg", "https://images.freeimages.com/images/small-previews/981/cow-1380252.jpg", "https://images.freeimages.com/images/small-previews/5b7/on-the-road-7-1384791.jpg", "https://images.freeimages.com/images/small-previews/58f/double-bass-1423720.jpg", "https://images.freeimages.com/images/small-previews/e0c/hawaiin-sunset-1368289.jpg", "https://images.freeimages.com/images/small-previews/b74/wild-poppies-1384853.jpg", "https://images.freeimages.com/images/small-previews/241/night-fog-1521028.jpg", "https://images.freeimages.com/images/small-previews/bfd/clouds-1371838.jpg", "https://images.freeimages.com/images/small-previews/1e7/japanese-food-1327425.jpg", "https://images.freeimages.com/images/small-previews/716/flower-1372780.jpg", "https://images.freeimages.com/images/small-previews/85a/daisy-s-1375598.jpg", "https://images.freeimages.com/images/small-previews/8ee/multicolor-drop-2-1056473.jpg", "https://images.freeimages.com/images/small-previews/8a5/red-tulip-2-1401227.jpg", "https://images.freeimages.com/images/small-previews/859/burning-trees-1391193.jpg", "https://images.freeimages.com/images/small-previews/7c7/tulips-1531279.jpg", "https://images.freeimages.com/images/small-previews/615/corcovado-sunset-1527899.jpg", "https://images.freeimages.com/images/small-previews/700/road-to-nowhere-1383109.jpg", "https://images.freeimages.com/images/small-previews/0d6/blue-flowers-with-macro-4-1400913.jpg", "https://images.freeimages.com/images/small-previews/efb/lotus-flower-1382251.jpg", "https://images.freeimages.com/images/small-previews/b2d/kiwi-fruit-macros-1313905.jpg", "https://images.freeimages.com/images/small-previews/b85/bay-lrt-station-1626400.jpg", "https://images.freeimages.com/images/small-previews/9a4/large-pumpkin-1387927.jpg", "https://images.freeimages.com/images/small-previews/0cf/tulips-1-1377350.jpg", "https://images.freeimages.com/images/small-previews/f13/factory-1446641.jpg", "https://images.freeimages.com/images/small-previews/e2a/boise-downtown-1387405.jpg"] 13 | 14 | }, 15 | onLoad: function () { 16 | 17 | lazyload = new lazyLoad(this, { 18 | classNote: '.item-', //循环节点 19 | initNum: 5, //初始化展示多少个节点 20 | limit: 5 //每次加载多少个节点 21 | }) 22 | }, 23 | 24 | onReady: function () { 25 | lazyload.observe(); 26 | 27 | 28 | }, 29 | }) 30 | -------------------------------------------------------------------------------- /src/pages/index/index.wxml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/pages/index/index.wxss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jayZOU/lazyload/c193f4a67b553bb06dae6b4c84d3c2a1029f88c9/src/pages/index/index.wxss -------------------------------------------------------------------------------- /src/project.config.json: -------------------------------------------------------------------------------- 1 | { 2 | "description": "项目配置文件。", 3 | "packOptions": { 4 | "ignore": [] 5 | }, 6 | "setting": { 7 | "urlCheck": false, 8 | "es6": true, 9 | "postcss": true, 10 | "minified": true, 11 | "newFeature": true 12 | }, 13 | "compileType": "miniprogram", 14 | "libVersion": "2.1.3", 15 | "appid": "touristappid", 16 | "projectname": "lazyload", 17 | "condition": { 18 | "search": { 19 | "current": -1, 20 | "list": [] 21 | }, 22 | "conversation": { 23 | "current": -1, 24 | "list": [] 25 | }, 26 | "game": { 27 | "currentL": -1, 28 | "list": [] 29 | }, 30 | "miniprogram": { 31 | "current": -1, 32 | "list": [] 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /src/utils/util.js: -------------------------------------------------------------------------------- 1 | const formatTime = date => { 2 | const year = date.getFullYear() 3 | const month = date.getMonth() + 1 4 | const day = date.getDate() 5 | const hour = date.getHours() 6 | const minute = date.getMinutes() 7 | const second = date.getSeconds() 8 | 9 | return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') 10 | } 11 | 12 | const formatNumber = n => { 13 | n = n.toString() 14 | return n[1] ? n : '0' + n 15 | } 16 | 17 | module.exports = { 18 | formatTime: formatTime 19 | } 20 | --------------------------------------------------------------------------------