UNPKG

@adrianarroyoc/mediaplayer

Version:

Proyecto del Curso Profesional de JavaScript en Platzi

40 lines (39 loc) 1.33 kB
"use strict"; exports.__esModule = true; var AutoPause = /** @class */ (function () { function AutoPause() { this.threshold = .25; this.handlerIntersection = this.handlerIntersection.bind(this); this.handlerVisibilityChange = this.handlerVisibilityChange.bind(this); } AutoPause.prototype.run = function (player) { this.player = player; var observer = new IntersectionObserver(this.handlerIntersection, { threshold: this.threshold }); observer.observe(this.player.media); document.addEventListener("visibilitychange", this.handlerVisibilityChange); }; AutoPause.prototype.handlerIntersection = function (entries) { var entry = entries[0]; //console.log(entry); var isVisible = entry.intersectionRatio >= this.threshold; if (isVisible) { this.player.play(); } else { this.player.pause(); } }; AutoPause.prototype.handlerVisibilityChange = function () { var isVisible = document.visibilityState === "visible"; if (isVisible) { this.player.play(); } else { this.player.pause(); } }; return AutoPause; }()); exports["default"] = AutoPause;