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

lh-prelazy

v0.1.2

Published

This is a preload and lazyload tool

Downloads

4

Readme

This is a preload and lazyload tool

🏠 Homepage

Example

Explain

优化页面加载速度、减轻服务器压力、节约流量、提高用户体验

Install

yarn add lh-prelazy --save (推荐)

cnpm i lh-prelazy --save

npm i lh-prelazy --save

Usage

HTML-预加载

  1. 首先定义您的HTML代码,如果您需要预加载,必须要有 #preloading 容器来管理您的预加载动画。
  • Tips
  <div id="preloading">
    <!-- 预加载动画 -->
  </div>

  <div id="container">
    <!-- 内容 -->
  </div>
  • 为什么必须要用#preloading 容器来管理您的预加载动画。
  您只要规定该预加载容器`#preloading`,我们会根据预加载结束后,隐藏您的预加载动画。
  document.getElementById('preloading').style.display = 'none'
  1. 设置您的首屏元素属性为pre="preloading",预加载动画需要等待的预加载元素。
  • Tips
  <img src="./images/1.jpeg" alt="pre1" pre="preloading">
  • 为什么必须要用pre="preloading",来设置您的预加载元素属性。
  我们将会筛选判断您设置的首屏预加载元素是否加载成功,以此隐藏与加载动画。
  preItem[i].onload = function () {
    if (preItem.length === imgLoad.length) {
      preloadElm.style.display = 'none'
    }
  }

HTML-懒加载

  1. 设计您的懒加载图片 src 设置为 data-src && 设计您的元素类名为 .lazy
  • Tips
  <img data-src="./images/1.jpeg" alt="pre1" class="lazy">
  • 为什么设置data-sr .lazy
  我们将筛选您的懒加载元素,根据元素是否出现在视口中,动态将 data-src -> src
  if (lazyItem[i].getAttribute('pre') || lazyItem[i].offsetTop < seeHeight + scrollTop){}
  lazyItem[i].setAttribute('src', lazyItem[i].getAttribute('data-src'));
  1. 设计您的懒加载展示动画,格式必须为 .lazy-${animate}, animate为你自己设置的动画类名。
  • Tips
  <img data-src="./images/1.jpeg" alt="pre1" class="lazy lazy-fadeIn">
  
  @keyframes fadeIn {
    from {
        opacity: 0;
        visibility: hidden;
    }
    to {
        opacity: 1;
        visibility: visible;
    }
  }
  .fadeIn {
    animation-name: fadeIn;
    animation-duration: 2s;
  }
  • 为什么要这样写?
  我们动态会将您设置的`.lazy-${animate}`转化为 `.animate`
   const setClassName = () => { // lazy-fadeIn -> fadeIn
     let lazyClassStrs = lazyItem[i].getAttribute('class');
     let lazyClassArr = lazyClassStrs.split(' ');
     for (let item of lazyClassArr) {
       if (item.includes('lazy-')) {
         let classAnimate = item.substr(5);
         lazyItem[i].className += ` ${classAnimate}`;
       };
     };
   };

css

  1. 建议您的预加载容器这样设计。
    #preloading {
      display: block;
      position: fixed;
      z-index: 999;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      width: 100vw;
      height: 100vh;
      background-color: #374140;
    }
  1. 建议您的预加载动画可从以下查找并设计。

图片加载失败替代方案

  new Prelazy({
    errorSrc: '替代图片加载不出来的背景图.png'
  })

js

  1. 引入我们已经下好的依赖。
  import Prelazy from 'lh-prelazy'
  new Prelazy({
    errorSrc: '../images/error.png'
  })

Author

👤 LOUSANPANG

🤝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2019 LOUSANPANG. This project is ISC licensed.