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

easy-lazyload

v1.0.1

Published

原生js实现的图片懒加载库。 预览地址: https://sl1673495.github.io/easy-lazyload/

Downloads

3

Readme

easy-lazyload

原生js实现的图片懒加载库。 预览地址: https://sl1673495.github.io/easy-lazyload/

使用说明

  <div class="img-wrap">
    <img data-src="./assets/images/timg1.jpeg" />
    <img data-src="./assets/images/timg2.jpeg" />
    <img data-src="./assets/images/timg3.jpeg" />
  </div>

  最简单的用法: var loader = new EasyLazyLoad() 会将body作为滚动容器并找body下所有有data-src属性的img作为懒加载对象。

  如果需要确定一个容器内部的img需要懒加载:var loader = new EasyLazyLoad('.img-wrap')

  详细api:
  var loader = new EasyLazyLoad('.img-wrap', {
      loading, // 加载图片url
      error, // 加载失败图片url
      preLoad: 默认为1.3, // 预加载时机 如1.3则提前30%的视口高度就加载, 演示设为1是为了更好的展示效果
      observer: 默认为true // 是否使用 IntersectionObserver api来监听图片是否进入视口 默认为true 性能更好 如果浏览器不兼容则降级为事件监听

      // 下面的选项启用observer时无效
      throttleWait: 100, // 监听事件触发频率
      listenEvents, // 需要监听的事件 默认为'scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend', 'touchmove'

      // 生命周期
      onPreload: function() // 在loading图片预加载完成后调用, 可以防止用户看到一片空白的图片占位, 一般用不着。
      beforeMount: function(img) // 在img替换真实url前调用,在这里可以做一些动画的初始化工作 如img.style.opacity = 0
      mounted: function(img) // 在img替换真实url后调用, 在这里可以做一些动画的结束值替换 如 img.style.transition = 'all .5s';img.style.opacity = 1;
 })

 实例方法 
 loader.refresh(): 用于新增图片节点后对这些图片进行监听。
 loader.destory(): 移除一个实例的所有事件监听。

作为瀑布流插件使用

.loading作为列表底部的加载提示,比如一行'正在加载'的文字,和懒加载的逻辑一样,在合适的时机触发回调即可完成无限加载的效果。

  var loadMore = new EasyLazyLoad('.loading', {
      // 只要传入onLoadMore参数就会作为瀑布流插件加载
      onLoadMore: function (done) {
        ... // 到达底部执行的任何逻辑,一般用来插入节点,可以是同步也可以是异步, 异步的话注意done的调用时机
            // 注意插入节点以后一般让.loading类挤到可见范围之外,否则调用了done()以后会继续触发onLoadMore
            
        loader.refresh() // 监听新加入的图片节点

        done() 调用done方法, 通知loader可以开始下一次的监听,如果不调用则下次拉到loading的可视范围不会触发回调。        
      }    
 })

 index.html中为了演示效果全部做了延迟处理,实际场景业务情况调整