admin管理员组

文章数量:1393114

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();

本文标签: htmlAdding Autoplay To Video Element With JavascriptStack Overflow