Code Monkey home page Code Monkey logo

lazyframe's Introduction

Lazyframe

Node.js Package npm version

Dependency-free library for lazyloading iframes. Demo

Why?

Because embedded content takes time to load.

  • Youtube โ€“ 11 requests โ‰ˆ 580kb
  • Google maps โ€“ 52 requests โ‰ˆ 580kb
  • Vimeo โ€“ 8 requests โ‰ˆ 145kb

Lazyframe creates a responsive placeholder for embedded content and requests it when the user interacts with it. This decreases the page load and idle time.

Lazyframe comes with brand-like themes for Youtube and Vimeo.

  1. Install
  2. Import
  3. Initialize
  4. Options

Install

NPM

$ npm install lazyframe --save

Bower

$ bower install lazyframe

Import

JavaScript ES6 imports

import lazyframe from "lazyframe";

Include JavaScript in html

<script src="dist/lazyframe.min.js"></script>

Sass import

@import 'src/scss/lazyframe'

Include css in html

<link rel="stylesheet" href="dist/lazyframe.css" />

Initialize

// Passing a selector
lazyframe(".lazyframe");

// Passing a nodelist
let elements = document.querySelectorAll(".lazyframe");
lazyframe(elements);

// Passing a jQuery object
let elements = $(".lazyframe");
lazyframe(elements);

Options

You can pass general options to lazyframe on initialization. Element-specific options (most options) are set on data attributes on the element itself.

General options and corresponding defaults

lazyframe(elements, {
  debounce: 250,
  lazyload: true,
  autoplay: true,

  // Callbacks
  onLoad: (lazyframe) => console.log(lazyframe),
  onAppend: (iframe) => console.log(iframe),
  onThumbnailLoad: (img) => console.log(img),
});

debounce

Value (in milliseconds) for when the update function should run after the user has scrolled. More here

lazyload

Set this to false if you want all API calls and local images to be loaded on page load (instead of when the element is in view).

autoplay

Set this to false to remove autoplay from the allow attribute on the iframe tag i.e if set this to false if you want don't want your Youtube video to automatically start playing once the user clicks on the play icon.

onLoad

Callback function for when a element is initialized.

onAppend

Callback function for when the iframe is appended to DOM.

onThumbnailLoad

Callback function with the thumbnail URL

Element-specific options

<div
  class="lazyframe"
  data-vendor=""
  data-title=""
  data-thumbnail=""
  data-src=""
  data-ratio="1:1"
  data-initinview="false"
  data-autoplay="false"
></div>

data-vendor

Attribute for theming lazyframe. Currently supported values are youtube, youtube_nocookie and vimeo.

data-title

Attribute for custom title. Leave empty to get value from noembed.com.

data-thumbnail

Attribute for custom thumbnail. Leave empty to get value from noembed.com.

data-src

The source of what you want to lazyload.

data-ratio

The ratio of the lazyframe. Possible values: 16:9, 4:3, 1:1

data-initinview

Set this to true if you want the resource to execute (for example video to play) when the element is in view.

License

MIT. ยฉ 2016 Viktor Bergehall

lazyframe's People

Contributors

vb avatar niftylettuce avatar xhmikosr avatar dependabot[bot] avatar hunzaboy avatar theel0ja avatar joe1chen avatar martify avatar danbovey avatar bitbruder avatar gg avatar pan93412 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.