Go to homepage

Projects /

Video Feature

A CTA box over a background video.

View Demo

How to

The Video Feature component is a content box over a background video.

The height of the video depends on the padding-top utility class applied to the .video-feature element. The width of the .video-feature__box is set using the grid column utility classes.

Video background #

If you want to play the background video only when it enters the viewport, and pause when it leaves it, you can include the following JS snippet in your project:

(function() {
  var VideoAutoplay = function(element) {
    this.element = element;
    this.intersectionObserverSupported = ('IntersectionObserver' in window && 'IntersectionObserverEntry' in window && 'intersectionRatio' in window.IntersectionObserverEntry.prototype);
    initVideoAutoplay(this);
  };

  function initVideoAutoplay(video) {
    // use the Intersection Observer API to detect when video enters/leaves the viewport
    video.element.autoplay = video.intersectionObserverSupported ? false : true;
    if(!video.intersectionObserverSupported) return;
    var observer = new IntersectionObserver(videoAutoplayCallback.bind(video));
		observer.observe(video.element);
  };

  function videoAutoplayCallback(entries, observer) {
    entries[0].isIntersecting ? this.element.play() : this.element.pause();
  };

  window.VideoAutoplay = VideoAutoplay;

  // init the VideoAutoplay object
  var videoAutoplay = document.getElementsByClassName('js-video-autoplay');
  if( videoAutoplay.length > 0) {
		for(var i = 0; i < videoAutoplay.length; i++) {
      new VideoAutoplay(videoAutoplay[i]);
		}
	}
}());

Make sure to add the .js-video-autoplay class to all the video elements you want to target.


Video by Evgenia Kirpichnikova from Pexels

Categories

Bug report & feedback

Component Github page ↗

Project duplicated

Project created

Globals imported

There was an error while trying to export your project. Please try again or contact us.