Giter Club home page Giter Club logo

ab-interchange's Introduction

AB-interchange

AB-interchange is a small, dependencie free and vanilla JavaScript component that conditionnaly load things depending on media queries and it also has a lazy-loading option:

  • img
  • picture
  • background-image
  • HTML content (Ajax)

It's damn small: about 1800 bytes (uglyfied and GZipped). It is used in the French website of ENGIE.

Have a look at the Codepen demonstration.

Maintainability

Install

Install with npm:

npm install --save ab-interchange

Install with yarn:

yarn add ab-interchange

Setup

You can then import it in your JS bundle (webpack, ES6, browserify...):

import abInterchange from 'ab-interchange';

Or loading the js right before </body> if you are not using a builder.

Because of the usage of matchMedia and requestAnimationFrame, compatibility start with IE 10. To rise compatibility up to IE 9, you can add matchMedia polyfill and requestAnimationFrame polyfill.

Usage

Follow AB-mediaQuery readme to configure it the way you like depending on your needs. For exemple:

abMediaQuery({
  bp: {
    smallOnly:  'screen and (max-width: 767px)',
    mediumOnly: 'screen and (min-width: 768px) and (max-width: 1024px)',
    medium:     'screen and (min-width: 768px)',
    largeOnly:  'screen and (min-width: 1025px) and (max-width: 1280px)',
    large:      'screen and (min-width: 1025px)'
  }
});

Then you only need to initialize with AB.interchange() or with some options:

abInterchange({
  lazy      : true, // or false
  offscreen : 1.5,  // load items only when in the view + 0.5 by default
});

Then use data-ab-interchange attribute to pass options.

data-ab-interchange-src attribute is where you define different sources and breakpoints defined with AB-mediaQuery. It should contain a list of arrays with the path to the asset and the breakpoint name. Beware to respect mobile first order. Order is VERY important!

Examples

img

<img
  alt=""
  data-ab-interchange='{"lazy": false}"'
  data-ab-interchange-src="[xxx, smallOnly], [xxx, medium]"/>

picture

<picture>
  <source srcset="xxx" media="(min-width: 80em)"/>
  <source srcset="xxx" media="(min-width: 64em)"/>
  <source srcset="xxx" media="(min-width: 48em)"/>
  <source srcset="xxx"/>
  <img
    alt=""
    data-ab-interchange='{"lazy": false}"'
    data-ab-interchange-src="[xxx, smallOnly], [xxx, medium]"/>
</picture>

background-image

<div
  data-ab-interchange='{"mode": "background", "lazy": true, "offscreen": 1.5}"'
  data-ab-interchange-src="[xxx, smallOnly], [xxx, medium]">
</div>

XMLHttpRequest content (Ajax)

<div
  data-ab-interchange='{"mode": "ajax", "lazy": false}'
  data-ab-interchange-src="[xxx, smallOnly], [xxx, mediumOnly]">
</div>

JS event

replaced.ab-interchange event is automatically triggered when an IMG (or else) changed. For IMG and HTML, it's fired only when the new content is loaded, for background-image, immediatly, because it does not impact the layout:

window.addEventListener('replaced.ab-interchange', function(e){
  console.log(e.detail.element);
});

ab-interchange's People

Contributors

lordfpx avatar

Watchers

 avatar James Cloos 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.