This project is a simple Spotify clone built using HTML, CSS, and JavaScript. It allows users to play songs from different albums, control playback, and manage volume. The project demonstrates basic web development skills, including handling events, dynamically updating the DOM, and using the Fetch API to load resources.
The website is currently hosted on Vercel. You can view it here.
In the future, the project will be hosted on Freehostmost after making some additional fixes and improvements.
The project is structured as follows:
├── index.html
├── styles.css
├── script.js
├── songs/
│ ├── Album1/
│ │ ├── song1.mp3
│ │ ├── song2.mp3
│ │ └── ...
│ └── Album2/
│ ├── song1.mp3
│ ├── song2.mp3
│ └── ...
└── svg/
├── play.svg
├── pause.svg
├── previous.svg
├── next.svg
└── ...
The HTML file contains the basic structure of the Spotify clone, including the album cards, playlist, playback controls, and the seek bar.
The CSS file is responsible for the styling of the project. It includes styles for the album cards, playlist, playback controls, and the seek bar.
The JavaScript file contains the core logic of the project:
Here is a snippet of the JavaScript code used to load songs and handle playback:
document.addEventListener('DOMContentLoaded', () => {
// Fetch and display songs from an album when a card is clicked
const cards = document.querySelectorAll('.card');
cards.forEach(card => {
card.addEventListener('click', async () => {
const album = card.getAttribute('data-album');
const songUrls = await fetchSongs(album);
displaySongs(songUrls);
});
});
// Play/pause button event listener
document.getElementById('play').addEventListener('click', togglePlayPause);
// Previous and next button event listeners
document.getElementById('previous').addEventListener('click', playPreviousSong);
document.getElementById('next').addEventListener('click', playNextSong);
});
// Fetch songs from a specific album
async function fetchSongs(album) {
const response = await fetch(`songs/${album}/`);
const text = await response.text();
const parser = new DOMParser();
const doc = parser.parseFromString(text, 'text/html');
const links = doc.querySelectorAll('a');
const songUrls = Array.from(links)
.map(link => link.href)
.filter(href => href.endsWith('.mp3'));
return songUrls;
}
// Display songs in the playlist
function displaySongs(songUrls) {
const songList = document.querySelector('.songlist');
songList.innerHTML = '';
songUrls.forEach(songUrl => {
const songName = decodeURIComponent(songUrl.split('/').pop().replace('.mp3', ''));
const li = document.createElement('li');
li.innerHTML = `
<div style="display: flex; gap: 12px;">
<img class="invert" style="width: 20px;" src="https://github.com/EpicNesh26/Spotify-Clone/raw/main/svg/music.svg" alt="">
<div class="info">
<div>${songName}</div>
<div>Nesh</div>
</div>
<div class="playnow">
<img class="invert play-song" src="https://github.com/EpicNesh26/Spotify-Clone/raw/main/svg/play.svg" data-url="${songUrl}" alt="">
</div>
</div>
`;
songList.appendChild(li);
});
}
Contributions are welcome! If you would like to contribute to this project, please follow these steps:
git clone https://github.com/EpicNesh26/spotify-clone.git
git add -A
git commit --m "Your Commit Message"
git push origin feature-name
This project is licensed under the MIT License. Feel free to use and modify it as you like.