vue-responsive-video-background-player
Version:
Play your own videos in background responsively
329 lines (328 loc) • 9.38 kB
JavaScript
(function(){"use strict";try{if(typeof document!="undefined"){var e=document.createElement("style");e.appendChild(document.createTextNode(".video-wrapper[data-v-c3f4996a]{display:flex;justify-content:center;align-items:center;width:100%;height:100%;position:absolute;overflow:hidden;z-index:0}.fade[data-v-c3f4996a]{backface-visibility:hidden}.fade-enter-active[data-v-c3f4996a],.fade-leave-active[data-v-c3f4996a]{transition:opacity 1s}.fade-enter[data-v-c3f4996a],.fade-leave-to[data-v-c3f4996a]{opacity:0}video[data-v-c3f4996a]{visibility:visible;pointer-events:none;position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);height:100%;width:100%}.video-buffering[data-v-692e9d3d]{width:100%;overflow:hidden;height:100%;top:0;left:0;position:absolute}.video-overlay[data-v-95abe06c]{height:100%;width:100%;top:0;left:0;position:absolute}.vue-responsive-videobg[data-v-30aa7649]{background:none;position:relative;width:100%;overflow:hidden}.vue-responsive-videobg .videobg-content[data-v-30aa7649]{position:absolute;top:0;left:0;width:100%;height:100%}")),document.head.appendChild(e)}}catch(t){console.error("vite-plugin-css-injected-by-js",t)}})();
import { openBlock as s, createBlock as p, Transition as k, withCtx as w, withDirectives as P, createElementVNode as a, normalizeStyle as c, vShow as S, createElementBlock as y, resolveComponent as u, createCommentVNode as h, createVNode as j, renderSlot as V } from "vue";
const m = {
src: {
type: String,
required: !0
},
muted: {
type: Boolean,
default: !0
},
loop: {
type: Boolean,
default: !0
},
preload: {
type: String,
default: "auto"
},
objectFit: {
type: String,
default: "cover"
},
posterBgSize: {
type: String,
default: "cover"
},
objectPosition: {
type: String,
default: "center"
},
playsWhen: {
type: String,
default: "canplay",
note: "Google HTML Video Events"
},
playbackRate: {
type: Number,
default: 1
},
transition: {
type: String,
default: "fade"
}
}, E = ["src", "res", "autoplay"], R = (e) => Array.isArray(e) ? e.length === 0 ? !0 : _(e, E) : !1, _ = (e, t) => t.length === 1 ? v(e, t[0]) : v(e, t[0]) * _(e, t.slice(1)), v = (e, t) => {
for (let o = e.length - 1; o > -1; o -= 1) {
const r = e[o];
if (!W(r))
return !1;
if (z(r, t))
return !0;
}
return !1;
}, W = (e) => e != null && e.constructor.name === "Object", z = (e, t) => Object.prototype.hasOwnProperty.call(e, t), B = {
sources: {
type: Array,
default() {
return [];
},
validator: R
},
autoplay: {
type: Boolean,
default: !0
},
poster: {
type: String,
default: ""
},
overlay: {
type: String,
default: ""
},
...m
};
const l = (e, t) => {
const o = e.__vccOpts || e;
for (const [r, n] of t)
o[r] = n;
return o;
}, O = {
props: m,
emits: ["playing", "paused", "error", "loading", "ended", "ready"],
data() {
return {
showVideo: !1
};
},
computed: {
styleObject() {
return !this.objectFit && !this.objectPosition ? {} : {
objectFit: this.objectFit,
objectPosition: this.objectPosition
};
}
},
watch: {
src() {
this.load();
}
},
methods: {
pause() {
this.$refs.video && (this.$refs.video.pause(), this.$emit("paused"));
},
load() {
this.hide(), setTimeout(() => {
this.$refs.video.load(), this.$emit("loading");
}, 1e3);
},
play() {
this.setPlaybackRate(), this.$refs.video.play(), this.show(), this.$emit("playing");
},
show() {
this.showVideo = !0;
},
hide() {
this.showVideo = !1;
},
getMediaType(e) {
return `video/${e.split(".").pop().split(/[?#]/)[0]}`;
},
videoCanPlay() {
return !!this.$refs.video.canPlayType;
},
videoReady() {
this.pause(), this.$emit("ready");
},
videoError() {
this.$emit("error");
},
videoEnded() {
this.$emit("ended");
},
setPlaybackRate() {
this.$refs.video.playbackRate = this.playbackRate, this.$refs.video.defaultPlaybackRate = this.playbackRate;
}
},
mounted() {
this.videoCanPlay() && (this.$refs.video[`on${this.playsWhen}`] = this.videoReady, this.$refs.video.onerror = this.videoError, this.$refs.video.onended = this.videoEnded);
}
}, T = { class: "video-wrapper" }, C = ["muted", "loop", "preload"], F = ["src", "type"];
function I(e, t, o, r, n, i) {
return s(), p(k, { name: e.transition }, {
default: w(() => [
P(a("div", T, [
a("video", {
ref: "video",
autoplay: "",
playsinline: "",
muted: e.muted,
loop: e.loop,
preload: e.preload,
style: c(i.styleObject)
}, [
a("source", {
src: e.src,
type: i.getMediaType(e.src)
}, null, 8, F)
], 12, C)
], 512), [
[S, n.showVideo]
])
]),
_: 1
}, 8, ["name"]);
}
const M = /* @__PURE__ */ l(O, [["render", I], ["__scopeId", "data-v-c3f4996a"]]);
const L = {
props: {
poster: {
type: String,
required: !0
},
backgroundSize: {
type: String,
default: "cover"
},
backgroundPosition: {
type: String,
default: "center"
}
},
computed: {
image() {
return {
backgroundImage: `url(${this.poster})`,
backgroundSize: this.backgroundSize,
backgroundPosition: this.backgroundPosition
};
}
}
};
function q(e, t, o, r, n, i) {
return s(), y("div", {
class: "video-buffering",
style: c(i.image)
}, null, 4);
}
const A = /* @__PURE__ */ l(L, [["render", q], ["__scopeId", "data-v-692e9d3d"]]);
const H = {
props: {
overlay: {
type: String,
required: !0
}
}
};
function N(e, t, o, r, n, i) {
return s(), y("div", {
class: "video-overlay",
style: c({ background: o.overlay })
}, null, 4);
}
const D = /* @__PURE__ */ l(H, [["render", N], ["__scopeId", "data-v-95abe06c"]]), f = (e, t) => {
let o = "null";
return (...r) => {
o === "null" && (o = setTimeout(() => {
e(...r), o = "null";
}, t));
};
}, G = {
data() {
return {
width: 0
};
},
computed: {
current() {
if (this.sources.length === 0)
return this.default;
const e = this.sources.sort((t, o) => t.res - o.res).filter((t) => t.res >= this.width);
return e.length === 0 ? this.default : e[0];
},
default() {
return {
src: this.src,
poster: this.poster,
autoplay: this.autoplay
};
}
},
methods: {
_change_video_resolution() {
this.width = this.$_innerWidth();
},
$_innerWidth() {
return window.innerWidth && document.documentElement.clientWidth ? Math.min(window.innerWidth, document.documentElement.clientWidth) : window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName("body")[0].clientWidth;
}
},
beforeMount() {
this._change_video_resolution();
},
mounted() {
window.addEventListener("resize", f(this._change_video_resolution, 250));
},
beforeUnmount() {
window.removeEventListener("resize", f(this._change_video_resolution, 250));
}
};
const U = {
props: B,
mixins: [G],
emits: ["playing", "paused", "error", "loading", "ended", "ready"],
components: {
VideoPlayer: M,
VideoPoster: A,
VideoOverlay: D
},
computed: {
player() {
return this.$refs.player;
}
},
methods: {
playVideo() {
this.$emit("ready"), this.current.autoplay && this.player.play();
}
}
}, J = {
class: "vue-responsive-videobg",
ref: "vidbg"
}, K = { class: "videobg-content" };
function Q(e, t, o, r, n, i) {
const g = u("video-poster"), $ = u("video-player"), b = u("video-overlay");
return s(), y("section", J, [
e.current.poster || e.poster ? (s(), p(g, {
key: 0,
poster: e.current.poster || e.poster,
"background-size": e.posterBgSize,
"background-position": e.objectPosition
}, null, 8, ["poster", "background-size", "background-position"])) : h("", !0),
j($, {
ref: "player",
src: e.current.src,
muted: e.muted,
loop: e.loop,
preload: e.preload,
"plays-when": e.playsWhen,
"playback-rate": e.playbackRate,
transition: e.transition,
"object-fit": e.objectFit,
"object-position": e.objectPosition,
onReady: i.playVideo,
onPlaying: t[0] || (t[0] = (d) => e.$emit("playing")),
onPaused: t[1] || (t[1] = (d) => e.$emit("paused")),
onError: t[2] || (t[2] = (d) => e.$emit("error")),
onLoading: t[3] || (t[3] = (d) => e.$emit("loading")),
onEnded: t[4] || (t[4] = (d) => e.$emit("ended"))
}, null, 8, ["src", "muted", "loop", "preload", "plays-when", "playback-rate", "transition", "object-fit", "object-position", "onReady"]),
e.overlay ? (s(), p(b, {
key: 1,
overlay: e.overlay
}, null, 8, ["overlay"])) : h("", !0),
a("div", K, [
V(e.$slots, "default", {}, void 0, !0)
])
], 512);
}
const X = /* @__PURE__ */ l(U, [["render", Q], ["__scopeId", "data-v-30aa7649"]]), Z = {
install(e) {
e.component("VideoBackground", X);
}
};
export {
Z as Plugin,
X as default
};