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

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

lazyframe's Issues

Ratio issues with Vimeo videos on large widths

Steps to reproduce the issue on the demo page:

  1. Change the max-width to 1000px for the container
.container {
    max-width: 1000px;
    margin: 0 auto;
    width: 100%;
}
  1. Play a Vimeo video

The video won't occupy the full-space of the thumbnail image (see my screenshot):
lazyframe_vimeo

This doesn't seem to affect Youtube's videos though.

How to get thumbnail?

Hello. How to get thumbnail to manipulate with image? Is there simple way to wait untill it download, and then to get it without timeout?

lazyframe('.lazyframe', {
apikey: '',
onLoad: function (lazyframe) {
console.log(lazyframe.settings.thumbnail); //undefined
setTimeout(function(){
console.log(lazyframe.settings.thumbnail); // good
}, 1000);
}
});

Chrome autoplay

Hey,
I love this little script, so simple but great for my visitors.

I've noticed that my videos are no longer autoplaying when clicked.
I think this is only in Chrome and related to their new autoplay policies.

are you able to add
allow='autoplay' to the iframe?
IE:

<iframe src="https://www.youtube.com/embed/A-t5B4pZdYc/?autoplay=1" frameborder="0" allowfullscreen="" allow='autoplay' ></iframe>

i think this can be done by adding:
n.setAttribute("allow","autoplay"),
directly after
n.setAttribute("allowfullscreen",""),

but I'm not sure if that has other impacts.
It did work for me in my limited testing though.

thanks!

Custom callbacks for loading title/thumbnail

It would be really handy to be able to load titles and thumbnails using a custom callback.

As far as I can see, the current set of callbacks we can provide can't change how this data is loaded. We have to crawl the DOM and set the data-title and data-thumbnail attributes manually.

Cheers!

bug: player requires double click to launch the video

Hello there.

We tried to use this plugin to lazyload a youtube video.

When we tried to use it, we noticed that it requires to do 2 clicks most of the time to launch the video. This is reproducible even with the provided demo. Screencast

However, we noticed that there https://github.com/paulirish/lite-youtube-embed is a plugin, where it works without a double click.

We debugged the current plugin and figured out that to solve the double click problem we need to do 2 things.

  1. Add prefetch logic, which is here https://github.com/paulirish/lite-youtube-embed/blob/master/src/lite-yt-embed.js#L87-L99 in that plugin.
  2. Add allow iframe attribute with at least 'autoplay' value, which is here https://github.com/paulirish/lite-youtube-embed/blob/master/src/lite-yt-embed.js#L111.

PR welcomed to solve this issue.

How to import directly in html?

I'm not able to manually import this script with <script type="module" src="">. Is it planned to add this feat in the near future?

Support for YouTube Shorts.

I'm trying to use lazyframe to lazy load a youtube short. I'm not sure if anyone has been able to get this to work or if this is a feature that would need to be added to the library.

Ajax question

Hi Victor,

Thank you for your code - i'm hoping I can ask you an integration question. I'm making an ajax call into a modal using the Featherlight modal which works fine.

After it's open I use a callback to re-initialize your lazyframe function and bind it to the .lazyframe' class. Everything works except it doesn't pull the thumbnail from the Youtube API, but i can play the video just fine.

The only difference with a stand-along page of the same content I have working with lazy frame, is the background image isn't populated with the image from the youtube API.

I thought i'd field a question if you have any thoughts. Thanks!

Add class when iframe is loaded

In my use of lazyframe, I found that I needed to make some css changes when the iframe loads. Mainly just want to ensure that my own background pops into place, the play button is hidden if an odd aspect-ratio video loads, etc.

In the unminified lazyframe.js, I did this, which seems to work:

        function n(e) {
            var t = this;
            if (e instanceof HTMLElement != !1 && !e.classList.contains("lazyframe--loaded")) {
                var n = {
                    el: e,
                    settings: i(e)
                };
                n.el.addEventListener("click", function() {
                    n.el.appendChild(n.iframe);
                    n.el.classList.add("lazyframe--frame-loaded");
                    var i = e.querySelectorAll("iframe");
                    n.settings.onAppend.call(t, i[0])
                }), d.lazyload ? l(n) : u(n, !!n.settings.thumbnail)
            }
        }

(if it's not clear, the single line change is n.el.classList.add("lazyframe--frame-loaded");
I guess two questions:

  • Is this correct-ish?
  • Could this, or a more appropriate option, be made a feature?

set YT image quality

Library get's hqdefault image throu YT API, any chance to set another quality like maxresdefault?

Thanks

Trouble with autoplay

I am sure this just user error but could you help debug why my autoplay isn't working.

https://codepen.io/cluke009/pen/mdjwLgW

On Chrome in Ubuntu only youtube plays after interacting. On Firefox on Ubuntu both autoplay fine.

I also checked Edge on Windows 11 and neither autoplay.

In all instances your demo page behaves as it should.

Its a pretty straight forward setup so I am not sure what I am doing wrong.

Background center

Could you add background-position: center to the CSS (.lazyframe) for the cover image? Otherwise the cover image doesn't look great in some situations, like with the following code:

<div class="lazyframe" data-src="https://www.youtube.com/embed/3n35a28WL4s" data-vendor="youtube"></div>

Results in this:

image

With background-position: center applied, it looks correct as below:
image

Matt

YouTube player parameters

Hello,

Any way to set these up (to disable related videos at the end, hide controls etc)? Adding those at the end of data source url does not seem to work.

Disable Youtube Videos

?rel=0 gets ignored when added to the end of the youtube video url. It should disable related videos

uncaught type error

hello,
I'm getting this error and neither the thumbnail nor the video information are loaded

Uncaught TypeError: Cannot read property 'split' of undefined
at o (lazyframe.min.js:1)
at i (lazyframe.min.js:1)
at n (lazyframe.min.js:1)
at t (lazyframe.min.js:1)
at testconfiltros.php:987

Problem with slick carousel initialization

Hello,

I have a small problem with lazy loader and slick carousel initialization. I want to put 0..N videos inside slick like this
image

Problem is obvious. SOmetimes happen that some semi-visible parts are "red backgrounded". Is there any way how to get rid of that and load thumbnails for semi-visible videos? (Or load and paste thumbnails for all lazyloaded videos?)

Thank you for your time.

2.3.1 and 2.2.7 versions isn't working after update

Previously I was on 2.2.4.

What I see in node_modules now:
image
No src folder, no scss files.

My js code:

$(document).ready(function() {
    //YouTube lazy load
    let elements = $('.lazyframe');
    lazyframe(elements, {
        apikey: 'my-api-key,
    });

});

No changes in code except loading css with @import '~lazyframe/dist/lazyframe.css';, but now all YT videos isn't working:
image

Videos don't play, clicks don't work.

What is it?

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.