作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在嵌入一个带有特殊调整的 Youtube 播放列表——起点应该是随机选择的——通过以下两个片段:
HTML:
<iframe id="juliacon-player"
align="center"
width="560"
height="315"
frameborder="0"
allowfullscreen>
</iframe>
<body>
的底部:
<script type="text/javascript">
var playlistId = 'PLP8iPy9hna6Sdx4soiGrSefrmOPdUWixM',
videoCount = 61,
index = Math.floor(Math.random() * videoCount),
playlistUrl = 'https://www.youtube.com/embed/videoseries?list=' + playlistId + '&index=' + index,
videoPlayer = document.getElementById('juliacon-player');
if (videoPlayer) {
videoPlayer.src = playlistUrl;
}
</script>
最佳答案
我认为这与 youtube 根据视口(viewport)大小选择缩略图有关。如果 iframe 的宽度为 430 像素或更小,我认为您会发现缩略图将正确显示。当它高于此值时,由于某种原因,它只会显示播放列表的第一个缩略图。
iframe(width="280" height="158" src="https://www.youtube.com/embed/videoseries?list=PLaQokWZfgbykfIr6NKIcOMxsUC0cltOwE&index=2" frameborder="0" allowfullscreen)
iframe(width="720" height="405" src="https://www.youtube.com/embed/videoseries?list=PLaQokWZfgbykfIr6NKIcOMxsUC0cltOwE&index=2" frameborder="0" allowfullscreen)
iframe(width="720" height="405" src="https://www.youtube.com/embed/K-mG66pwQ5M?list=PLaQokWZfgbykfIr6NKIcOMxsUC0cltOwE")
$(function() {
// GET A RANDOM VIDEO FROM ALL PLAYLISTS
function randomVideo() {
// VARIABLES
// the playlist
var playlistDon = {url:"PLaQokWZfgbykfIr6NKIcOMxsUC0cltOwE", count:4, name:"Don's Design Lab"};
// get random video from that playlist
var indexRando = Math.floor((Math.random() * playlistDon.count));
// making the array of videos in playlist
var sum = 0;
var videoArray = [];
// CONNECTING TO API
function getVids(PageToken){
$.get(
"https://www.googleapis.com/youtube/v3/playlistItems",{
part : 'snippet',
maxResults : 50,
playlistId : playlistDon.url, // selecting the random playlist
pageToken : PageToken,
key: 'YOUR API KEY'
},
function(data){
myPlan(data);
}
);
}
// GETTING LIST OF VIDEOiDS FROM PLAYLIST
function myPlan(data){
var total = data.pageInfo.totalResults;
var nextPageToken = data.nextPageToken;
// cycle through the data
for(var i=0;i<data.items.length;i++){
// add .items to videoArray
videoArray.push(data.items[i].snippet.resourceId.videoId);
sum++ ;
if(sum === (total) ){ // if the current item number equals the total number of items
sum = 0; // reset the count
updateIframe(); // update the iframe
return;
}
}
if(sum < (total)){
getVids(nextPageToken);
}
}
function updateIframe() {
// put that video into the iframe
var videoUrl = "https://www.youtube.com/embed/" + videoArray[indexRando] +"?list=" + playlistDon.url + "&index=" + indexRando + "&showinfo=1";
$('.video.random iframe').attr("src", videoUrl);
}
getVids();
}
$('button').on('click', function() {
randomVideo();
});
});
关于javascript - 嵌入带有索引的 youtube 播放列表;缩略图始终是第一个视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32451701/
我有一个具有可变数量子元素的固定大小的 div。我不知道 children 的大小。目标是缩小它们以适合父级。 例子: .parent { width: 100px; height: 100p
我是一名优秀的程序员,十分优秀!