你好,我的JS代码里进度条拖拽鼠标松开后不能进度条不能固定
(function($){
var video=$('#myVideo');
var timeFormat=function(seconds){
var minite=Math.floor(seconds/60);
if(minite<10){
minite='0'+minite;
}
var second=Math.floor(seconds%60);
if(second<10){
second='0'+second;
}
return minite +":"+second;
}
//实现鼠标移入效果
var showTitleAndControl=function(shouldShow){
if(shouldShow){
$('.control').stop().animate({'bottom':0},500);
$('.caption').stop().animate({'top':0},500);
}else{
$('.control').stop().animate({'bottom':-50},500);
$('.caption').stop().animate({'top':-50},500);
}
}
//实现播放暂停
var playAndPause=function(){
if(video[0].paused||video[0].ended){
video[0].play();
$("#playBtn").removeClass('play').addClass('pause');
}else{
video[0].pause();
$("#playBtn").removeClass('pause').addClass('play');
}
}
//实现进度条拖拽
var enableProgressDrag=function(){
var progressDrag=false;
$('.progress').on('mousedown',function(e){
progressDrag =true;
//如果光标处于按钮上,那么offsetX是相对于按钮来说的
// updateProgress(e.offsetX);
//pageX相当与整个页面定位,相当于一个绝对的坐标
updateProgress(e.pageX);
});
$(document).on('mouseup',function(e){
if(progressDrag){
progressDrag =false;
updateProgress(e.pageX);
}
});
$(document).on('mousemove',function(e){
if(progressDrag){
updateProgress(e.pageX);
}
});
}
var updateProgress=function(x){
var progress=$('.progress');
var percent=100 *( x -progress.offset().left) / progress.width();
//保证百分比在0-100之间↓↓
if(percent>100){
percent=100;
}if(percent<0){
percent=0;
}
$('.timeBar').css('width',percent+'%');
video[0].currentTime=video[0].duration*percent/100;
}
video.on("loadedmetadata",function(){
video.width($(".vContainer").width());
video.height($(".vContainer").height());
showTitleAndControl(false);//一开始就是移除的状态
//计算总时长
$('#currentTime').html(timeFormat(0));
$('#duration').html(timeFormat(video[0].duration));
$('.vContainer').hover(function(){
showTitleAndControl(true);
},function(){
showTitleAndControl(false);
})
$('#playBtn').on('click',playAndPause);
enableProgressDrag();
});
//实现进度条跟随
video.on('timeupdate',function(){
var currentTime=video[0].currentTime;
var duration=video[0].duration;
var percent=100*currentTime/duration;
//进度条随时间跳跃
$('.timeBar').css('width',percent+"%");
//当前时间跳跃
$('#currentTime').html(timeFormat(currentTime));
})
video.on('canplay',function(){
$('.loading').fadeOut(100);
})
})(jQuery);
正在回答 回答被采纳积分+1
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="./播放器.css">
</head>
<body>
<div class="vContainer">
<video id="myVideo">
<!--<source src="../新标签/1.mp4">-->
<source src="http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"/>
<!-- "此处应该多加几种格式的视频文件" -->
您的浏览器不支持video标签
</video>
<div class="caption">视频播放器</div>
<div class="control">
<div class="top">
<div class="progress">
<span class="timeBar"></span>
</div>
<div class="time">
<span id="currentTime">0:00</span>
<span>/</span>
<span id="duration">0:00</span>
</div>
</div>
<div class=" "></div>
<div class="bottom">
<div class="button play" id="playBtn">起停</div>
<div class="button stop" id="stopBtn">停止</div>
<div class="button text selected" id="speed1Btn">x1</div>
<div class="button" id="speed3Btn">x3</div>
<div class="volume">
<span class="volumeBar"></span>
</div>
<div class="button sound" id="soundBtn">音量</div>
</div>
</div>
<div class="loading">loading</div>
</div>
<script type="text/javascript" src="jquery-2.1.0.js" ></script>
<script type="text/javascript" src="播放器.js"></script>
</body>
</html>
- 参与学习 人
- 提交作业 2198 份
- 解答问题 5012 个
如果你有web端基础,既想进阶,又想进军移动端开发,那就来吧,我们专题为你带来的课程有HTML5、CSS3、移动基础、响应式、bootstrap、less等,让你在前端道路上畅通无阻!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星