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

html - How to make video play only when a button is clicked in javascript - Stack Overflow

programmeradmin0浏览0评论

I have a div that inside of it has a <video autoplay> <source src='myvid'> </video> and by default the div has a display ='none' on css. I am adding an event listener of click on the div and once its clicked i am changing the display none to display block.

the problem is that the video plays automatically when the site reloads and not when the button is clicked. basically i want the video to play only when the div is display ='block'. How can i do that in Javascript?

I have a div that inside of it has a <video autoplay> <source src='myvid'> </video> and by default the div has a display ='none' on css. I am adding an event listener of click on the div and once its clicked i am changing the display none to display block.

the problem is that the video plays automatically when the site reloads and not when the button is clicked. basically i want the video to play only when the div is display ='block'. How can i do that in Javascript?

Share Improve this question asked Sep 19, 2020 at 10:45 Why u do disWhy u do dis 4581 gold badge5 silver badges19 bronze badges 4
  • 1 have you tried removing the autoplay attribute ? :) – Roko C. Buljan Commented Sep 19, 2020 at 10:46
  • @RokoC.Buljan yes and if i do that the video doesnt play at all – Why u do dis Commented Sep 19, 2020 at 10:47
  • @enhzflep Sorry if you think so. I feel like the I have already listed the only code that is needed, if you read what i wrote. But anyway i will have it as a tip for next time – Why u do dis Commented Sep 19, 2020 at 10:57
  • @Whyudodis - . Since my ment has been deleted, it's obviously been judged inappropriate. I'm sorry I did not understand this before writing it. It was penned (keyed!) with the following page in mind. stackoverflow./help/minimal-reproducible-example – enhzflep Commented Sep 19, 2020 at 12:08
Add a ment  | 

2 Answers 2

Reset to default 4
  • Remove the autoplay attribute
  • Use JavaScript to play the video using a custom button and the .play() method
<video id="video"> <source src='myvid'></video>
<button id="play">PLAY</button>
document.querySelector("#play").addEventListener("click", () => {
  document.querySelector("#video").play();
});

If you don't want a custom button (not clear from your question) than you could provide the browser default by using the controls attribute

const EL_video = document.querySelector("#video");
const EL_play = document.querySelector("#play");

EL_play.addEventListener("click", () => {
  const isPaused = EL_video.paused;
  EL_video[isPaused ? "play" : "pause"]();
  EL_video.classList.toggle("u-none", !isPaused);
});
#video {width: 300px;}

/* Utility classes: */
.u-none {display: none;}
<button id="play">Toggle &amp; play</button><br>
<video id="video" class="u-none">
  <source src='http://vjs.zencdn/v/oceans.mp4' type='video/mp4'>
</video>

try addind an onclick attribute to the div like this:

<div onclick="play_video();">

Or you can create a button:

<button onclick="play_video();">play the video</button>

Then, create the javascript function like this:

function play_video()
{
  document.getElementById("id_of_the_video").play();
} 
发布评论

评论列表(0)

  1. 暂无评论