Skip to content

Commit

Permalink
Refactored the Javascript, added new video
Browse files Browse the repository at this point in the history
  • Loading branch information
sebekstrom committed Jul 10, 2015
1 parent 7a68105 commit 559bf57
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 174 deletions.
40 changes: 19 additions & 21 deletions css/style.css
Expand Up @@ -24,12 +24,12 @@ video {
width: 100%;
}

.teaser-video {
.header-video__teaser-video {
width: 100%;
height: auto;
}

.header-video--media {
.header-video__media {
width: 100%;
height: auto;
}
Expand Down Expand Up @@ -72,59 +72,57 @@ h2 {
margin-top: 1em;
}

.video-trigger {
.header-video__play-trigger {
z-index: 5;
position: absolute;
background: #222;
background: rgba(0,0,0,.3);
text-align: center;
color: white;
text-decoration: none;
width: 200px;
height: 60px;
top: 0;
padding: 1.5em 3em;
z-index: 5;
left: 50%;
line-height: 60px;
margin-left: -100px;
top: 50%;
margin-top: -30px;
-webkit-transition: background .3s;
-moz-transition: background .3s;
transition: background .3s;
border-radius: 10px;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
-webkit-transition: background .5s;
-moz-transition: background .5s;
transition: background .5s;
}

.video-trigger:hover {
background: #363636;
.header-video__play-trigger:hover {
background: rgba(0,0,0,.5);
}

/**
Just some basic styling for the closing trigger
**/

.video-close-trigger {
.header-video__close-trigger {
z-index: 99;
position: absolute;
background: #A61212;
text-align: center;
color: white;
text-decoration: none;
width: 200px;
height: 60px;
padding: 2em 3em;
border-radius: 10px;
right: 30px;
line-height: 60px;
top: 30px;
-webkit-transition: background .3s;
-moz-transition: background .3s;
transition: background .3s;
border: none;
}
.video-close-trigger:hover {
.header-video__close-trigger:hover {
background: #363636;
cursor: pointer;
}

.container {
margin: 0 auto;
max-width: 1200px;
padding: 2em;
padding: 10em 2em 10em 2em;
}
Binary file modified img/masthead.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 20 additions & 24 deletions index.html
@@ -1,30 +1,25 @@
<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Z63 - Header video</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="shortcut icon" type="image/x-icon" href="img/favicon.ico">
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/style.css?v1.1">
</head>
<body>

<div class="header-video">
<img src="img/masthead.jpg"
class="header-video--media"
data-video-src="0CxWLQxvY9g"
data-teaser-source="video/masthead-teaser"
data-provider="Youtube"
data-video-width="500"
data-video-height="281">
<a href="http://vimeo.com/87701971" class="video-trigger" id="video-trigger">Play video</a>
<button type="button" class="video-close-trigger" id="video-close-trigger">Close video</button>
class="header-video__media"
data-video-URL="https://www.youtube.com/embed/Scxs7L0vhZ4"
data-teaser="video/teaser-video"
data-video-width="560"
data-video-height="315">
<a href="https://www.youtube.com/embed/Scxs7L0vhZ4" class="header-video__play-trigger" id="header-video__play-trigger">Play video</a>
<button type="button" class="header-video__close-trigger" id="header-video__close-trigger">Close video</button>
</div>

<div class="container">
Expand All @@ -50,17 +45,18 @@ <h2>Dolor sit amet</h2>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/modernizr.js"></script>
<script src="js/script.js"></script>
<script src="js/script.js?v1.1"></script>
<script>
$(document).ready(function() {

HeaderVideo.init({
container: $('.header-video'),
header: $('.header-video--media'),
videoTrigger: $("#video-trigger"),
autoPlayVideo: false
});

$('.header-video').each(function(i, elem) {
headerVideo = new HeaderVideo({
element: elem,
media: '.header-video__media',
playTrigger: '.header-video__play-trigger',
closeTrigger: '.header-video__close-trigger',
autoPlayVideo: false
});
});
});
</script>
</body>
Expand Down
211 changes: 82 additions & 129 deletions js/script.js
@@ -1,136 +1,89 @@
var HeaderVideo = (function ($, document) {

var settings = {
container: $('.header-video'),
video: '#video',
header: $('.header-video--media'),
videoTrigger: $('#video-trigger'),
videoCloseTrigger: $('#video-close-trigger'),
teaserVideo: $('#teaser-video'),
autoPlayVideo: false
};

var init = function(options){
settings = $.extend(settings, options);
getVideoDetails();
setFluidContainer();
bindClickActions();
settings.videoCloseTrigger.hide();

if(videoDetails.teaser) {
appendTeaserVideo();
}

if(settings.autoPlayVideo) {
appendFrame();
}
};

var bindClickActions = function() {
settings.videoTrigger.on('click', function(e) {
e.preventDefault();
appendFrame();
settings.videoCloseTrigger.fadeIn();
});
settings.videoCloseTrigger.on('click', function(e){
e.preventDefault();
removeFrame();
});
};

var getVideoDetails = function() {
//Get all the data attributes from the HTML container and return them as an object for easy data retrieval
videoDetails = {
id: settings.header.attr('data-video-src'),
teaser: settings.header.attr('data-teaser-source'),
provider: settings.header.attr('data-provider').toLowerCase(),
videoHeight: settings.header.attr('data-video-height'),
videoWidth: settings.header.attr('data-video-width')
};
return videoDetails;
};

var setFluidContainer = function () {
settings.container.data('aspectRatio', videoDetails.videoHeight / videoDetails.videoWidth);

$(window).resize(function() {
var winWidth = $(window).width(),
winHeight = $(window).height();

settings.container
.width(Math.ceil(winWidth)) //Round up to the nearest pixel value to prevent breaking of layout
.height(Math.ceil(winWidth * settings.container.data('aspectRatio'))); //Set the videos aspect ratio, see https://css-tricks.com/fluid-width-youtube-videos/

if(winHeight < settings.container.height()) {
settings.container
.width(Math.ceil(winWidth))
.height(Math.ceil(winHeight));
}

}).trigger('resize'); //Trigger resize to force it to run on page load as well

};

var appendTeaserVideo = function() {
if(Modernizr.video && !isMobile()) {
var source = videoDetails.teaser,
html = '<video autoplay="true" loop="loop" muted id="teaser-video" class="teaser-video"><source src="'+source+'.mp4" type="video/mp4"><source src="'+source+'.ogv" type="video/ogg"></video>';
settings.container.append(html);
}
};

var createFrame = function() {
//Added an ID attribute to be able to remove the video element when the user clicks on the remove button
if(videoDetails.provider === 'youtube') {
var html = '<iframe id="video" src="http://www.youtube.com/embed/'+videoDetails.id+'?rel=0&amp;hd=1&autohide=1&showinfo=0&autoplay=1&enablejsapi=1&origin=*" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>';
}
else if(videoDetails.provider === 'vimeo') {
var html = '<iframe id="video" src="http://player.vimeo.com/video/'+videoDetails.id+'?title=0&amp;byline=0&amp;portrait=0&amp;color=3d96d2&autoplay=1" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>';
}
else if(videoDetails.provider === 'html5') {
var html = '<video autoplay="true" loop="loop" id="video"><source src="'+videoDetails.id+'.mp4" type="video/mp4"><source src="'+videoDetails.id+'.ogv" type="video/ogg"></video>';
}
return html;
};

var appendFrame = function() {
settings.header.hide();
settings.container.append(createFrame());
removePlayButton();
settings.teaserVideo.hide();
};

var removeFrame = function() {
$(settings.video).remove();
settings.teaserVideo.fadeIn();
displayPlayButton();
removeRemoveButton();
};
/*global Modernizr, $ */

// /**
// * Reponsive and gracefully degrading header video
// More info on http://zerosixthree.se/create-a-responsive-header-video-with-graceful-degradation/
// * -----------------------------------------------------------------------------
// */
'use strict';

var HeaderVideo = function(settings) {
this.$element = $(settings.element);
this.settings = settings;
this.videoDetails = this.getVideoDetails();
this.init();
};

HeaderVideo.prototype.init = function() {
if (this.settings.element.length === 0) {
return;
}

var removePlayButton = function () {
if(settings.videoTrigger) {
settings.videoTrigger.fadeOut('slow');
}
};
$(this.settings.closeTrigger).hide();
this.setFluidContainer();
this.bindUIActions();

var displayPlayButton = function() {
if(settings.videoTrigger) {
settings.videoTrigger.fadeIn('slow');
if(this.videoDetails.teaser && Modernizr.video && !Modernizr.touch) {
this.appendTeaserVideo();
}
};

HeaderVideo.prototype.bindUIActions = function() {
var that = this;
$(this.settings.playTrigger).on('click', function(e) {
e.preventDefault();
that.appendIframe();
});
$(this.settings.closeTrigger).on('click', function(e){
e.preventDefault();
that.removeIframe();
});
};

HeaderVideo.prototype.appendIframe = function() {
var html = '<iframe id="header-video__video-element" src="'+this.videoDetails.videoURL+'?rel=0&amp;hd=1&autohide=1&showinfo=0&autoplay=1&enablejsapi=1&origin=*" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>';
$(this.settings.playTrigger).fadeOut();
$(this.settings.closeTrigger).fadeIn();
this.$element.append(html);
};

HeaderVideo.prototype.removeIframe = function() {
$(this.settings.playTrigger).fadeIn();
$(this.settings.closeTrigger).fadeOut();
this.$element.find('#header-video__video-element').remove();
};

HeaderVideo.prototype.appendTeaserVideo = function() {
var source = this.videoDetails.teaser,
html = '<video autoplay="true" loop="true" muted id="header-video__teaser-video" class="header-video__teaser-video"><source src="'+source+'.webm" type="video/mp4"><source src="'+source+'.mp4" type="video/mp4"></video>';
this.$element.append(html);
};

HeaderVideo.prototype.setFluidContainer = function() {
var element = this.$element;
element.data('aspectRatio', this.videoDetails.videoHeight / this.videoDetails.videoWidth);

$(window).resize(function() {
var windowWidth = $(window).width();
var windowHeight = $(window).height();

element.width(Math.ceil(windowWidth));
element.height(Math.ceil(windowWidth * element.data('aspectRatio'))); //Set the videos aspect ratio, see https://css-tricks.com/fluid-width-youtube-videos/

if(windowHeight < element.height()) {
element.width(Math.ceil(windowWidth));
element.height(Math.ceil(windowHeight));
}
};

var removeRemoveButton = function() {
settings.videoCloseTrigger.hide();
};
}).trigger('resize');
};

var isMobile = function () {
//A super basic way of detecting mobile devices. Should be extended to a more
//fool proof way in a production enviroment.
return Modernizr.touch;
}
HeaderVideo.prototype.getVideoDetails = function() {
var mediaElement = $(this.settings.media);

return {
init: init
videoURL: mediaElement.attr('data-video-URL'),
teaser: mediaElement.attr('data-teaser'),
videoHeight: mediaElement.attr('data-video-height'),
videoWidth: mediaElement.attr('data-video-width')
};

})(jQuery, document);
};
Binary file removed video/masthead-teaser.mp4
Binary file not shown.
Binary file removed video/masthead-teaser.ogv
Binary file not shown.
Binary file added video/teaser-video.mp4
Binary file not shown.
Binary file added video/teaser-video.webm
Binary file not shown.

0 comments on commit 559bf57

Please sign in to comment.