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

              
                #player
  ul.header#header
  
  ul#playlist
        
  ul#controls
    li#previous: .fa.fa-step-backward(onclick="player.previous()")
    li#stop: .fa.fa-stop(onclick="player.stop()")
    li#play: .fa.fa-play(onclick="player.play()")
    li#next: .fa.fa-step-forward(onclick="player.next()")
              
            
!

CSS

              
                // Utilities

// px to ems
@function em($u) {
  @return ($u/16)+em;
}
// color maps
@function color($shades, $shade: 'base') {
  @return map-get(map-get($color, $shades), $shade);
}

//config
$font-primary: 'Nunito', sans-serif;
$font-icons: FontAwesome;
$color-primary: #607D8B;
$color: (
  white: (
    x-light    : transparentize(white, 0.7),
    light    : transparentize(white, 0.5),
    base 		 : white,
  ),
  primary: (
    light    : transparentize($color-primary, 0.75),
    base     : $color-primary,
    dark     : darken($color-primary, 20%),
  )
);

// Extends
%center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

// GLOBAL
img {
  max-width: 100%;
  max-height: 100%;
}
ul, ol {
  list-style: none;
  margin: 0;
  padding: 0;
}


// PLAYER
#player {
  width: em(420);
  margin: auto;
}

.header {
  text-align: center;
  margin: auto;
  color: color(primary);
  background: color(white);
  padding: em(10);
  border-top-right-radius: em(5);
  border-top-left-radius: em(5);
  &__image {
    height: em(120);
    width: em(120);
    margin: auto;
    padding: em(4);
    margin-bottom: em(8);
    border: em(2) solid color(primary);
    border-radius: em(5);
  }
  &__title {
    font-size: em(32);
  }
  &__artist {
    font-size: em(18);
    opacity: 0.75;
  }
  &__duration {
    margin-top: em(8);
    opacity: 0.5;
  }
}



.song {
  > li {
    display: inline-block;
    vertical-align: middle;
    padding: em(4);
    text-overflow: ellipsis;
  }
  &__image {
    height: em(36);
    width:  em(36);
  }
  &__title {
    width: em(130);
  }
  &__artist {
    width: em(130);
  }
  &__duration {
    width: em(40);
    opacity: 0.5;
  }
  &__isplaying {
    width: em(20);
  }
}

#playlist {
  background: color(white, x-light);
  > ul {
    &:hover, &.active {
      background: color(white, x-light);
    }
  }
}

#controls {
  display: flex;
  justify-content: center;
  color: color(primary, light);
  background: color(white);
  border-bottom-right-radius: em(5);
  border-bottom-left-radius: em(5);
  user-select: none;
  > li {
    font-size: 1.5em;
    padding: em(8);
    cursor: pointer;
    &:hover {
      color: color(primary);
    }
    &:active {
      color: color(primary, dark);
    }
  }
  #play {
    display: none;
  }
}

@keyframes volumePlaying {
  50% { opacity: 1; }
}
.fa {
  &-volume-off {
   opacity: 0.5; 
  }
  &-volume-up {
   opacity: 0.5; 
   animation: volumePlaying 2s infinite;
  }
  
}

// DEMO STYLES
body {
  font: 100% $font-primary;
  font-weight: 100;
  color: white;
  background-color: color(primary);
}
#player {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

// codepen viewbox zoom
@media screen and (max-height: 420px){
  body {
    font-size: 75%
  }
}
              
            
!

JS

              
                class Song {
  constructor(title, artist, duration, image) {
    this.title = title
    this.artist = artist
    this.duration = duration
    this.imageUrl = image
    this.isPlaying = false
  }
  play() {
    this.isPlaying = true
  }
  stop() {
    this.isPlaying = false
  }
  toHTML(block, index) {
    // block as in BEM for `header` and `song`
    return `
      <ul class="${block} ${this.isPlaying ? 'active' : ''}" onclick="player.select(${index})">
        <li class="${block}__image"><img src="${this.imageUrl}" alt="${this.title} ${this.artist}"></li>
        <li class="${block}__title">${this.title}</li>
        <li class="${block}__artist">${this.artist}</li>
        <li class="${block}__duration">${this.duration}</li>
        ${ (block === 'song') ? `<li class="${block}__isplaying"><i class="fa fa-volume-${this.isPlaying ? 'up' : 'off'}"></i></li>` : '' }
      </ul>`
  }
}

class Player {
  constructor() {
    this.songs = []
    this.nowPlayingIndex = 0
    this.$header = $('#header')
    this.$playlist = $('#playlist')
    this.$stop = $('#stop')
    this.$play = $('#play')
  }
  add(songs) {
    songs.forEach(song => {
      this.songs.push(song)
    })
  }
  render() {
    this.renderPlaylist()
    this.renderHeader()
  }
  play() {
    this.songs[this.nowPlayingIndex].play()
    this.$play.hide()
    this.$stop.show()
    this.render()
  }
 stop() {
    this.songs[this.nowPlayingIndex].stop()
    this.$stop.hide()
    this.$play.show()
    this.render()
  }
  next() {
    this.stop()
    this.nowPlayingIndex++
    if(this.nowPlayingIndex === this.songs.length) {
      this.nowPlayingIndex = 0
    }
    this.play()
  }
  previous() {
    this.stop()
    this.nowPlayingIndex--
    if(this.nowPlayingIndex < 0) {
      this.nowPlayingIndex = (this.songs.length - 1)
    }
    this.play()
  }
  select(index) {
    this.stop()
    this.nowPlayingIndex = index
    this.play()
  }
  renderPlaylist() {
    this.$playlist.html('')
    this.songs.forEach( (song, index)=> {
      this.$playlist.append(song.toHTML('song', index))
    })
  }
  renderHeader() {
    this.$header.html('')
    this.$header.html(this.songs[this.nowPlayingIndex].toHTML('header'))
  }
  render() {
    this.renderPlaylist()
    this.renderHeader()
  }
}

let player = new Player

// Song list
player.add([
  new Song('Counting Starts', 
           'OneRepublic', '4:17', 
           'https://upload.wikimedia.org/wikipedia/en/3/37/OneRepublic_Counting_Stars_cover.png'),
  new Song('Do it', 
           'Tuxedo', 
           '4:19', 
           'http://static.djbooth.net/pics-artist/tuxedo.jpg'),
  new Song('Paradise', 
           'Coldplay', 
           '4:38', 
           'http://popcrush.com/files/2011/09/coldplay-paradise.jpg')
])

player.play()
              
            
!
999px

Console