admin管理员组文章数量:1426907
I've been developing a custom player with Youtube JavaScript API V3. And this is part of my code:
In index.html
I've:
<div class="player_music-progressBar" id="player_music-progressBar"><div></div></div>
styles.css
:
.player_music-progressBar {
position: relative;
float: left;
width: 100%;
height: 6px;
background-color: #444444;
margin-top: 1px;
cursor: pointer;
}
.player_music-progressBar div {
height: 100%;
width: 0px;
background-color: #ccc;
}
And, in my JS
this is the function to do the progress bar animation:
function convert_time(seconds) {
var s = seconds,
h = Math.floor(s/3600);
s -= h*3600;
var m = Math.floor(s/60);
s -= m*60;
if(seconds >= "3600") {
return "0" + h + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
} else {
return (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
}
}
function progressBar(percent, element) {
var progressBar_width = percent * element.width() / 100;
element.find("div").animate({width: progressBar_width });
}
function onPlayerStateChange(event) {
if(event.data == YT.PlayerState.PLAYING) {
var playerTotalTime = player.getDuration();
playing = setInterval(function() {
var playerCurrentTime = player.getCurrentTime(),
playerDifferenceTime = (playerCurrentTime / playerTotalTime) * 100;
progressBar(playerDifferenceTime, $("#player_music-progressBar"));
}, 1000);
} else if(event.data == YT.PlayerState.PAUSED) {
clearInterval(playing);
}
}
And I want to when progress bar is clicked, get the seconds by local of progress bar clicked, so, i try:
$("#player_music-progressBar").click(function(e) {
//player.seekTo(e.pageX - $("#player_music-progressBar").offset().left);
// to get part of width of progress bar clicked
var widthclicked = e.pageX - $(this).offset().left;
// do calculation of the seconds clicked
var calc = (widthclicked / player.getDuration()) * 100;
var calc_fix = calc.toFixed(0);
var time = convert_time(calc_fix);
console.log(time + " - " + widthclicked);
});
But the output of time
is wrong (because he is relative to the progress bar width, that have fixed width), and I no have ideia of how to do this calculation... How to do it?
I've been developing a custom player with Youtube JavaScript API V3. And this is part of my code:
In index.html
I've:
<div class="player_music-progressBar" id="player_music-progressBar"><div></div></div>
styles.css
:
.player_music-progressBar {
position: relative;
float: left;
width: 100%;
height: 6px;
background-color: #444444;
margin-top: 1px;
cursor: pointer;
}
.player_music-progressBar div {
height: 100%;
width: 0px;
background-color: #ccc;
}
And, in my JS
this is the function to do the progress bar animation:
function convert_time(seconds) {
var s = seconds,
h = Math.floor(s/3600);
s -= h*3600;
var m = Math.floor(s/60);
s -= m*60;
if(seconds >= "3600") {
return "0" + h + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
} else {
return (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
}
}
function progressBar(percent, element) {
var progressBar_width = percent * element.width() / 100;
element.find("div").animate({width: progressBar_width });
}
function onPlayerStateChange(event) {
if(event.data == YT.PlayerState.PLAYING) {
var playerTotalTime = player.getDuration();
playing = setInterval(function() {
var playerCurrentTime = player.getCurrentTime(),
playerDifferenceTime = (playerCurrentTime / playerTotalTime) * 100;
progressBar(playerDifferenceTime, $("#player_music-progressBar"));
}, 1000);
} else if(event.data == YT.PlayerState.PAUSED) {
clearInterval(playing);
}
}
And I want to when progress bar is clicked, get the seconds by local of progress bar clicked, so, i try:
$("#player_music-progressBar").click(function(e) {
//player.seekTo(e.pageX - $("#player_music-progressBar").offset().left);
// to get part of width of progress bar clicked
var widthclicked = e.pageX - $(this).offset().left;
// do calculation of the seconds clicked
var calc = (widthclicked / player.getDuration()) * 100;
var calc_fix = calc.toFixed(0);
var time = convert_time(calc_fix);
console.log(time + " - " + widthclicked);
});
But the output of time
is wrong (because he is relative to the progress bar width, that have fixed width), and I no have ideia of how to do this calculation... How to do it?
1 Answer
Reset to default 6 +50Total bar width is the 100%, and the width clicked is a percentage of that. To get the proportionate requested time do clickedWidth / totalWidth * player.getDuration()
:
$("#player_music-progressBar").click(function(e) {
var $this = $(this);
// to get part of width of progress bar clicked
var widthclicked = e.pageX - $this.offset().left;
var totalWidth = $this.width(); // can also be cached somewhere in the app if it doesn't change
// do calculation of the seconds clicked
var calc = (widthclicked / totalWidth * player.getDuration()); // get the percent of bar clicked and multiply in by the duration
var calc_fix = calc.toFixed(0);
var time = convert_time(calc_fix);
console.log(time + " - " + widthclicked);
});
本文标签: javascriptGet width of part of progress bar clicked and do calculation of secondsStack Overflow
版权声明:本文标题:javascript - Get width of part of progress bar clicked and do calculation of seconds - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745488337a2660486.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论