JavaScript再次开始视频
我有通过滚动鼠标轮滚动的视频。一切都很好,但是我想检查视频是否完成并跳回开始。
我已经尝试了一个IF语句将所有变量设置为0,但它不起作用。 到目前为止,这是我的代码,与不工作的IF语句:
const action = document.querySelector(".action");
const video = action.querySelector("video");
//Scroll Magic scenes
const controller = new ScrollMagic.Controller();
let scene = new ScrollMagic.Scene({
duration: 200000, //64000
triggerElement: action,
triggerHook: 0
})
// .addIndicators()
.setPin(action)
.addTo(controller);
//Scroll Magic video animation
let accelAmount = 0.1;
let scrollpos = 0;
let currentTime = video.currentTime;
let lastcurrentTime;
let delay = 0;
scene.on("update", e => {
scrollpos = e.scrollPos / 3000; //the higher the number, the slower
});
//Move
setInterval(() => {
delay += (scrollpos - delay) * accelAmount;
video.currentTime = delay;
console.log(video.currentTime + " reading");
lastcurrentTime = video.currentTime;
}, 33.3);
//function to check if is still scrolling
function scrollTracker(delay, callback) {
let timeout = null;
return function(...args) {
if (timeout !== null) {
clearTimeout(timeout);
timeout = null;
}
timeout = setTimeout(callback, delay, ...args)
};
}
//check if is still scrolling after x seconds
const tracker = scrollTracker(5000, () => {
console.log('User stopped scrolling.');
window.scrollTo(0, 0);
});
window.addEventListener('scroll', tracker);
//scroll back to beginning when movie has finished -- not working
if (video.currentTime >= 100.0){
window.scrollTo(0, 0);
scrollpos = 0;
currentTime = 0;
delay = 0;
}
感谢您的任何帮助!
PS是否可以通过使用“ window.scrollto”来回到视频的开头?
Video.CurrentTime = 0;在这里不工作。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
循环
属性添加到视频元素,它将自动为我们循环。如果不起作用,则仅在代码运行时才检查一次。您需要end
事件侦听器才能检查。Add a
loop
attribute to the video element and it will loop for us automatically. The if doesnt work beacause it only checks once when the code is ran. You need theend
event listener to check.https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/ended_event
https://www.w3schools.com/tags/att_video_loop.asp