npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

my-lazyload-picture

v1.1.0

Published

piture-lazyload lib

Downloads

2

Readme

图片懒加载

图片懒加载是很多门户网站会用到的功能,一个网页的图片不需要在网页一开始就全部加载,可以延迟到图片出现在浏览器的可视框内时再去加载,用户体验会更好一些。

好处有如下:

  1. 节省用户流量
  2. 减轻服务器压力
  3. 浏览器可以更快响应触发window.onload事件

图片懒加载的逻辑

  1. 把所有需要懒加载的图片的src设置为加载中的显示图
  2. 把所有需要懒加载的图片的data-src设置为真正图片的URL
  3. 监听window.onscroll事件,并遍历所有的图片,如果图片在可视窗口的范围内则把src替换为data-src
  4. 因为window.onscroll事件会频繁触发,导致回调频繁触发,我们可以用防抖的方式优化

如何使用

安装:

npm i my-lazyload-picture --save

引用:

// CommonJS
const {initLazyPicture, lazyPicture} = require('my-lazyload-picture')

API:

/**
 * 懒加载的初始化工作
 * @param {*} selector 选择器,默认为所有img标签
 * @param {*} tempSrc 存放在dataset的键,默认为data-src
 * @param {*} loadingPitureURL 加载完成前的图片URL
 * @param {*} realPitureURLArray 真实图片URL数组
 */
function initLazyPicture(selector = 'img', tempSrc = 'src', loadingPitureURL, realPitureURLArray){...}

/**
 * 让img标签拥有懒加载功能
 * @param {String} selector 选择器,默认为所有img标签
 * @param {Number} awayFromInnerHeightBottom 距离可视窗口底部距离,默认为0
 * @param {String} tempSrc data-*中,*代表的字符串,默认为data-src
 * @param {Number} interval 防抖设计中的时间间隔,默认为150毫秒
 */
function lazyPicture(selector = 'img', awayFromInnerHeightBottom = 0, tempSrc = 'src', interval = 150){...}

演示地址

codepen-图片懒加载

也可以用浏览器打开test/test.html