Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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

              
                <div class="fancy-overlay" id="content">
		<a href="https://www.youtube.com/watch?v=Yt_myLDSdsY" class="picture" alt="Video 1" data-options='{"autoPlay" : "true"}'>
			<img src="paisaje.jpg" alt="">
		</a>
		<a href="https://s-media-cache-ak0.pinimg.com/564x/65/33/85/6533859c383845b498d81990c4be2a41.jpg" class="picture" alt="Foto 2">
			<img src="https://s-media-cache-ak0.pinimg.com/564x/65/33/85/6533859c383845b498d81990c4be2a41.jpg" alt="">
		</a>
		<a href="https://s-media-cache-ak0.pinimg.com/564x/19/dc/3d/19dc3d3f392a57378c55c32e3ef7574e.jpg" class="picture" alt="Foto 3">
			<img src="https://s-media-cache-ak0.pinimg.com/564x/19/dc/3d/19dc3d3f392a57378c55c32e3ef7574e.jpg" alt="">
		</a>
		<a href="https://s-media-cache-ak0.pinimg.com/564x/5c/dd/72/5cdd72b452abf457e85ec9ea38aa6559.jpg" class="picture" alt="Foto 4">
			<img src="https://s-media-cache-ak0.pinimg.com/564x/5c/dd/72/5cdd72b452abf457e85ec9ea38aa6559.jpg" alt="">
		</a>
		<a href="https://s-media-cache-ak0.pinimg.com/564x/f8/e7/bf/f8e7bfc4e7523e1d050fd12e2141eb48.jpg" class="picture" alt="Foto 5">
			<img src="https://s-media-cache-ak0.pinimg.com/564x/f8/e7/bf/f8e7bfc4e7523e1d050fd12e2141eb48.jpg" alt="">
		</a>
	</div>
              
            
!

CSS

              
                body {
  background: #f2f2f2;
  padding: 0;
  margin: 0; }

.fancy-overlay {
  list-style: none;
  max-width: 700px;
  margin: auto;
  display: none; }
  .fancy-overlay .fancy-image {
    width: 160px;
    margin: 0 10px 10px 0;
    background-color: #ffffff;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
    border-radius: 4px;
    display: inline-block;
    vertical-align: top;
    /* Animaciones */
    opacity: 0;
    -webkit-transition: all 200ms ease-in-out;
    -moz-transition: all 200ms ease-in-out;
    -ms-transition: all 200ms ease-in-out;
    -o-transition: all 200ms ease-in-out;
    transition: all 200ms ease-in-out; }
    .fancy-overlay .fancy-image a {
      display: block;
      padding: 4px;
      text-decoration: none; }
      .fancy-overlay .fancy-image a h3 {
        margin: 0;
        color: #423F3F;
        font-family: Arial;
        padding: 4px 0 0; }
      .fancy-overlay .fancy-image a img {
        width: 100%; }

.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  -webkit-transition: all 300ms ease-in-out;
  -moz-transition: all 300ms ease-in-out;
  transition: all 300ms ease-in-out; }
  .overlay .overlay-content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    text-align: center;
    width: 80%;
    height: 80%; }
    @media only screen and (min-width: 320px) and (max-width: 640px) {
      .overlay .overlay-content {
        height: auto; } }
    .overlay .overlay-content img {
      width: 50% !important;
      background-color: #ffffff;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
      border-radius: 4px;
      padding: 5px; }
    .overlay .overlay-content iframe {
      width: 80%;
      background-color: #ffffff;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
      border-radius: 4px;
      padding: 5px;
      height: 100%; }
    .overlay .overlay-content h3 {
      color: #fff;
      font-family: Arial;
      margin: 0;
      padding: 4px 0 0; }
    .overlay .overlay-content .close {
      position: absolute;
      right: 0;
      top: -5%;
      color: #fff;
      cursor: pointer;
      display: block; }
    .overlay .overlay-content .prev {
      left: 0; }
    .overlay .overlay-content .next {
      right: 0; }
    .overlay .overlay-content .prev, .overlay .overlay-content .next {
      position: absolute;
      color: #fff;
      font-size: 30px;
      top: 50%;
      transform: translate(0%, -50%); }

.fancy-image--loaded, .fancy-overlay--loaded {
  opacity: 1 !important; }

/*# sourceMappingURL=style.css.map */

              
            
!

JS

              
                (function(){
	var images = [],
	fo = {
		init : function(){
			//detectar todas las instancias que tengan la clase .fo
			var wrapper = document.getElementById('content'),
			photos = document.querySelectorAll(".picture")

			for(var i = 0; i < photos.length; i++){
				
				var opts = photos[i].getAttribute('data-options'),
				imgAttrs = {
					id : i,
					src : photos[i].getAttribute('href'),
					title : photos[i].getAttribute('alt'),
					options : opts
				}

				console.log(imgAttrs)

				images.push(imgAttrs)

			}
			var content = '<ul style="margin: 0 -42px">'
			for(var i = 0; i < images.length; i++){

				//detectar si es imagen ó video
				//?autoplay=1
				//console.log(fo.getExtension(images[i].src).indexOf('.jpg') > -1)
				content += '<li class="fancy-image">'
				if(fo.getExtension(images[i].src)[0].indexOf('jpg') > -1 || fo.getExtension(images[i].src)[0].indexOf('png') > -1 || fo.getExtension(images[i].src)[0].indexOf('gif') > -1){
					content += '<a href="#image-'+i+'" onclick="fo.viewPhoto('+i+',\'photo\')"><img src="'+images[i].src+'" /><h3>'+images[i].title+'</h3></a>'
				}else{
					content += '<a href="#video-'+i+'" onclick="fo.viewPhoto('+i+',\'video\')"><img src="'+fo.getYTThumbnail(images[i].src,'big')+'" /><h3>'+images[i].title+'</h3></a>'
				}
				content += '</li>'
			}
			content += '</ul>'

			wrapper.innerHTML = content;
			wrapper.style.display = 'block'
			
		},
		getYTThumbnail : function(url,size){
			  if(url === null){ return ""; }

			  size = (size === null) ? "big" : size;
			  var vid;
			  var results;

			  results = url.match("[\\?&]v=([^&#]*)");

			  vid = ( results === null ) ? url : results[1];

			  if(size == "small"){
			    return "https://img.youtube.com/vi/"+vid+"/2.jpg";
			  }else {
			    return "https://img.youtube.com/vi/"+vid+"/0.jpg";
			  }
		},
		getExtension : function(file){
			return (/[.]/.exec(file)) ? /[^.]+$/.exec(file) : undefined;
		},
		transformYTURL : function(url){
			var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
		    var match = url.match(regExp);

		    if (match && match[2].length == 11) {
		        return match[2];
		    } else {
		        return 'error';
		    }
		},
		viewPhoto : function(id,type){

			var overlay = document.createElement('div'),
			overlayContent = document.createElement('div'),
			photo = document.createElement('img'),
			iframe = document.createElement('iframe'),
			title = document.createElement('h3'),
			closeBtn = document.createElement('a'),
			closeIcon = document.createElement('i'),
			textTitle = document.createTextNode(images[id].title)//<images[id]

			if(type == 'photo'){
				photo.setAttribute('src',images[id].src)
				photo.setAttribute('title',images[id].title)
				title.appendChild(textTitle)
				overlay.setAttribute('class','overlay')
				overlayContent.setAttribute('class','overlay-content')
				closeIcon.setAttribute('class','close icon ion-close-round')
				closeBtn.appendChild(closeIcon)


				overlayContent.appendChild(closeBtn)
				overlayContent.appendChild(photo)
				overlayContent.appendChild(title)

				overlayContent.style.width = 'auto'

				if(window.innerWidth <= 480){
				  overlayContent.style.width = '80%'
				}

			}else{
				//console.log(JSON.parse(images[id].options))
				title.appendChild(textTitle)
				overlay.setAttribute('class','overlay')
				overlayContent.setAttribute('class','overlay-content')
				closeIcon.setAttribute('class','close icon ion-close-round')
				closeBtn.appendChild(closeIcon)
				iframe.setAttribute('src','https://www.youtube.com/embed/'+fo.transformYTURL(images[id].src)+'?autoplay='+JSON.parse(images[id].options).autoPlay)


				overlayContent.appendChild(closeBtn)
				overlayContent.appendChild(iframe)
				overlayContent.appendChild(title)

				overlayContent.style.width = '80%'

				/*if(window.innerWidth > 400){
				  overlayContent.style.height = '80%'
				}*/

			}

			if(images.length > 0){
				//mostrar flechas de anterior siguient
				var anterior = document.createElement('a'),
				siguiente = document.createElement('a'),
				flechaAnt = document.createElement('i'),
				flechaSig = document.createElement('i')

				flechaAnt.setAttribute('class','prev icon ion-chevron-left')
				flechaSig.setAttribute('class','next icon ion-chevron-right')

				anterior.appendChild(flechaAnt)
				siguiente.appendChild(flechaSig)

				overlayContent.appendChild(anterior)
				overlayContent.appendChild(siguiente)

				if(id == 0){
					flechaAnt.remove()
				}else{
					if(id+1 == images.length){
						flechaSig.remove()
					}
				}

				console.log(id,images.length)

				flechaAnt.addEventListener("click", function(){
					//cambiar de elemento
					if(id <= images.length){
						//anterior
						//buscar el elemento anterior
						var opts = JSON.parse(images[id-1].options)
						if(null == opts){
							overlay.remove()
							fo.viewPhoto(id-1,'photo')
						}else{
							overlay.remove()
							fo.viewPhoto(id-1,'video')
						}
						//console.log(id,opts)
						//fo.viewPhoto(id-1,type)
					}
				})

				flechaSig.addEventListener("click", function(){
					//cambiar de elemento
					//buscar
					var opts = JSON.parse(images[id+1].options)
					console.log(opts)
					if(null == opts){
						overlay.remove()
						fo.viewPhoto(id+1,'photo')
					}else{
						overlay.remove()
						fo.viewPhoto(id+1,'video')
					}
				})
			}

			overlay.appendChild(overlayContent)
			
			overlay.style.opacity = 0;

			document.body.appendChild(overlay)

			setTimeout(function(){
				overlay.style.opacity = 1;
			},100)
			
			//evento clic de cerrar
			closeBtn.addEventListener("click", function(){
				//eliminar el overlay creado
				overlay.style.opacity = 0;
				setTimeout(function(){
					overlay.remove()
				},250)
				
			})

		}
	}

	window.fo = fo

})();

(function(exports) {
    
    'use strict';
    
    function PinterestGrid(options) {
       this.settings = Object.assign({
           delay: 100,
           shorterFirst: true,
           gutter: 6
       }, options);
       this.loaded = false;
       this.transform = _getTransformProperty();
       
       // Objetos del DOM
       this.$container = (options.container instanceof Node) 
            ? options.container 
            : document.querySelector(options.container);
       if (!this.$container) return false;
       
       this.$itemCollection = (options.item instanceof NodeList)
            ? options.item
            : this.$container.querySelectorAll(options.item);
       if (!this.$itemCollection || this.$itemCollection.length === 0) return false;
       
       if (!this.loaded) {
           return this.init();
       }
    }

    PinterestGrid.prototype.init = function() {
        // 1. Cambiar Estado
        this.loaded = true;
        
        // 2. Resetear Contenedor
        this.$container.style.width = '';
        
        // 3. Medidas y calculo de columnas
        var gutter = parseInt(this.settings.gutter);
        var containerWidth = this.$container.getBoundingClientRect().width;
        var itemsWidth = this.$itemCollection[0].getBoundingClientRect().width + gutter;
        var cols = Math.max(Math.floor((containerWidth - gutter) / itemsWidth), 1);
        
        // 4. Nuevo tamaño de contenedor
        containerWidth = (itemsWidth * cols + gutter) + 'px';
        this.$container.style.width = containerWidth;
        this.$container.style.position = 'relative';
        
        // 5. Posiciones de primera fila
        var itemsPosY = [];
        var itemsPosX = [];
        for (var i = 0; i < cols; i++) {
            itemsPosX.push(i * itemsWidth + gutter);
            itemsPosY.push(gutter);
        }
        
        // 6. Recorrer elementos
        Array.from(this.$itemCollection).forEach(function(item, i) {
            var firstItem, itemIndex, posX, posY;
            
            if (this.settings.shorterFirst) {
                // 7.a Espacio mas pequeño primero
                firstItem = itemsPosY.slice(0).sort(function(a, b){ return a - b }).shift();
                itemIndex = itemsPosY.indexOf(firstItem);
            } else {
                // 7.b Order natural
                itemIndex = i % cols;
            }
            
            posX = itemsPosX[itemIndex];
            posY = itemsPosY[itemIndex];
            
            // 8. Posicionamiento
            item.style.position = 'absolute';
            item.style.webkitBackfaceVisibility = item.style.backfaceVisibility = 'hidden';
            item.style[this.transform] = 'translate3d(' + posX + 'px, ' + posY + 'px, 0)';
            
            // 9. Actualización de posición eje Y
            itemsPosY[itemIndex] += item.getBoundingClientRect().height + gutter;
            
            // 10. Asignación de clases
            if (!/loaded/.test(item.className)) {
                setTimeout(function() {
                    item.classList.add(item.className.split(' ')[0] + '--loaded');
                }, (parseInt(this.settings.delay) * i));
            }
            
        }.bind(this));
        
        // 11. Altura del contenedor
        var containerHeight = itemsPosY.slice(0).sort(function(a, b) { return a - b }).pop();
        this.$container.style.height = containerHeight + 'px';
        
        // 12. Clase del contenedor
        if (!/loaded/.test(this.$container.className)) {
            this.$container.classList.add(this.$container.className.split(' ')[0] + '--loaded');
        }
        
        // 13. Ejecución de callback
        if (typeof this.settings.callback === 'function') {
            this.settings.callback(this.$itemCollection);
        }
    };


    function _getTransformProperty() {
        var style = document.createElement('div').style;
        var transform;
        var vendorProp;

        if (undefined !== style[vendorProp = 'webkitTransform']) {
            transform = vendorProp;
        }

        if (undefined !== style[vendorProp = 'msTransform']) {
            transform = vendorProp;
        }

        if (undefined !== style[vendorProp = 'transform']) {
            transform = vendorProp;
        }

        return transform;
    }

    
    // AMD
    if (typeof define === 'function' && define.amd) {
        define(function(){ return PinterestGrid });
    }
    
    // CommonJS
    else if (typeof module !== 'undefined' && module.exports) {
        module.exports = PinterestGrid;
    }
    
    // Global
    else {
        exports.PinterestGrid = PinterestGrid;
    }
} (this));

 (function () {
        'use strict';
        
        window.onload = function(){
			 	fo.init()

			 //setTimeout(function(){

			 	// 1. Plugin
		        var grid = new PinterestGrid({
		            container: '.fancy-overlay',
		            item: '.fancy-image',
		            gutter: 10,
		            delay: 200
		        });
		        
		        // 2. Redimensionamiento
		        window.addEventListener('resize', function() {
		            grid.init();
		            console.log('here')
		        });

		        // 3. Reajuste al cargar imagenes
		        Array.from(document.querySelectorAll('.fancy-image img')).forEach(function(item) {
		            item.addEventListener('load', function() {
		                grid.init();
		                item.removeEventListener('load');
		            }, false);
		        });

			// },100)
		}
        
    } ());
              
            
!
999px

Console