How to Control YouTube’s Video Player with JavaScript
Progress Bar
This is done using the
player.seekTo(sec)
function, which jumps the video to the seconds provided in the parameter.To demonstrate this we’ve made our own version of YouTube’s progress bar, using an input field of type range. When we click anywhere on it, we take the inputs value, witch gives us a percentage. We then use this percentage to calculate what progress we want made to the video and skip to the according seconds.
$('#progress-bar').on('mouseup touchend', function (e) { // Calculate the new time for the video. // new time in seconds = total duration in seconds * ( value of range input / 100 ) var newTime = player.getDuration() * (e.target.value / 100); // Skip video to new time. player.seekTo(newTime); });