Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!--
The complete explanation of the script can be found on Codepalm (currently only in German and out-to-date): 
https://www.codepalm.de/post/youtube-2-klick-loesung/
-->

<div id="main-wrapper">
	
  <div class="contrast-bg">
    <h1>iFrame 2-Click-Solution</h1>
    <h4>Let your users consent before the iFrame is displayed</h4>
    <p>With the 2-click solution, you can make your users aware of the privacy policy before they view a YouTube video or Google Maps.</p>
    <a href="#notes">Go to the Notes ►</a>
  </div>
	
	<div class="container">
		
		<div class="contrast-bg">
      <div>
        
        <!-- Copy this... -->
        <div class="video_wrapper" style="background-image: url( 'https://images.unsplash.com/photo-1483959651481-dc75b89291f1?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ' );">
          <div class="video_trigger" data-source="aqz-KE-bpKQ" data-type="youtube">
            <p class="text-center">By viewing the video you agree that your data will be transferred to YouTube and that you have read the <a href="#privacy-policy" target="_blank">privacy policy</a>.</p>
            <input type="button" class="btn" value="I ♥ 2 Consent" />
          </div>
          <div class="video_layer"><iframe src="" border="0" data-scaling="true" data-format="16:9"></iframe></div>
        </div>
        <!-- ...until here -->
        
      </div>

    </div>
      
		<div class="contrast-bg">
      
      <div>
        
        <!-- Copy this... -->
        <div class="video_wrapper" style="background-image: url( 'https://images.unsplash.com/photo-1483959651481-dc75b89291f1?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ' );">
          <div class="video_trigger" data-source="!1m18!1m12!1m3!1d2662.580472207323!2d11.577736615630995!3d48.13761335913764!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x479e758bcf52fcc5%3A0xa9c170edcdbd1a2f!2zSG9mYnLDpHVoYXVzIE3DvG5jaGVu!5e0!3m2!1sde!2sde!4v1605619887174!5m2!1sde!2sde" data-type="google-maps">
            <p class="text-center">By calling up the map you agree that your data will be transmitted to Google Maps and that you have read the <a href="#datenschutz" target="_blank">privacy policy</a>.</p>
            <input type="button" class="btn" value="I ♥ 2 Consent" />
          </div>
          <div class="video_layer"><iframe src="" border="0" data-scaling="true" data-format="16:9"></iframe></div>
        </div>
        <!-- ...until here -->
        
      </div>
			
		</div>
		
	</div>
	
  <div class="contrast-bg text-left" id="notes" style="margin-top: 60px;">
    
    <div class="container">
      <h3>Please note</h3>
      <ul>
        <li>This script is programmed only using vanilla JavaScript. You don't need to include libraries like jQuery to use it.</li>
        <li>I've used SCSS for styling. You can show the parsed CSS by clicking the arrow and select "View Compiled CSS", if you don't use SCSS in your project.</li>
        <li>The banner was developed for the German data protection. it is very likely that the data protection in your country will accept this. But please clarify this with your data protection officer.</li>
      </ul>

      <div class="container">
        <h3>Upcomming...</h3>
        <p>Currently, consent cannot be secured. I will soon add cookies to secure consent for all similar 2-click banners.</p>
      </div>
    
  </div>
	
</div>
              
            
!

CSS

              
                /* Video Wrapper */
.video_wrapper {
  
	background-image: url( 'bg_image.jpg' );
	background-size: cover;
	position: relative;
	box-shadow: 0px 3px 5px #333;
	height: 400px;
	width: 100%;
  
  .video_trigger {
    
    padding: 130px 30px;

    height: -webkit-calc( 100% - 260px );
    height: -moz-calc( 100% - 260px );
    height: -o-calc( 100% - 260px );
    height: calc( 100% - 260px );

    width: -webkit-calc( 100% - 60px );
    width: -moz-calc( 100% - 60px );
    width: -o-calc( 100% - 60px );
    width: calc( 100% - 60px );

    position: absolute;
    bottom: 0;
    z-index: 9;
    background-color: rgba( 0, 0, 0, .8 );
    color: #fff;

    &:before {
      content: '\f04b';
      font-family: 'FontAwesome';
      position: absolute;
      z-index: -1;
      font-size: 120px;
      color: rgba( 255, 255, 255, .2 );
      width: 120px;
      height: 120px;
      line-height: 120px;
      text-align: center;
      top: -webkit-calc( 50% - 60px );
      top: calc( 50% - 60px );
      left: -webkit-calc( 50% - 60px );
      left: calc( 50% - 60px );
    }
    
  }
  
  .video_layer {
    
    position: relative;
    display: none;
    height: inherit;
    width: inherit;
    margin: auto;
    
    iframe {
      border: 0px none transparent;
      height: inherit;
      width: inherit;
      vertical-align: middle;
    }
    
  }
  
}


/* Button */
.btn {
  
  cursor: pointer;
  display: inline-block;
  transition: color .5s, background-color .5s, border .5s;
  padding: 8px 16px;
  font-size: 16px;
  border: 1px solid #ffffff;
  
  &:hover {
    background-color: transparent;
    color: #ffffff;
  }
  
}





/* Some CSS to prettyfy the screen - You shouldn't copy this */
body {

	text-align: center;
	font-family: Arial, sans-serif;
	margin: 0;
	min-height: 100vh;

	position: relative;
	z-index: 1;
	overflow: hidden;
	overflow-y: auto;

	background: linear-gradient(0deg, rgba(213,255,74,1) 0%, rgba(0,212,255,1) 100%);
	background-repeat: no-repeat;

	&:after {

		content: " ";
		display: block;
		
		width: 100%;
		height: 100%;
		
		position: absolute;
		left: 0;
		top: 0;
		
		z-index: -1;
		opacity: .8;
		
		background-image: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" width="28" height="49" viewBox="0 0 28 49"%3E%3Cg fill-rule="evenodd"%3E%3Cg id="hexagons" fill="%239C92AC" fill-opacity="0.4" fill-rule="nonzero"%3E%3Cpath d="M13.99 9.25l13 7.5v15l-13 7.5L1 31.75v-15l12.99-7.5zM3 17.9v12.7l10.99 6.34 11-6.35V17.9l-11-6.34L3 17.9zM0 15l12.98-7.5V0h-2v6.35L0 12.69v2.3zm0 18.5L12.98 41v8h-2v-6.85L0 35.81v-2.3zM15 0v7.5L27.99 15H28v-2.31h-.01L17 6.35V0h-2zm0 49v-8l12.99-7.5H28v2.31h-.01L17 42.15V49h-2z"/%3E%3C/g%3E%3C/g%3E%3C/svg%3E');

	}
	
	a {
		color: #0078cc;
		text-decoration: none;
	}
	
	#main-wrapper {
		
		padding: 40px 0;
		
	}
	
	.contrast-bg {
		
		padding: 15px;
		background-color: rgba( 255, 255, 255, .6 );
		
		&:first-child {
			
			margin-bottom: 60px;
			
		}
		&:last-child {
			
			margin-top: 60px;
			
		}
		
		p {
			
			margin-top: 0;
			
		}
		
	}
	
	.container {
		
		width: 100%;
		max-width: 1200px;
		margin: 0 auto;
    
    & > * {
      width: 100%;
    }
    
	}

  .text-left {

    text-align: left !important;

  }

}
@media ( max-width: 767px ) {
	
	body .container {
		
		display: block;
		
	}
	body .container .left,
	body .container .right {
		
		margin: 15px;
		width: calc( 100% - 60px );
		
	}
	
}
              
            
!

JS

              
                /* Document Ready Script */
document.ready = function( callback ) {

	if( document.readyState != 'loading' ) {

		callback();

	}
	else {

		document.addEventListener( 'DOMContentLoaded', callback );

	}

};

/* Automattically resize the iFrame */
var iFrame2C = {};
iFrame2C.rescale = function( iframe, format ) {

	let formatWidth = parseInt( format.split(':')[0] );
	let formatHeight = parseInt( format.split(':')[1] );
	let formatRatio = formatHeight / formatWidth;
	var iframeBounds = iframe.getBoundingClientRect();

	let currentWidth = iframeBounds.width;
	let newHeight = formatRatio * currentWidth;

	iframe.style.height = Math.round( newHeight ) + "px";

};

/* Resize iFrame */
function iframeResize() {

	var iframes = document.querySelectorAll( 'iframe[data-scaling="true"]' );
	if( !!iframes.length ) {

		for( var i=0; i < iframes.length; i++ ) {

			let iframe = iframes[ i ];
			let videoFormat = '16:9';

			let is_data_format_existing = typeof iframe.getAttribute( 'data-format' ) !== "undefined";
			if( is_data_format_existing ) {

				let is_data_format_valid = iframe.getAttribute( 'data-format' ).includes( ':' );
				if( is_data_format_valid ) {

					videoFormat = iframe.getAttribute( 'data-format' );

				}

			}

			iFrame2C.rescale( iframe, videoFormat );

		}

	}

}

/* Event Listener on Resize for iFrame-Resizing */
document.ready( function() {

	window.addEventListener( "resize", function() {

		iframeResize();

	});

	iframeResize();

});

/* Source-URLs */
/*
  data_type will be the value of the attribute "data-type" on element "video_trigger"
  data_souce will be the value of the attribute "data-source" on element "video_trigger", which will be replaced on "{SOURCE}"
*/
function get_source_url( data_type ) {

	switch( data_type ) {

		case "youtube":
			return "https://www.youtube-nocookie.com/embed/{SOURCE}?rel=0&controls=0&showinfo=0&autoplay=1&mute=1";

		case "google-maps":
			return "https://www.google.com/maps/embed?pb={SOURCE}";

			/* Add your own sources here */
			/* Always set "{SOURCE}" as placeholder for individual data from attribute "data-source" */

		default: break;

	}

}

/* 2-Click Solution */
document.ready( function() {

	var video_wrapper = document.querySelectorAll( '.video_wrapper' );

	if( !!video_wrapper.length ) {
		for( var i=0; i < video_wrapper.length; i++ ) {

			let _wrapper = video_wrapper[ i ];
			var video_triggers = _wrapper.querySelectorAll( '.video_trigger' );

			if( !!video_triggers.length ) {
				for( var l=0; l < video_triggers.length; l++ ) {

					var video_trigger = video_triggers[ l ];
					var accept_buttons = video_trigger.querySelectorAll( 'input[type="button"]' );

					if( !!accept_buttons.length ) {
						for( var j=0; j < accept_buttons.length; j++ ) {

							var accept_button = accept_buttons[ j ];
							accept_button.addEventListener( "click", function() {

								var _trigger = this.parentElement;
								var data_type = _trigger.getAttribute( "data-type" );
								var source = "";
								_trigger.style.display = "none";

								source = get_source_url( data_type );

								var data_source = _trigger.getAttribute( 'data-source' );
								source = source.replace( "{SOURCE}", data_source );

								var video_layers = _trigger.parentElement.querySelectorAll( ".video_layer" );
								if( !!video_layers.length ) {
									for( var k=0; k < video_layers.length; k++ ) {

										var video_layer = video_layers[ k ];
										video_layer.style.display = "block";
										video_layer.querySelector( "iframe" ).setAttribute( "src", source );

									}
								}

								_wrapper.style.backgroundImage = "";
								_wrapper.style.height = "auto";

								var timeout = 100; // ms
								setTimeout( function() {

									iframeResize();

								}, timeout );

							});

						}
					}

				}
			}

		};
	}
});
              
            
!
999px

Console