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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
                <h1>Single Asset Image Loading</h1>
  
  <p>As images in the DOM are loaded by the browser, we capture the "load" event and use it to fade in our images with CSS3, hiding the traditional line-by-line loading from the user.</p>

<div id="Sandbox">
  
    <!-- BASIC EXAMPLE -->

    <div class="img_wrapper">
        <img src="http://www.drummerworld.com/pics/drum43/ElvinJones_LeeTanner.jpg" alt="Elvin Jones" onload="imgLoaded(this)"/>
    </div>

    <!-- WITH CSS LOADING SPINNER -->
  
    <div class="img_wrapper">
        <div class="css_spinner">
            <div class="half left">
                <div class="band"></div>
		        </div>
		        <div class="half right">
			          <div class="band"></div>
		        </div>
        </div>
        <img src="http://www.drummerworld.com/pics/drum43/ElvinJones_LeeTanner.jpg" alt="Elvin Jones" onload="imgLoaded(this)"/>
    </div>
      
    <!-- DEMO RESET BUTTONS - NOT PART OF TECHNIQUE -->
  
  <p>Incase you missed it:</p>
  
    <button id="Unload">Unload Images</button>
    <button id="Reload">Reload Images</button>
</div>
            
          
!

CSS

            
              /* 
*  The .img_wrapper and img styles should be 
*  changed to suit your project. The only crucial
*  properties are the opacity values and CSS3 transitions
*  defined on the img tags.
*/

.img_wrapper{
    position: relative;
    padding-top: 75%;
    margin-bottom: 40px;
    background: #fafafa;
    overflow: hidden;
}

.img_wrapper img{
    position: absolute;
    width: 100%;
    top: 0;
    left: 0;
    opacity: 0;
  
    -webkit-transition: opacity .5s;
    -moz-transition: opacity .5s;
    -ms-transition: opacity .5s;
    transition: opacity .5s;
}

.img_wrapper.loaded img{
    opacity: 1;
}

.img_wrapper.loaded .css_spinner{
    display: none;
}

/*
* CSS loading spinner styles for second example.
* You could also use a background image .gif 
* on the img_wrapper, or any rotating element.
*/

.css_spinner{
	position: absolute;
	width: 48px;
	height: 48px;
	left: 50%;
	top: 50%;
	margin: -24px 0 0 -24px;
	opacity: .3;
	
	-webkit-border-radius: 999px;
	-moz-border-radius: 999px;
	border-radius: 999px;
}

.css_spinner .half{
	position: absolute;
	width: 24px;
	height: 48px;
	top: 0;
	overflow: hidden;
}

.css_spinner .left{
	left: 0px;
	
	-webkit-border-radius: 999px 0 0 999px;
	-moz-border-radius: 999px 0 0 999px;
	border-radius: 999px 0 0 999px;
}

.css_spinner .right{
	left: 24px;
	
	-webkit-border-radius: 0 999px 999px 0;
	-moz-border-radius: 0 999px 999px 0;
	border-radius: 0 999px 999px 0;
}

.css_spinner .band{
	position: absolute;
	width: 24px;
	height: 48px;
	background: #922d8d;
	
	-webkit-border-radius: 999px;
	-moz-border-radius: 999px;
	border-radius: 999px;
	
	-webkit-animation: cssSpinnerRight 1s infinite linear;
	-moz-animation: cssSpinnerRight 1s infinite linear;
}

.css_spinner .left .band{
	left: 0;
	border-left: 24px solid #fafafa;

	-webkit-animation: cssSpinnerLeft 1s infinite linear;
	-moz-animation: cssSpinnerLeft 1s infinite linear;
}

.css_spinner .right .band{
	right: 0;
	border-right: 24px solid #fafafa;
}

@-webkit-keyframes cssSpinnerLeft{
  0%{-webkit-transform:rotate(0deg)}
  25%{-webkit-transform:rotate(0deg)}
  50%{-webkit-transform:rotate(180deg)}
  75%{-webkit-transform:rotate(180deg)}
  100%{-webkit-transform:rotate(360deg)}
}

@-webkit-keyframes cssSpinnerRight{
  0%{-webkit-transform:rotate(0deg)}
  25%{-webkit-transform:rotate(180deg)}
  50%{-webkit-transform:rotate(180deg)}
  75%{-webkit-transform:rotate(360deg)}
  100%{-webkit-transform:rotate(360deg)}
}

@-moz-keyframes cssSpinnerLeft{
  0%{-moz-transform:rotate(0deg)}
  25%{-moz-transform:rotate(0deg)}
  50%{-moz-transform:rotate(180deg)}
  75%{-moz-transform:rotate(180deg)}
  100%{-moz-transform:rotate(360deg)}
}

@-moz-keyframes cssSpinnerRight{
  0%{-moz-transform:rotate(0deg)}
  25%{-moz-transform:rotate(180deg)}
  50%{-moz-transform:rotate(180deg)}
  75%{-moz-transform:rotate(360deg)}
  100%{-moz-transform:rotate(360deg)}
}

/* The following styles are not part of the technique - do not integrate! */

body{
    padding: 10px;
}

h1{
    font: 600 20px "Helvetica Neue";
    text-transform: uppercase;
    margin-bottom: 20px;
}

p{
    display: inline-block;
    font: 200 15px "Helvetica Neue";
    line-height: 1.6;
    max-width: 400px;
    margin-bottom: 30px;
    overflow: hidden;
}

#Sandbox p{
  text-align: center;
  display: block;
  max-width: 100%;
}

#Sandbox{
    width: 300px;
    margin: 0 auto;
    padding: 30px;
}

button{
    display: block;
    margin: 0 auto 20px;
    border: 0 none;
    padding: 8px 20px;
    background: #922d8d;
    color: #fff;
    font-family: 'Helvetica Neue';
    font-weight: 300;
    font-size: 16px;
    cursor: pointer;
   
    border-radius: 4px;
}



            
          
!

JS

            
              /* -- IMAGE LOADING FUNCTION -- */

/*
*  This function should be placed in a 
*  javascript file in the <head> of your page, so that 
*  it is defined before the <body> is parsed, and 
*  images are loaded.
*/

function imgLoaded(img){
    var imgWrapper = img.parentNode;

    imgWrapper.className += imgWrapper.className ? ' loaded' : 'loaded';
};



/* -- RELOAD FUNCTION-- */

/*
* This code is not part of the technique and is 
* included for demonstration purposes - do not integrate!
*/

$(function(){
    var $images = $('img');
  
    $('#Unload').click(function(){
        $images.each(function(){
            var $t = $(this),
                src = this.src,
                gif1px = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
          
            this.backupSrc = src;
          
        $t.bind('load', function(){
                $t.parent().removeClass('loaded');
                $t.unbind('load');
           }).attr('src',gif1px);
        });
    });
  
    $('#Reload').click(function(){
        $images.each(function(){
           this.src = this.backupSrc;
        });
    });
});
            
          
!
999px

Console