This error occurs when trying to set currentTime before the browser recognizes the duration video. Duration - part of the "metadata", which is usually located in the header of the video file and includes the height and width.
Usually, if you have a video element without the preload attribute, the browser will try to at least load the metadata shortly after the page loads. But, depending on the specifics of the browser, the rest of the content on the page and the speed of the network connection may not happen while you scroll at least once.
The solution looks something like this.
var video = $('#trees').get(0), $window = $(window), $document = $(document); function updateVideo() { var duration = video.duration, scrollPosition = window.scrollY; if (duration) { video.currentTime = (scrollPosition / ($document.height() - $window.height())) * duration; } }
This should make the error go away. If you try this and the loadedmetadata event never fires, try adding it to the end to force it:
video.load();
Edit: Defined and set scrollPosition . Here is a working example: http://jsbin.com/vurap/1/edit
brianchirls
source share