Embed a YouTube Player Easily and Lazy Load the Video with VUE

Vue Lazytube

Embed a YouTube player easily and lazy load the video to save resources and reduces initial load size. Useful when performance and page size is important or for sites with many embedded youtube videos.

For a simple example page with 10 videos, vue-lazytube will reduce loadtime by 7x and memory usage by 2-3x.

Features

  • reduces initial load size by ~1.1 MB per video
  • fully responsive and customizable through props
  • provides methods to control (playstoppause and reset) embedded videos
  • provide options to set up custom title and preview of embedded videos

Installation

With a build systems

$ npm install --save vue-lazytube
$ yarn add vue-lazytube

To make the plugin available globally

In your main.js:

import LazyYoutube from "vue-lazytube";

Vue.component("LazyYoutube", LazyYoutube);

OR

To include only in specific components

import LazyYoutube from 'vue-lazytube'

export default {
    name: 'YourComponent',
    components: {
        LazyYoutube,
    },
}

OR

Directly in browser

<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<script src="./vue-lazytube.umd.min.js"></script>

<script>
    // as a component
    Vue.use('LazyYoutube', LazyYoutube)
</script>

HTML

Usage

<template>
    <LazyYoutube src="https://www.youtube.com/watch?v=TcMBFSGVi1c" />
</template>

API

Props

This is image title

Methods

These methods let you control the player using JavaScript, so you can perform the operations like playstoppause and reset.

The user’s browser must support the HTML5 postMessage feature. Most modern browsers support postMessage.

This is image title

Example
<template>
    <LazyYoutube
        ref="lazyVideo"
        src="https://www.youtube.com/watch?v=TcMBFSGVi1c"
    />

    <button @click="handleClick('playVideo')">Play</button>
    <button @click="handleClick('stopVideo')">Stop</button>
    <button @click="handleClick('pauseVideo')">Pause</button>
    <button @click="handleClick('resetView')">Reset</button>
</template>

<script>
    export default {
      name: "YourComponent",
      components: {},
      methods: {
        handleClick(event) {
          this.$refs["lazyVideo"][event]();
        },
      },
    };
</script>

Download Details:

Author: seeratawan01

Live Demo: https://codesandbox.io/s/vue-lazytube-hj03y

GitHub: https://github.com/seeratawan01/vue-lazytube

#vue

Embed a YouTube Player Easily and Lazy Load the Video with VUE
12.45 GEEK