javascript youtube like slider control

前端 未结 2 834
一生所求
一生所求 2021-01-13 07:21

I\'ve a question about implementing a slider control in a browser.

I need to playback data over time, in a browser. I will have one Worker filling the playback buffe

2条回答
  •  礼貌的吻别
    2021-01-13 07:50

    You could just modify the jQuery UI slider a bit by using your own background image, then adjusting the width to show the load progress (demo):

    $(function(){
    
        var ytplayer = $('#player')[0],
            // # seconds from YouTube API
            duration = ytplayer.getDuration(),
            // # bytes
            totalBytes = ytplayer.getVideoBytesTotal(),
            // start # bytes - may not be necessary
            startBytes = ytplayer.getVideoStartBytes();
    
        $("#slider").slider({
            range: "max",
            min: startBytes,
            max: duration,
            value: 1
        })
        // image: http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/blitzer/images/ui-bg_highlight-soft_15_cc0000_1x100.png
        // with a 30% opacity
        .css('background-image', 'url(http://i56.tinypic.com/fbjad2.png)');
    
        // Loop to update slider   
        function loop() {
            var time = ytplayer.getCurrentTime(),
                // make loaded a percentage
                loaded = 100 - (ytplayer.getVideoBytesLoaded() / totalBytes) * 100;
    
            // set limit - no one likes negative widths
            if (loaded < 0) { loaded = 0; }
    
            // update time on slider
            $('#slider')
                .slider('option', 'value', time)
                .find('.ui-widget-header').css('width', loaded + '%');
    
            // repeat loop as needed
            if (loaded < 0 || time < duration) {
                setTimeout(loop, 500);
            }
        }
        loop(); 
    });
    

提交回复
热议问题