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

            
              <div class="mask">Chargement en cours…</div>
<main>
  <div class="ui">
  <button>
    Quelle est la réponse à La grande question sur la vie, l'univers et le reste ?
  </button>
  <div class="reponse"></div>
  </div>
  <div class="logger"></div>
</main>
            
          
!

CSS

            
              *{box-sizing: border-box;}

html, body, main {
  width : 100%;
  height: 100%;
  background-color: #222222;
  color: #FFFFFF;
  margin: 0;
  padding: 0;
}

main {
    background-image: url(http://www.nasa.gov/sites/default/files/thumbnails/image/potw1450a.jpg);
}

.mask {
  position: absolute;
  left: 0px;
  top: 0px;
  right: 0px;
  bottom: 0px;
  background-color: #000;
  opacity: 0.9;
  visibility: hidden;
  display: flex;
  justify-content: center;
  align-self: center;
  text-align: center;
  flex-direction: column;
  font-size: 24px;
}

main {
  display: flex;
  justify-content: center;
}

.ui {
  width: 50%;
  max-width: 50%;
  display: flex;
  align-items: center;
  padding-top: 0.5rem;
  flex-direction: column;
  flex-grow: 1;
}

.ui button {
  flex-grow: 0;
  max-width: 300px;
  font-size: 20px;
  padding: 1em;
  color: white;
  margin-top: 0.5rem;
}

button {
      background: #3498db;
  background-image: -webkit-linear-gradient(top, #3498db, #2980b9);
  background-image: -moz-linear-gradient(top, #3498db, #2980b9);
  background-image: -ms-linear-gradient(top, #3498db, #2980b9);
  background-image: -o-linear-gradient(top, #3498db, #2980b9);
  background-image: linear-gradient(to bottom, #3498db, #2980b9);
  border-radius: 4px;
  text-decoration: none;
}

button[disabled] {
    opacity: 0.5;
}

.reponse {
  margin: 0.5rem 0.5rem 0 0.5rem;
  font-size: 2.5rem;
  display: flex;
  width: 90%;
  height: 200px;
  border: 4px solid  #693900;
  flex-direction: column;
  text-align: center;
  justify-content: center;
  background-color: #F5CFA2;
  color : #693900;
}

.error {
  background-color: #FF4949;
  font-weight: bold;
  color: #6F0000;
}

.logger {
  flex-grow: 1;
  font-family: sans;
  font-weight: bold;
  width: 50%;
  max-width: 50%;
}

.logger span {
  display: block;
  margin: 0.5rem;
  background-color: rgba(0,0,0,0.9);
  border: 1px solid rgba(255,255,255,0.5);
  padding: 0.5rem;
}
            
          
!

JS

            
              var logger = document.querySelector('.logger');
function log(message){
  var span = document.createElement('span');
  var date = new Date()
  var dateStr = date.getHours()+":"+date.getMinutes()+":"+date.getSeconds()+" "+date.getMilliseconds();
  span.innerHTML = dateStr + " - " + message;
  logger.insertBefore(span,logger.firstChild);
}

//Définition de la fonction asynchrone
//Définition de la fonction asynchrone
function get_reponse(callback, failureCallback) {
  //On simule un appel ajax qui prend au plus 2 secondes
  setTimeout(function () {
    try {
      if(Math.random()> 0.5){
        throw TypeError("Surchauffe");
      }
      log("réponse récuperée");
      //Une fois la requête terminée, on execute le callback passé en paramètre, avec la réponse en paramètre de cette dernière
      callback(42);
    } catch(e){
      log("surfchauffe - lancement failureCallback");
      failureCallback(e);
    }
  }, Math.random() * 2000);
}

var button = document.querySelector('button');
var reponseDiv = document.querySelector('.reponse');

//Lors du clic sur le bouton
button.addEventListener('click', function(){
  log('click');
  //On indique à l'utilisateur que le calcul est en cours
  reponseDiv.innerHTML = 'Calcul en cours…';
  //On désactive le bouton pour empecher de lancer N traitements
  button.disabled = true;
  reponseDiv.classList.remove('error');
  
    //Et on lance la fonction pour obtenir notre réponse en passant une fonction qui sera appelé avec la valeur de la réponse
  get_reponse(function (reponse) {
    log("affichage réponse");
    //On affiche alors notre réponse
    reponseDiv.innerHTML = reponse;
    //Et on réactive le bouton
    button.disabled = false;
  }, function(e){
    reponseDiv.innerHTML = "Erreur lors du calcul : " + e.message;
    reponseDiv.classList.add('error');
    //Et on réactive le bouton
    button.disabled = false;
    log("affichage erreur");
  });
});
            
          
!
999px

Console