我将如何在 jquery 中为 html5 视频制作一个 playTo() 函数
How would I make a playTo() function in jquery for html5 video
问题:如何构造我的 playTo() 函数来获取一个值并播放该值作为视频的时间戳? 视频将播放,直到点击该时间戳;之后视频将暂停。
.HTML:
<video src="assets/trailer.mp4" class="video" controls muted>
Your browser does not support the <code>video</code> element.
</video>
<button class="next-chapter">next chapter</button>
.JS:
var vid = $('.video')[0];
var play = $('.next-chapter');
//this controlls which index of timestamp
var sceneCounter = 0;
//these are the timestampes
var timestamps = [10,20,30,40];
//this function will play the video normally
var playVideo = function() {
vid.play();
};
//heres what I plan to set the next timestamp
var playTo = function(t){
//help here!!
console.log('heyhey');
};
var nextScene = function() {
sceneCounter++
};
$( play ).click(function() {
if(sceneCounter !== 0){
nextScene();
}
playTo(sceneCounter);
});
vid.addEventListener('timeupdate',function(event){
time = vid.currentTime;
console.log(time);
},false);
您所需要的只是一个.play()
方法和.pause()
方法。
var vid = $('.video')[0];
var play = $('.next-chapter');
//this controlls which index of timestamp
var sceneCounter = -1;
//these are the timestampes
var timestamps = [2,4,6,8];
//this function will play the video normally
var playVideo = function() {
vid.play();
};
//heres what I plan to set the next timestamp
var playTo = function(t){
//help here!!
vid.play();
};
var nextScene = function() {
sceneCounter++;
};
$(play).click(function() {
if(sceneCounter != timestamps.length-1){
nextScene();
playTo(sceneCounter);
}
});
vid.addEventListener('timeupdate',function(event){
if(vid.currentTime >= timestamps[sceneCounter]){
vid.pause();
}
},false);
html {padding: 20px 0; background-color: #efefef;}
body {width: 400px; padding: 40px; margin: 0 auto; background: #fff; box-shadow: 1px 1px 5px rgba(0,0,0,0.5);}
video {
width: 400px;
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<video src="http://grochtdreis.de/fuer-jsfiddle/video/sintel_trailer-480.mp4" class="video" controls muted>
Your browser does not support the <code>video</code> element.
</video>
<button class="next-chapter">next chapter</button>
<!--
<source id="mp4" src="" type="video/mp4">
-->
希望这有帮助。
相关文章:
- 我可以在json对象中添加一个函数吗
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 在另一个函数中使用变量this
- 在另一个函数成功结束后调用该函数
- mongoose.connect undefined不是一个函数
- 监听器必须是一个函数
- 使用JS函数来使用另一个函数的语法?node.js
- 如何取消object.prototypes javascript的一个函数
- 从Chrome扩展名中的popup.html文件在background.js文件中运行一个函数
- 嵌套到另一个函数中的Fancybox函数;不起作用
- 如何在javascript中使用不止一个函数
- jQuery-在页面加载时执行一个函数
- jquery UI draggable:UI.children不是一个函数
- jQuery Mobile Undefined不是一个函数
- 如何在php中创建一个函数,该函数与文本区域一起工作,通过输入类似[color:red]的内容来打印具有等效颜色的文本
- javascript函数将数据添加到屏幕,但随后被另一个函数覆盖
- JS异常:animate不是一个函数
- 如何将一个函数附加到一个不存在的元素上
- JavaScript/jQuery-添加添加和删除类与下一个函数之间的延迟