cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <div class="puzzle-container">
  <div class="puzzleboard" id="puzzleboard">
    <div class="row">
      <img src="https://cloud.githubusercontent.com/assets/12915446/21251398/446dada2-c302-11e6-89f1-ae0cd25d9188.png" alt="puzzle piece top">
      <div data-cell="p1"></div>
      <div data-cell="p2"></div>
      <div data-cell="p3"></div>
      <div data-cell="p4"></div>
      <div data-cell="p5"></div>
    </div>
    <div class="row">
      <img src="https://cloud.githubusercontent.com/assets/12915446/21251396/446bd7de-c302-11e6-8152-605edeaa31b9.png" alt="puzzle piece bottom">
      <div data-cell="p6"></div>
      <div data-cell="p7"></div>
      <div data-cell="p8"></div>
      <div data-cell="p9"></div>
      <div data-cell="p10"></div>
      <div data-cell="p11"></div>
      <div data-cell="p12"></div>
      <div data-cell="p13"></div>
      <div data-cell="p14"></div>
    </div>
  </div>

  <!-- Assets created with Picmonkey https://www.picmonkey.com -->
  <div class="tray" id="tray">
    <button type="button" id="shuffle-btn"><img src="https://cloud.githubusercontent.com/assets/12915446/21251400/447ab2fe-c302-11e6-9015-8bb30145d1ca.png" alt="shuffle button"></button>
    <button type="button" id="reset-btn"><img src="https://cloud.githubusercontent.com/assets/12915446/21251401/447f14fc-c302-11e6-918b-9600d6a95195.png" alt="reset button"></button>
    <img id="p1" src="https://cloud.githubusercontent.com/assets/12915446/21251380/4424e658-c302-11e6-8958-b5d090ec9260.png" alt="puzzle piece">
    <img id="p2" src="https://cloud.githubusercontent.com/assets/12915446/21251373/440c3676-c302-11e6-9ae0-533b8a58cbe8.png" alt="puzzle piece">
    <img id="p3" src="https://cloud.githubusercontent.com/assets/12915446/21251382/4439e0e4-c302-11e6-9286-04af8204377c.png" alt="puzzle piece">
    <img id="p4" src="https://cloud.githubusercontent.com/assets/12915446/21251384/443c480c-c302-11e6-8307-ae3697863fb4.png" alt="puzzle piece">
    <img id="p5" src="https://cloud.githubusercontent.com/assets/12915446/21251395/4467d54e-c302-11e6-916b-2c14e5fbb8ee.png" alt="puzzle piece">
    <img id="p6" src="https://cloud.githubusercontent.com/assets/12915446/21251374/440cdcc0-c302-11e6-8072-40852e1fe9c8.png" alt="puzzle piece">
    <img id="p7" src="https://cloud.githubusercontent.com/assets/12915446/21251379/4424cd3a-c302-11e6-95d9-1c37114df53b.png" alt="puzzle piece">
    <img id="p8" src="https://cloud.githubusercontent.com/assets/12915446/21251387/444bc570-c302-11e6-9ffe-a8e27d5636f1.png" alt="puzzle piece">
    <img id="p9" src="https://cloud.githubusercontent.com/assets/12915446/21251376/44223296-c302-11e6-9c80-c2af0158f78d.png" alt="puzzle piece">
    <img id="p10" src="https://cloud.githubusercontent.com/assets/12915446/21251389/44529daa-c302-11e6-9d1c-f6072122bb9d.png" alt="puzzle piece">
    <img id="p11" src="https://cloud.githubusercontent.com/assets/12915446/21251393/4463a960-c302-11e6-8b40-a84e22c78758.png" alt="puzzle piece">
    <img id="p12" src="https://cloud.githubusercontent.com/assets/12915446/21251385/443c1274-c302-11e6-8244-88310e46ae29.png" alt="puzzle piece">
    <img id="p13" src="https://cloud.githubusercontent.com/assets/12915446/21251369/4409bb08-c302-11e6-97cf-80a701a8e852.png" alt="puzzle piece">
    <img id="p14" src="https://cloud.githubusercontent.com/assets/12915446/21251390/445760ba-c302-11e6-983c-102dbb34188f.png" alt="puzzle piece">
  </div>
</div>

<div>
  <!-- Audio for puzzle piece drop -->
  <audio id="plop-sound" src="https://azureowl.github.io/christmaspuzzle/sounds/Plop.mp3" preload="auto">
    Your browser does not support the <code>audio</code> element.
  </audio>
  <!-- Audio for end of game -->
  <audio id="bell-sound" src="https://azureowl.github.io/christmaspuzzle/sounds/Small-bell-ringing.wav" preload="auto">
    Your browser does not support the <code>audio</code> element.
  </audio>
  <!-- Audio for end of game, song -->
  <audio id="song-sound" src="https://azureowl.github.io/christmaspuzzle/sounds/We-wish-you-a-merry-christmas-bells.mp3" preload="auto">
    Your browser does not support the <code>audio</code> element.
  </audio>
</div>
            
          
!
            
              * {
  box-sizing: border-box;
}

html, body {
  background-color: #4e7ca0;
  height: 100%;
  margin: 0;
  padding: 0;
}

.puzzle-container {
  height: 100%;
}

.puzzleboard {
  background-color: #4e7ca0;
  min-height: 65%;
  margin: 0 auto;
  position: relative;
  width: 100%;
}

.row {
  height: 50%;
  position: relative;
  width: 100%;
}

/* Puzzleboard drop zone shapes */
.row > img {
  display: block;
  height: auto;
  width: 100%;
}

.row:last-of-type {
  padding-top: 1%;
}

.row div {
  display: inline-block;
  height: 100%;
  position: absolute;
  top: 0;
  width: 14%;
  z-index: 100;
}

.row:first-of-type div img {
  margin-top: 14.8%;
}

.row:last-of-type div img {
  margin-top: 7.8%;
}

.row div img {
  width: 100%;
  -webkit-backface-visibility: visible;
  backface-visibility: visible;
}

/* Merry */
/* M */
.row:first-of-type div:first-of-type {
  margin-left: 15.8%;
}
/* E */
.row:first-of-type div:nth-child(3) {
  margin-left: 27.6%;
}
/* R */
.row:first-of-type div:nth-child(4) {
  margin-left: 39.7%;
}
/* R */
.row:first-of-type div:nth-child(5) {
  margin-left: 53.9%;
}
/* Y */
.row:first-of-type div:nth-child(6) {
  margin-left: 66.77%;
}

/* Christmas */
/* C */
.row:last-of-type div:first-of-type {
  margin-left: -.28%;
}
/* H */
.row:last-of-type div:nth-child(3) {
  margin-left: 10%;
}
/* R */
.row:last-of-type div:nth-child(4) {
  margin-left: 21%;
}
/* I */
.row:last-of-type div:nth-child(5) {
  margin-left: 30%;
}
/* S */
.row:last-of-type div:nth-child(6) {
  margin-left: 39.5%;
}
/* T */
.row:last-of-type div:nth-child(7) {
  margin-left: 49.7%;
}
/* M */
.row:last-of-type div:nth-child(8) {
  margin-left: 62.5%;
}
/* A */
.row:last-of-type div:nth-child(9) {
  margin-left: 73.8%;
}
/* S */
.row:last-of-type div:nth-child(10) {
  margin-left: 83.9%;
}

.tray {
  background-color: #86ac70;
  border: 1em solid #699965;
  bottom: 0;
  height: 35%;
  padding: 8em;
  position: relative;
  width: 100%;
}

/* Individual puzzle pieces with drop shadow and hover effect*/
.tray > img {
  position: absolute;
  transition: all .5s;
  width: 14%;

  -webkit-filter: drop-shadow(.1em .1em 0 #4a4848);
  filter: drop-shadow(.1em .1em 0 #4a4848);
}

.tray > img:hover {
  -webkit-transform: scale(1.05);
  transform: scale(1.05);

  -webkit-filter: drop-shadow(0 0 .6em #faffa6);
  filter: drop-shadow(0 0 .6em #faffa6);
}

button {
  background-color: transparent;
  border: none;
  cursor: pointer;
  height: 3.5em;
  left: 0;
  margin: 0 auto;
  position: absolute;
  right: 0;
  text-align: center;
  top: -3em;
  transition: transform .3s;
  width: 7em;
  z-index: 100;
}

button:focus {
  outline: none;
}

button:active {
  -webkit-transform: scale(.9);
  transform: scale(.9);
}

button img {
  width: 5em;
}

button:nth-child(2) {
  display: none;
}

.xmas {
  -webkit-animation-delay: 0s, 11s;
  -webkit-animation-duration: 3s, 1s;
  -webkit-animation-iteration-count: 3, 1;
  -webkit-animation-name: spin, jingle;
  animation-delay: 0s, 11s;
  animation-duration: 3s, 1s;
  animation-iteration-count: 3, 1;
  animation-name: spin, jingle;
}

img[data-twirl] {
  -webkit-animation-delay: 0s, 11s;
  -webkit-animation-duration: .7s, 1s;
  -webkit-animation-iteration-count: 1, 1;
  -webkit-animation-name: twirl, jingle;
  animation-delay: 0s, 11s;
  animation-duration: .7s, 1s;
  animation-iteration-count: 1, 1;
  animation-name: twirl, jingle;
}

@-webkit-keyframes spin {
  0% {
    transform: rotate3d(1,2,1,0deg);
  }
  100% {
    transform: rotate3d(1,2,1,360deg);
  }
}

@keyframes spin {
  0% {
    transform: rotate3d(1,2,1,0deg);
  }
  100% {
    transform: rotate3d(1,2,1,360deg);
  }
}

@-webkit-keyframes twirl {
  0% {
    transform: rotate3d(0,1,0,0deg);
  }
  100% {
    transform: rotate3d(0,1,0,360deg);
  }
}

@keyframes twirl {
  0% {
    transform: rotate3d(0,1,0,0deg);
  }
  100% {
    transform: rotate3d(0,1,0,360deg);
  }
}

@-webkit-keyframes jingle {
  0% {
    transform: rotateZ(0);
  }
  25% {
    transform: rotateZ(-10deg);
  }
  50% {
    transform: rotateZ(10deg);
  }
  100% {
    transform: rotateZ(0deg);
  }
}

@keyframes jingle {
  0% {
    transform: rotateZ(0);
  }
  25% {
    transform: rotateZ(-10deg);
  }
  50% {
    transform: rotateZ(10deg);
  }
  100% {
    transform: rotateZ(0deg);
  }
}


@media (min-width: 2000px) {
  button {
    top: -4em;
    width: 9em;
  }

  button img {
    width: 7.5em;
  }
}

@media (max-width: 1024px) {
  button {
    height: 3em;
    top: -2em;
    width: 5em;
  }

  button:active {
    -webkit-transform: scale(.9);
    transform: scale(.9);
  }

  button img {
    width: 3.5em;
  }
}

@media (max-width: 768px) {
  .puzzleboard, .tray {
    height: 50vh;
    width: 768px;
  }
}

            
          
!
            
              window.onload = function () {
// Define draggable items and drag's data
var draggables = {
  init: function () {
    this.cacheDOM();
    this.bindEvents();
  },
  cacheDOM: function () {
    this.parent_puzzlePieces = document.getElementById("tray");
  },
  bindEvents: function () {
    this.parent_puzzlePieces.addEventListener("mousedown", this.addAttr);
    this.parent_puzzlePieces.addEventListener("dragstart", this.dragStart);
  },
  addAttr: function (e) {
    if (e.target && e.target.nodeName === "IMG") {
      e.target.setAttribute("draggable","true");
    }
  },
  dragStart: function (e) {
    e.dataTransfer.setData("image/png", e.target.id);
    e.dataTransfer.dropEffect = "move";
  }
};

// Define drop zones
var dropZone = {
  count: 0,
  init: function () {
    this.cacheDOM();
    this.bindEvents();
  },
  cacheDOM: function () {
    this.puzzleBoard = document.getElementById("puzzleboard");
    this.plopAudio = document.getElementById("plop-sound");
  },
  bindEvents: function () {
    this.puzzleBoard.addEventListener("dragover",this.dragItem);
    this.puzzleBoard.addEventListener("drop",this.dropItem);
  },
  dragItem: function (e) {
    e.preventDefault();
    e.dataTransfer.dropEffect = "move";
  },
  dropItem: function (e) {
    e.preventDefault();
    var pieceData = e.dataTransfer.getData("image/png");
    // Only drop if the cell matches the pieces
    if (pieceData === e.target.getAttribute("data-cell")) {
      e.target.appendChild(document.getElementById(pieceData));
      dropZone.plopAudio.play();
      dropZone.count++;
    }
    // Once all the pieces are placed, call completedPuzzle.init() to play music and animation
    if (dropZone.count === 14) {
      completedPuzzle.init();
      btnShuffle.btn.style.display = "none";
      btnReset.btn.style.display = "block";
    }
  }
};

var completedPuzzle = {
  timerArray: [],
  init: function () {
    this.cacheDOM();
    this.applyClass();
    this.hideBoard();
    this.wishXMasSong();
  },
  cacheDOM: function () {
    this.puzzlePieces = document.querySelectorAll("img[id*=p]");
    this.board = document.querySelectorAll("img[src*=puzzleboard]");
    this.piece1 = document.getElementById("p7");
    this.piece2 = document.getElementById("p3");
    this.piece3 = document.getElementById("p13");
    this.piece4 = document.getElementById("p2");
    this.piece5 = document.getElementById("p1");
    this.xmasAudio = document.getElementById("song-sound");
    this.bellAudio = document.getElementById("bell-sound");
  },
  applyClass: function () {
    for (var i = 0; i < completedPuzzle.puzzlePieces.length; i++) {
      completedPuzzle.puzzlePieces[i].setAttribute("class","xmas");
    }
  },
  hideBoard: function () {
    for (var i = 0; i < this.board.length; i++) {
      this.board[i].style.opacity = "0";
    }
  },
  wishXMasSong: function () {
    this.xmasAudio.play();
    this.setTimers();
  },
  setTimers: function () {
    this.timerArray = [
      window.setTimeout(this.piece1State, 9000),
      window.setTimeout(this.piece2State, 9100),
      window.setTimeout(this.piece3State, 9300),
      window.setTimeout(this.piece4State, 9700),
      window.setTimeout(this.piece5State, 10300),
      window.setTimeout(this.ring, 10700)
    ];
  },
  piece1State: function () {
    completedPuzzle.piece1.setAttribute("data-twirl","true");
  },
  piece2State: function () {
    completedPuzzle.piece2.setAttribute("data-twirl","true");
  },
  piece3State: function () {
    completedPuzzle.piece3.setAttribute("data-twirl","true");
  },
  piece4State: function () {
    completedPuzzle.piece4.setAttribute("data-twirl","true");
  },
  piece5State: function () {
    completedPuzzle.piece5.setAttribute("data-twirl","true");
  },
  ring: function () {
    completedPuzzle.bellAudio.play();
  }
};

var btnShuffle =  {
  init: function () {
    this.cacheDOM();
    this.bindEvents();
  },
  cacheDOM: function () {
    this.btn = document.getElementById("shuffle-btn");
  },
  bindEvents: function () {
    this.btn.addEventListener("click", this.startShuffle);
  },
  startShuffle: function () {
    shuffler.init();
  }
};

var btnReset =  {
  init: function () {
    this.cacheDOM();
    this.bindEvents();
  },
  cacheDOM: function () {
    this.btn = document.getElementById("reset-btn");
  },
  bindEvents: function () {
    this.btn.addEventListener("click", this.start);
  },
  start: function () {
    startOver.init();
  }
};

var startOver = {
  init: function () {
    this.cacheDOM();
    this.resetPuzzle();
    this.removeAttr();
    shuffler.init();
  },
  cacheDOM: function () {
    this.puzzlePieces = document.querySelectorAll("img[id*=p]");
    this.board = document.querySelectorAll("img[src*=puzzleboard]");
    this.audio = document.getElementsByTagName("audio");
  },
  removeAttr: function () {
    for (var i = 0; i < this.puzzlePieces.length; i++) {
      this.puzzlePieces[i].removeAttribute("class","xmas");
      this.puzzlePieces[i].removeAttribute("data-twirl","true");
    }
  },
  resetPuzzle: function () {
    for (var i = 0; i < this.board.length; i++) {
      this.board[i].style.opacity = "1";
    }
    btnShuffle.btn.style.display = "block";
    btnReset.btn.style.display = "none";
    this.clearTimers();
    this.resetAudio();
    dropZone.count = 0;
    dropZone.init();
  },
  clearTimers: function () {
    for (var i = 0; i < completedPuzzle.timerArray.length; i++) {
      window.clearTimeout(completedPuzzle.timerArray[i]);
    }
  },
  resetAudio: function () {
    var i = 2;
    while (i > 0) {
      this.audio[i].pause();
      this.audio[i].currentTime = 0;
      i--;
    }
  }
};

var shuffler = {
  init: function () {
    this.cacheDOM();
    this.convertToArr();
  },
  cacheDOM: function () {
    this.puzzlePieces = document.querySelectorAll("img[id*=p]");
    this.parent_puzzlePieces = document.getElementById("tray");
  },
  convertToArr: function () {
    var nodesArray = Array.prototype.slice.call(this.puzzlePieces);
    this.shuffleIt(nodesArray);
  },
  shuffleIt: function (arr) {
    var length = arr.length,
        randomN,
        beShuffled;
    while (length) {
      randomN = Math.floor(Math.random() * length);
      beShuffled = arr.splice(randomN,1);
      arr.push(beShuffled[0]);
      --length;
    }
    this.shuffleTray(arr);
  },
  randomPosition: function (top, left) {
    if (top) {
      return Math.floor(Math.random() * 1 + 2);
    } else {
      return Math.floor(Math.random() * 75 + 5);
    }
  },
  shuffleTray: function (arr) {
    for (var i = 0; i < arr.length; i++) {
      this.parent_puzzlePieces.appendChild(arr[i]);
      arr[i].style.left = this.randomPosition(0,1) + "%";
      arr[i].style.top = this.randomPosition(1,0) + "em";
    }
  }
};

draggables.init();
dropZone.init();
btnShuffle.init();
btnReset.init();
shuffler.init();
};

            
          
!
999px
Loading ..................

Console