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

              
                <div class="bg">
  <img src="https://images.unsplash.com/photo-1514320291840-2e0a9bf2a9ae?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2700&q=80" alt="">
</div>

<div id="player">
  <div id="picker">
    <label id="click-label" for="input">Add Track(s) to Playlist</label>
    <input type="file" id="input" accept="audio/*" multiple>
  </div>
  <button id="minimize"></button>
  <ol id="playlist">
  </ol>
  <div id="controls">
    <button id="prev">
      <ion-icon name="play-skip-back-outline"></ion-icon>
    </button>
    <button id="play">
      <ion-icon id="play-pause-icon" name="play-outline"></ion-icon>
    </button>
    <button id="stop">
      <ion-icon name="stop-outline"></ion-icon>
    </button>
    <button id="next">
      <ion-icon name="play-skip-forward-outline"></ion-icon>
    </button>
    <div id="progress">
      <progress id="progress-percent" max="100" value="0"></progress>
    </div>
  </div>
</div>
<audio id="audio"></audio>
              
            
!

CSS

              
                body {
  box-sizing: border-box;
  margin: 0;
  height: 100vh;
  padding: 0;
  width: 100%;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: "Oswald", sans-serif;
}

.bg {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: -1;
}

img {
  height: 100%;
  width: 100vw;
  object-fit: cover;
  filter: brightness(0.2);
}

input[type="file"] {
  display: none;
}

#player {
  background: #e4e4e4;
  display: inline-block;
  border-radius: 15px;
  position: relative;
  width: 45vw;
  min-width: 250px;
  max-width: 425px;
  overflow: hidden;
  text-align: center;
}

#picker {
  text-align: center;
  padding: 1em 2em 0.5em 2em;
}

#picker label {
  background: rgba(59, 136, 253, 0.8);
  padding: 0.15em 0.9em;
  font-size: 0.8em;
  letter-spacing: 0.05em;
  color: whitesmoke;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s ease-in-out;
}

#picker label:hover {
  background: rgba(255, 69, 0, 0.9);
}

#minimize {
  position: absolute;
  top: 0.75em;
  right: 0.75em;
  border-radius: 50%;
  border: none;
  width: 16px;
  height: 16px;
  font-size: 1em;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #ffbd44;
  opacity: 0.66;
  outline: none;
}

#minimize:hover {
  opacity: 1;
}

#playlist {
  color: #555;
  background: #222;
  font-size: 1em;
  padding: 0.4em 2em;
  max-height: 120px;
  overflow-y: scroll;
  margin-top: 0.5em;
  margin-bottom: 1em;
  user-select: none;
  border: 7px solid #e4e4e4;
  border-radius: 14px;
}

#playlist li {
  border-bottom: 1px dotted rgba(85, 85, 85, 0.4);
  cursor: pointer;
}

#controls {
  margin-bottom: 1em;
  margin-left: auto;
  margin-right: auto;
  margin-top: 0.75em;
  background: transparent;
  text-align: center;
  border-radius: 20px;
  width: 85%;
  padding-top: 1em;
  border-right: 1px solid #609cf4;
  border-left: 1px solid #609cf4;
}

#controls button {
  color: orangered;
  font-size: 1.25em;
  background: #e4e4e4;
  border-radius: 5px;
  border: 1px solid #3b88fd;
  text-align: center;
  line-height: 0.5;
  cursor: pointer;
  margin-right: 0.2em;
  transition: all 0.2s;
  outline: none;
}

#controls button:hover {
  border-color: orangered;
  color: whitesmoke;
  background: #3b88fd;
}

progress {
  cursor: pointer;
  padding-bottom: 0.5em;
  width: 90%;
  filter: brightness(0.75);
}

@media (max-width: 600px) {
  #player {
    width: 50vw;
  }
}

@media (max-width: 455px) {
  #player {
    width: 60vw;
  }
}

              
            
!

JS

              
                const input = document.getElementById("input");
const label = document.getElementById("click-label");
const playList = document.getElementById("playlist");
const playBtn = document.getElementById("play");
const playIcon = document.getElementById("play-pause-icon");
const stopBtn = document.getElementById("stop");
const prevBtn = document.getElementById("prev");
const nextBtn = document.getElementById("next");
const audio = document.getElementById("audio");
const progress = document.getElementById("progress-percent");
const minimize = document.getElementById("minimize");
const picker = document.getElementById("picker");
const controls = document.getElementById("controls");
const player = document.getElementById("player");

let trackList = [];
let currentSong = 0;
let isPlaying = false;
let minimized = false;
let playlistEls;

input.addEventListener("change", handleFiles, false);

function handleFiles() {
  audio.pause();
  isPlaying = false;
  playIcon.name = "play-outline";
  playList.innerHTML = "";
  currentSong = 0;
  trackList = [];
  const files = this.files;
  for (let i = 0; i < files.length; i++) {
    let listItem = document.createElement("li");
    listItem.classList.add("list-item");
    listItem.textContent = files[i].name.slice(0, files[i].name.indexOf("."));
    playList.appendChild(listItem);
    let objectURL = window.URL.createObjectURL(files[i]);
    trackList.push(objectURL);
  }
  let song = trackList[currentSong];
  audio.src = song;
  playlistEls = document.getElementsByClassName("list-item");
  [...playlistEls].forEach((el, ind) => {
    el.addEventListener("click", (i) => playClickedSong(ind));
    playlistEls[currentSong].style.color = "rgba(255, 165, 0, 0.5)";
  });
}

function playTrack() {
  if (!isPlaying && playlistEls) {
    isPlaying = !isPlaying;
    playIcon.name = "pause-outline";
    updatePlaylistStyle();
    changeBorders();
    audio.play();
  } else {
    if (playlistEls) {
      isPlaying = !isPlaying;
      changeBorders();
      playlistEls[currentSong].style.color = "rgba(255, 165, 0, 0.5)";
      playIcon.name = "play-outline";
      audio.pause();
    }
  }
}

function stopPlayback() {
  if (isPlaying) {
    audio.pause();
    audio.currentTime = 0;
    isPlaying = false;
    changeBorders();
    playIcon.name = "play-outline";
    [...playlistEls].forEach((el) => (el.style.color = "#555"));
    playlistEls[currentSong].style.color = "rgba(255, 165, 0, 0.5)";
  } else {
    if (playlistEls) {
      audio.currentTime = 0;
    }
  }
}

function playPrev() {
  if (playlistEls) {
    audio.pause();
    audio.currentTime = 0;
    progress.value = 0;
    currentSong--;
    if (currentSong < 0) currentSong = trackList.length - 1;
    updatePlaylistStyle();
    audio.src = trackList[currentSong];
    if (isPlaying) {
      audio.play();
    } else {
      playlistEls[currentSong].style.color = "rgba(255, 165, 0, 0.5)";
    }
  }
}

function playNext() {
  if (playlistEls) {
    audio.pause();
    audio.currentTime = 0;
    progress.value = 0;
    currentSong++;
    if (currentSong > trackList.length - 1) currentSong = 0;
    updatePlaylistStyle();
    audio.src = trackList[currentSong];
    if (isPlaying) {
      audio.play();
    } else {
      playlistEls[currentSong].style.color = "rgba(255, 165, 0, 0.5)";
    }
  }
}

function displayProgress() {
  const currentTime = audio.currentTime;
  const progressPercent = (currentTime / audio.duration) * 100;
  progress.value = Number.isFinite(progressPercent)
    ? progressPercent.toFixed(2)
    : "0";
}

function scrub(e) {
  if (playlistEls) {
    const scrubTime = (e.offsetX / progress.offsetWidth) * audio.duration;
    audio.currentTime = scrubTime;
    if (isPlaying) {
      audio.play();
    }
  }
}

function playClickedSong(ind) {
  audio.pause();
  audio.currentTime = 0;
  currentSong = ind;
  updatePlaylistStyle();
  playIcon.name = "pause-outline";
  isPlaying = true;
  changeBorders();
  audio.src = trackList[currentSong];
  audio.play();
}

function changeBorders() {
  if (isPlaying) {
    controls.style.borderRight = "1px solid orangered";
    controls.style.borderLeft = "1px solid orangered";
  } else {
    controls.style.borderRight = "1px solid #609cf4";
    controls.style.borderLeft = "1px solid #609cf4";
  }
}

function updatePlaylistStyle() {
  [...playlistEls].forEach((el) => (el.style.color = "#555"));
  playlistEls[currentSong].style.color = "orange";
  playlistEls[currentSong].scrollIntoView();
}

playBtn.addEventListener("click", playTrack);
stopBtn.addEventListener("click", stopPlayback);
nextBtn.addEventListener("click", playNext);
prevBtn.addEventListener("click", playPrev);
audio.addEventListener("timeupdate", displayProgress);
audio.addEventListener("ended", playNext);
progress.addEventListener("mousedown", () => audio.pause());
progress.addEventListener("mouseup", scrub);
minimize.addEventListener("click", () => {
  if (!minimized) {
    picker.style.display = "none";
    playList.style.maxHeight = "14px";
    playList.style.margin = "auto";
    player.style.padding = "1.5em 0.5em 0.25em 0.5em";
    if (playlistEls) {
      playlistEls[currentSong].scrollIntoView();
    }
    minimize.style.background = "#00ca4e";
    minimize.style.top = "0.5em";
    minimize.style.right = "0.5em";
    minimized = !minimized;
  } else {
    picker.style.display = "block";
    playList.style.maxHeight = "120px";
    minimize.style.background = "#ffbd44";
    minimize.style.top = ".75em";
    minimize.style.right = ".75em";
    minimized = !minimized;
  }
});

              
            
!
999px

Console