Skip to content

Instantly share code, notes, and snippets.

@zwaper
Last active April 22, 2021 11:01
Show Gist options
  • Select an option

  • Save zwaper/21eb3f10376b811dd2a879b63c12e35d to your computer and use it in GitHub Desktop.

Select an option

Save zwaper/21eb3f10376b811dd2a879b63c12e35d to your computer and use it in GitHub Desktop.
YouTube playlist
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>CodePen - YouTube playlist</title>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css'><link rel="stylesheet" href="./style.css">
</head>
<body>
<!-- partial:index.partial.html -->
<div class="container">
<header>
<a href="https://www.youtube.com/channel/UCxovSRa8yigaVWIB67wvgOw/" title="Weow Music" target="_blank" rel="nofollow">
<img class="youtube-img" src="https://yt3.ggpht.com/ytc/AAUvwnjbZZoI7KimMX8bB3H_86_5S6Vjd2AzCsyKfhnZVA=s88-c-k-c0x00ffffff-no-rj"></a>
<h4 class="logo">WEOW MUSIC</h4>
</header>
<section id="video">
</section>
</div>
<div class="list">
<div id="list">
<div class="list-inner">
</div>
</div>
</div>
<!-- partial -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js'></script><script src="./script.js"></script>
</body>
</html>
$(document).ready(function() {
var key = "AIzaSyB7mPs042i3i0IF0k5f3H1OimLbvBHeYk0";
var playlistId = "PLkSaehvtoPcxdDhbdig_EYYVl5ONrhAqt";
var URL = "https://www.googleapis.com/youtube/v3/playlistItems";
var options = {
part: "snippet",
key: key,
maxResults: 20,
playlistId: playlistId
};
loadVids();
function loadVids() {
$.getJSON(URL, options, function(data) {
var id = data.items[0].snippet.resourceId.videoId;
console.log(data);
mainVid(id);
resultsLoop(data);
});
}
function mainVid(id) {
$("#video").html(`
<iframe width="100%" height="315" src="https://www.youtube.com/embed/${id}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
`);
}
function resultsLoop(data) {
$.each(data.items, function (i, item) {
var thumb = item.snippet.thumbnails.high.url;
var title = item.snippet.title;
var desc = item.snippet.description.substring(0, 100);
var vid = item.snippet.resourceId.videoId;
$('.list-inner').append(`
<article class="item" data-key="${vid}">
<img src="${thumb}" alt="" class="thumb">
<div class="details">
<div class="title">${title}</div>
<div class="description">${desc}...</div>
</div>
</article>
`);
});
}
$('.list-inner').on('click', 'article', function () {
var id = $(this).attr('data-key');
mainVid(id);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
@mixin font-base {
padding: 0;
margin: 0;
line-height: 1.3;
font-weight: 600;
}
h4 {
@include font-base;
}
body {
background-color: #eee;
}
.container {
background: rgb(51, 51, 51);
width: 600px;
align-items: center;
justify-content: center;
header {
padding: 20px 0;
background: rgb(51, 51, 51);
.logo {
text-align: center;
}
.youtube-img {
border-radius: 50%;
display: block;
margin-left: auto;
margin-right: auto;
width: 15%;
}
}
#video {
padding-bottom: 10px;
}
}
.list{
background: rgb(51, 51, 51);
width: 600px;
margin: 0 auto;
max-height: 300px;
overflow-y: scroll;
overflow-x: hidden;
max-width: 100%;
}
article {
display: flex;
align-items: center;
padding: 0;
margin: 10px auto;
&:hover {
cursor: pointer;
background: #ccc;
}
}
.thumb {
height: 100px;
width: auto;
}
.title {
color: #fff;
font-weight: 700;
}
.description {
color: #666;
}
.details {
padding: 8px 22px;
font-size: 12px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment