Giter Site home page Giter Site logo

echo's Introduction

Echo.js

Forked from Echo.

Echo 是一个轻量的 JS 图片懒加载插件,不依赖其它库。支持 IE8+。

使用示例:

获取 Echo.js

使用 NPM:

Amaze UI 只是添加了使用示例并发布到 NPM,代码与官方发布的版本保持一致。

npm install amazeui-echo

使用 Bower:

bower install echojs

使用方法

在图片上添加 data-echo 属性填写图片的真实地址;如果想懒加载背景图片,可以使用 `data-echo-background' 属性填写真实地址。

<img src="img/blank.gif" alt="Photo" data-echo="img/photo.jpg">

<script src="path/to/echo.js"></script>
<script>
echo.init({
  offset: 100,
  throttle: 250,
  unload: false,
  callback: function (element, op) {
    console.log(element, 'has been', op + 'ed')
  }
});

// echo.render(); is also available for non-scroll callbacks
</script>

API 说明

.init() (options)

init() API 有以下几个选项:

  • offset

    类型:Number|String,默认: 0

    offset 用于设置距离视口多远(水平、垂直方向)时开始载入图片, 为 0 时,图片进入视口以后立即加载。

  • offsetVertical

    类型: Number|String,默认: offset 选项的值

    设置图片距离视口垂直方向上距离多少时开始载入图片。

  • offsetHorizontal

    类型: Number|String,默认: offset 选项得值

    设置图片距离视口水平方向上距离多少时开始载入图片。

  • offsetTop

    类型: Number|String,默认: offsetVertical 的值

    图片距离顶部多少时开始载入图片。

  • offsetBottom

    类型:Number|String,默认:offsetVertical 的值

    图片距离底部多少时开始载入图片。

  • offsetLeft

    类型: Number|String,默认:offsetVertical 的值

    图片距离左侧多少时开始载入图片

  • offsetRight

    类型: Number|String,默认:offsetVertical 的值

    图片距离右侧多少时开始载入图片

  • throttle

    类型:Number|String,默认:250

    控制 window.onscroll 触发频率,以避免过于频繁导致性能问题,默认为 250 毫秒。

  • debounce

    类型: Boolean,默认:true

    debounce,用户停止滚动时才触发位置检测函数。

  • unload

    类型:Boolean,默认:false

    图片超过视口时不加载。

  • callback

    类型: Function

    回调函数接受两个参数,第一个为当前元素,第二个为操作状态(如 loadunload)。

下面的代码会在图片加载完成后添加 loaded class。

echo.init({
  callback: function(element, op) {
    if(op === 'load') {
      element.classList.add('loaded');
    } else {
      element.classList.remove('loaded');
    }
  }
});

.render()

调用此方法可以在不滚动窗口的情况下触发图片加载。

echo.render();

License

MIT license

echo's People

Contributors

afc163 avatar danielguillan avatar davidrapson avatar jonathan-fielding avatar lngramos avatar raphaeleidus avatar toddmotto avatar vzwick avatar wbinnssmith avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.