HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="c-containter">
<div class="music-container">
<section class="album-cover">
<button class="arrow left" id="prev">
<img src="https://snowleo208.github.io/100-Days-of-Code/7.%20Music%20Player/img/arrow_left.svg" alt="Next Music">
</button>
<img src="//cdn.atrera.com/images/cover_yz2mak.jpg" class="cover" alt="From One To Nine by Marcel Pequel">
<button class="arrow right" id="next">
<img src="https://snowleo208.github.io/100-Days-of-Code/7.%20Music%20Player/img/arrow_right.svg" alt="Next Music">
</button>
</section>
<section class="music-player">
<h1 class="music-player__title"></h1>
<h2 class="music-player__author"></h2>
<div class="music-time">
<p class="music-time__current"></p>
<p class="music-time__last"></p>
</div>
<div class="music-bar" id="progress">
<div id="length"></div>
</div>
<div class="music-order">
<div class="music-order__loop is-loop" id="loop">
<img src="https://snowleo208.github.io/100-Days-of-Code/7.%20Music%20Player/img/loop.svg" alt="Loop music">
</div>
<div class="music-order__shuffle" id="shuffle">
<img src="https://snowleo208.github.io/100-Days-of-Code/7.%20Music%20Player/img/shuffle.svg" alt="Shuffle music">
</div>
</div>
<div class="music-control">
<div class="music-control__backward" id="backward">
<img src="https://snowleo208.github.io/100-Days-of-Code/7.%20Music%20Player/img/backward.svg" alt="Backward">
</div>
<div class="music-control__play" id="play">
<img src="https://snowleo208.github.io/100-Days-of-Code/7.%20Music%20Player/img/play.svg" alt="Play" class="play">
</div>
<div class="music-control__forward" id="forward">
<img src="https://snowleo208.github.io/100-Days-of-Code/7.%20Music%20Player/img/forward.svg" alt="Forward">
</div>
</div>
</section>
</div>
<p class="disclaimer">Music by Marcel Pequel and audio from <a href="http://freemusicarchive.org/music/Marcel_Pequel/From_One_To_Nine/" target="_blank" rel="noopener">Free Music Archive</a>. Design idea from <a href="https://dribbble.com/shots/2315906-Music-player-PSD-freebie-dailyui-Day-009" target="_blank" rel="noopener">Dima Blover</a></p>
</div>
$bg-1: #FFCDD2
$bg-2: #B2EBF2
$music-bar: #2196F3
$music-bar-bg: #efefef
%player-action
transition: 300ms
&:hover
opacity: 0.7
cursor: pointer
div
box-sizing: border-box
img
width: 100%
height: 100%
p
margin-top: 0
margin-bottom: 0.3em
body
font-family: 'Mukta', sans-serif
width: 100%
height: 100%
margin: 0
padding: 0
min-width: 100vw
min-height: 100vh
background: linear-gradient(-45deg, $bg-1 50%, $bg-2 50%)
//for all buttons
div > img
@extend %player-action
div,
section
box-sizing: border-box
.c-containter
display: flex
flex-flow: column nowrap
justify-content: center
align-items: center
width: 100%
height: 100vh
padding: 1em
.music-container
position: relative
display: flex
box-shadow: 1px 1px 5px 0 rgba(0,0,0,0.3)
max-height: 290px
.album-cover
flex: 1 0 30%
img
width: 100%
height: 100%
.arrow
position: absolute
top: calc(50% - 2.5em)
background: rgba(255,255,255,0.3)
border: 0
width: 5em
height: 5em
cursor: pointer
&:hover
background: rgba(255,255,255,0.5)
img
display: block
width: 20px
margin: 0 auto
&.left
left: -5em
&.right
right: -5em
.music-player
display: flex
flex-flow: column wrap
justify-content: center
background: white
padding: 1em
text-align: center
width: 500px
max-width: 500px
&__title
margin: 0 0 0.1em 0
&__author
margin: 0 0 0.5em 0
.music-bar
background: $music-bar-bg
stroke-width: 1
height: 8px
width: 100%
&:hover
cursor: pointer
#length
width: 0%
background: $music-bar
height: 100%
transition: width linear 200ms
.music-time
display: flex
flex-flow: row wrap
&__last
margin-left: auto
.music-order
display: flex
flex-flow: row wrap
&__shuffle,
&__loop
width: 1.2em
height: 1.2em
opacity: 0.2
margin: 0.3em 0
&.is-loop
opacity: 1 !important
&.is-loop-one
opacity: 1 !important
&__shuffle
margin-left: auto
.music-control
display: flex
flex-flow: row wrap
justify-content: center
align-items: center
height: 2em
&__play
width: 3em
height: 3em
margin: 0 1em
&__backward,
&__forward
width: 1.5em
height: 1.5em
.disclaimer
font-size: 0.9em
margin-top: 1em
text-align: center
a
color: $music-bar
@media all and (max-width: 960px)
.c-containter
display: block
overflow: auto
.music-container
flex-flow: column wrap
max-height: inherit
max-width: 270px
margin: 0 auto
overflow: auto
.music-player
width: 100%
max-width: 100%
&__title
font-size: 1.5em
&__author
font-size: 1em
.album-cover
position: relative
flex: 1 1 100%
max-width: 270px
max-height: 270px
.arrow
position: absolute
top: calc(50% - 1.5em)
width: 3em
height: 3em
&.left
left: 0
&.right
right: 0
.music-control
&__play
width: 2.2em
height: 2.2em
(function IIFE() {
const list = [
{
id: 1,
url:
"//cdn.atrera.com/audio/Marcel_Pequel_-_01_-_One.mp3",
author: "Marcel Pequel",
title: "One",
cover:
"//cdn.atrera.com/images/cover_yz2mak.jpg"
},
{
id: 2,
url:
"//cdn.atrera.com/audio/Marcel_Pequel_-_02_-_Two.mp3",
author: "Marcel Pequel",
title: "Two",
cover:
"//cdn.atrera.com/images/cover_yz2mak.jpg"
},
{
id: 3,
url:
"//cdn.atrera.com/audio/Marcel_Pequel_-_03_-_Three.mp3",
author: "Marcel Pequel",
title: "Three",
cover:
"//cdn.atrera.com/images/cover_yz2mak.jpg"
}
];
let currentId = 0;
let isPlaying = false;
let isLoop = true;
let isShuffle = false;
let currentAudio = "music1";
let timer = null;
let loopOne = false;
const currentTimeIndicator = document.querySelector(".music-time__current");
const leftTimeIndicator = document.querySelector(".music-time__last");
const progressBar = document.getElementById("length");
const playBtn = document.querySelector(".play");
const cover = document.querySelector(".cover");
const title = document.querySelector(".music-player__title");
const author = document.querySelector(".music-player__author");
const loopBtn = document.getElementById("loop");
const shuffleBtn = document.getElementById("shuffle");
const forwardBtn = document.getElementById("forward");
const backwardBtn = document.getElementById("backward");
const prevBtn = document.getElementById("prev");
const nextBtn = document.getElementById("next");
const progressDiv = document.getElementById("progress");
function play(e) {
if (!isPlaying) {
// console.log('play');
e.target.src =
"https://snowleo208.github.io/100-Days-of-Code/7.%20Music%20Player/img/pause.svg";
e.target.alt = "Pause";
isPlaying = true;
document.getElementById(currentAudio).play();
showTime();
} else {
// console.log('pause');
e.target.src =
"https://snowleo208.github.io/100-Days-of-Code/7.%20Music%20Player/img/play.svg";
e.target.alt = "Play";
document.getElementById(currentAudio).pause();
isPlaying = false;
clearInterval(timer);
}
}
function changeBar() {
const audio = document.getElementById(currentAudio);
const percentage = (audio.currentTime / audio.duration).toFixed(3);
progressBar.style.transition = "";
// console.log(audio.currentTime);
//set current time
const minute = Math.floor(audio.currentTime / 60);
const second = Math.floor(audio.currentTime % 60);
const leftTime = audio.duration - audio.currentTime;
currentTimeIndicator.innerHTML =
("0" + minute).substr(-2) + ":" + ("0" + second).substr(-2);
//set left time
const leftMinute = Math.floor(leftTime / 60);
const leftSecond = Math.floor(leftTime % 60);
leftTimeIndicator.innerHTML =
("0" + leftMinute).substr(-2) + ":" + ("0" + leftSecond).substr(-2);
//set time bar
progressBar.style.width = percentage * 100 + "%";
}
function showTime() {
timer = setInterval(() => changeBar(), 500);
}
function nextMusic(mode) {
playBtn.src =
"https://snowleo208.github.io/100-Days-of-Code/7.%20Music%20Player/img/play.svg";
playBtn.alt = "Play";
document.getElementById(currentAudio).pause();
isPlaying = false;
clearInterval(timer);
if (mode === "next") {
currentId = currentId + 1 > list.length - 1 ? 0 : currentId + 1;
init();
} else {
currentId = currentId - 1 < 0 ? list.length - 1 : currentId - 1;
init();
}
}
function shuffle(e) {
isShuffle = !isShuffle;
if (isShuffle) {
e.target.parentNode.classList.add("is-loop");
} else {
e.target.parentNode.classList.remove("is-loop");
}
}
function backward() {
const audio = document.getElementById(currentAudio);
audio.currentTime -= 5;
if (!isPlaying) {
changeBar();
}
}
function forward() {
const audio = document.getElementById(currentAudio);
audio.currentTime += 5;
if (!isPlaying) {
changeBar();
}
}
function stopMusic() {
playBtn.src =
"https://snowleo208.github.io/100-Days-of-Code/7.%20Music%20Player/img/play.svg";
playBtn.alt = "Play";
isPlaying = false;
}
function goToNextMusic() {
let newId = currentId;
while (isShuffle && !loopOne && newId === currentId) {
newId = Math.floor(Math.random() * Math.floor(list.length - 1));
}
if (!isShuffle && !loopOne) {
currentId = currentId + 1 > list.length - 1 ? 0 : currentId + 1;
}
if (!isShuffle && loopOne) {
currentId = currentId;
}
if (isShuffle) {
currentId = newId;
}
init();
document.getElementById(currentAudio).play();
}
function loop(e) {
const audio = document.getElementById(currentAudio);
if (!isLoop && !loopOne) {
isLoop = true;
loopOne = false;
// console.log('is loop');
e.target.parentNode.classList.add("is-loop");
e.target.src =
"https://snowleo208.github.io/100-Days-of-Code/7.%20Music%20Player/img/loop.svg";
audio.loop = false;
audio.onended = e => goToNextMusic();
console.log(isLoop, loopOne);
} else if (isLoop && !loopOne) {
// console.log('is loop one');
isLoop = true;
loopOne = true;
e.target.parentNode.classList.add("is-loop");
e.target.src =
"https://snowleo208.github.io/100-Days-of-Code/7.%20Music%20Player/img/loopone.svg";
audio.loop = true;
audio.onended = e => goToNextMusic();
console.log(isLoop, loopOne);
} else {
// console.log('not loop');
isLoop = false;
loopOne = false;
e.target.parentNode.classList.remove("is-loop");
e.target.src =
"https://snowleo208.github.io/100-Days-of-Code/7.%20Music%20Player/img/loop.svg";
audio.loop = false;
audio.onended = e => stopMusic();
console.log(isLoop, loopOne);
}
}
function progress(e) {
const audio = document.getElementById(currentAudio);
//get current position and minus progress bar's x position to get current position in progress bar
const pos =
(e.pageX - progressDiv.getClientRects()[0].x) /
progressDiv.getClientRects()[0].width;
audio.currentTime = pos * audio.duration;
changeBar();
}
function init() {
//reset music duration and setup audio
const audio =
document.getElementById(currentAudio) === null
? new Audio()
: document.getElementById(currentAudio);
audio.src = list[currentId].url;
audio.id = currentAudio;
document.getElementById(currentAudio) === null
? document.body.appendChild(audio)
: "";
progressBar.style.transition = "none";
progressBar.style.width = "0%";
document.getElementById(currentAudio).currentTime = 0;
title.innerHTML = list[currentId].title;
author.innerHTML = list[currentId].author;
cover.src = list[currentId].cover;
//set current time
audio.addEventListener("loadedmetadata", function() {
const leftMinute = Math.floor(audio.duration / 60);
const leftSecond = Math.floor(audio.duration % 60);
currentTimeIndicator.innerHTML = "00:00";
leftTimeIndicator.innerHTML =
("0" + leftMinute).substr(-2) + ":" + ("0" + leftSecond).substr(-2);
progressBar.style.transition = "";
});
//set loop
document.getElementById(currentAudio).onended = e => goToNextMusic(e);
}
playBtn.addEventListener("click", play);
loopBtn.addEventListener("click", loop);
shuffleBtn.addEventListener("click", shuffle);
forwardBtn.addEventListener("click", forward);
backwardBtn.addEventListener("click", backward);
prevBtn.addEventListener("click", e => nextMusic("prev"));
nextBtn.addEventListener("click", e => nextMusic("next"));
progressDiv.addEventListener("click", e => {
progress(e);
});
init();
})();
Also see: Tab Triggers