We've used the collection list for the demonstration on the home page of this project, ideally you should use the CMS Collection page template.
1) Set up the CMS First, we need 2 CMS fields.. A rich text field and a text field .
Rich text field : This will be used to copy in our timestamps from the YouTube videos description. Each timestamp number (e.g 00:29:29) needs to be set as a link in the CMS but the link address does not matter where it points as the custom code we add later will change the link. We add each timestamp as a bulletpoint for nice formatting. The Text Field: This will be used for the YouTube Video ID Your field should look like this This is how you find your video ID 2) Set up the Video Player Add a div to the page with the id ‘iframe-wrapper ’.
Inside this div, add a code embed with the code below:
COPY
<div style="position: relative; padding-bottom: 56.25%; /* 16:9 aspect ratio */">
<iframe style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;" src="https://www.youtube-nocookie.com/embed/" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen=""></iframe>
</div>
Make sure to add the Video ID Field to the Youtube URL like this:
3) Add Timestamps to the page Go to the CMS Collection Page & Add a div with the id ‘timestamps-wrapper ’
Add a rich text element into that div and connect with the CMS field we created earlier.
4) Add the Code Add the Javascript code below into the before </body> tag field:
COPY
<script>
//Function to convert timestamps into Seconds
function timeStringToSeconds(timeString) {
const timeComponents = timeString.split(':').map(Number);
let totalSeconds = 0;
if (timeComponents.length === 1) {
totalSeconds = timeComponents[0] * 60;
} else if (timeComponents.length === 2) {
const [minutes, seconds] = timeComponents;
totalSeconds = minutes * 60 + seconds;
} else if (timeComponents.length === 3) {
const [hours, minutes, seconds] = timeComponents;
totalSeconds = hours * 3600 + minutes * 60 + seconds;
}
return totalSeconds;
}
const timestampsWrapper = document.querySelector('#timestamps-wrapper');
const iframeWrapper = document.querySelector('#iframe-wrapper').childNodes[0];
const innerDiv = iframeWrapper.childNodes[0];
const iframe = innerDiv.childNodes[1];
let iframeSRC = iframe.src;
let timestamp;
const linkElements = timestampsWrapper.querySelectorAll('a');
// To remove the default links from the timestamps
linkElements.forEach((item)=>{
item.href="#";
});
timestampsWrapper.addEventListener('click', (e)=>{
if(e.target.tagName === 'A'){
const item = e.target;
timestamp = timeStringToSeconds(item.textContent);
let urlEnd = `?start=${timestamp}&autoplay=1&mute=1&cc_load_policy=1`;
iframe.src = iframeSRC+urlEnd;
// Add your video container ID below (Smooth Scroll Into View)
document.getElementById("vid").scrollIntoView({behavior: 'smooth'});
}
});
</script>
Set your video container id to 'vid' to get that smooth scroll-into-view effect, each time we click on a timestamp.
5) Add the CMS content First go to YouTube and find the video ID from the URL. Paste this into the Video ID field
This is how you find your video ID Now, go to the video description and copy the timestamps for the video. This will generate the links we use to make your video jump to each chapter.
Note: the URL's YouTube generates do not matter as our code will adjust them, the important thing is that we have the correct chapter times as links. Paste those timestamps into your CMS field and format as bullet points.
6) Publish your project That's everything, now you can add videos to the CMS with interactive timestamps.