我想創建一個自定義的youtube progressbar。我做了一些事情,但它沒有按照應有的方式工作。我希望它像YouTube的進度條一樣平穩運行,不是像我一樣每秒更新一次,而是從0到100%,現在我的98%停止。另外,我希望前進行欄在視頻暫停時停止,並在再次播放視頻時再次運行。Youtube使用YouTube iframe api和jquery不工作的自定義progressbar
進度
function progress(percent, $element) {
var progressBarWidth = percent * $element.width()/100;
// $element.find('div').animate({ width: progressBarWidth }, 500).html(percent + "% ");
$element.find('div').animate({ width: progressBarWidth });
}
進度CSS
#progressBar {
width: 960px;
height: 6px;
background-color: #444444;
display: none;
margin-top: 1px;
}
#progressBar div {
height: 100%;
width: 0;
background-color: #ffffff;
}
YouTube的iframe中API
// Load the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// Replace the 'ytplayer' element with an <iframe> and
// YouTube player after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('ytplayer', {
height: '540',
width: '960',
videoId: 'UDxzMcCrOyI',
playerVars: { 'showinfo': 0, 'modestbranding': 1, 'rel': 0, 'iv_load_policy': 3 }
});
}
當我按下一個按鈕,視頻被顯示,並且還進度條。視頻正在自動播放。
$('#ytplayer').show(0, function() {
player.playVideo();
$('#progressBar').show();
var playerTotalTime = player.getDuration();
mytimer = setInterval(function() {
var playerCurrentTime = Math.round(player.getCurrentTime());
var playerTimeDifference = (playerCurrentTime/playerTotalTime) * 100;
var playerTimePercent = Math.round(playerTimeDifference);
console.log(playerTimePercent);
progress(playerTimePercent, $('#progressBar'));
}, 1000);
});
你想,或者你自己嘗試過的東西嗎? – mpgn 2014-08-31 14:57:49
我試過了,我在自己的代碼上面做了 – sorinu26 2014-08-31 14:59:14
我在你之前的帖子中回答了你,2天前:http://stackoverflow.com/a/25585727/2274530 – mpgn 2014-08-31 14:59:58