最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Adding Autoplay To Video Element With Javascript - Stack Overflow

programmeradmin2浏览0评论

I want to have my video automatically play and loop for me when the user scrolls to a specific area on the video element. I'm aware that I can use a JS observer to do this: however, how do I use JavaScript to add and remove 'autoplay'?

What I want my default code to be:

<video loop muted>
     <source src="/videos/my_video.mp4" type="video/mp4" >
</video>

What I want to happen when the user triggers the JS observer:

<video loop muted autoplay>
     <source src="/videos/my_video.mp4" type="video/mp4" >
</video>

I want to have my video automatically play and loop for me when the user scrolls to a specific area on the video element. I'm aware that I can use a JS observer to do this: however, how do I use JavaScript to add and remove 'autoplay'?

What I want my default code to be:

<video loop muted>
     <source src="/videos/my_video.mp4" type="video/mp4" >
</video>

What I want to happen when the user triggers the JS observer:

<video loop muted autoplay>
     <source src="/videos/my_video.mp4" type="video/mp4" >
</video>
Share Improve this question asked Sep 5, 2022 at 20:42 Larry-LarrieeLarry-Larriee 471 silver badge9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5
 document.querySelector('video').autoplay = true;
document.querySelector('video').play();

or also

document.querySelector('video').pause();
发布评论

评论列表(0)

  1. 暂无评论