I am trying to bind click event on video but it is not working. I need to fire event whenever user click on anywhere on the video or controls of video element. I have tried following:
component.html
<video
[src]="url"
(loadeddata)="onVideoLoaded($event)"
(click)="doSomething($event)"
>
</video>
component.ts
onVideoLoaded(event) {
const videoEl = event.target as HTMLVideoElement;
videoEl.onclick = (event) => this.doSomething(event)
videoEl.addEventListener('mousedown', this.doSomething);
}
doSomething(event){
console.log(event)
}
None of the above approach to bind video click event is working. How can I do it?
2
Answers
I think you can approach it another way. Wrap the video in a div and handle the click event on it
Generally you cannot bind a click into the area of the video object AND also including its controls.
The controls are outside of the DOM (so your code cannot reach them, although they will still accept being given a
<style>
setting).Since the browser’s built-in HTML5 controls are not reachable by code…
The best option would be to disable those
controls
and just use your own custom interface. Then the container Div (of custom controls) is clickable, and JS can report such clicks as you wish to know.Players like VideoJS and JWplayer etc all use custom controls. This problem doesn’t exist if you use some custom controls instead of the browser’s own controls (along with any given browser limitations).
If still keeping the browser’s built-in HTML5 controls, then you have two options:
Option (1): Capture any click attempt on the Video area.
This will disable any/all clicks on the
<video>
element itself. If you needed that user-click to do something, then you will have to trigger it manually (eg: if they click at the x/y of the "play" button, then use JS to cause the required play/pause result, or even use JS to send a.click()
command to that button where possible).In the HTML, setup a Div and Video object separately. Put the Div on a layer above the Video object by using
z-index
(which itself needs aposition
attribute to also exist, so for testing here I usedabsolute
. Change theposition
as needed for your project’s display layout)Then this JS code will give feedback (x/y pos) of the clicked area.
Option (2): Detect events related to clicking on a Video object
Another approach is to simply detect events that would only come from clicking on video area. Events such as play, pause, seeking, and volumechange will be useful.
The HTML is a simple Video tag with ID
Then this JS code lets you know if a Video event happened whilst the mouse is located over the Video object’s area. From there you can
do_Something()
as required…