I use tippy.js and when I add a <video loop muted playsinline autoplay>
inside a tooltip using allowHTML: true
, the video won’t autoplay on chrome. It works on firefox and safari though.
How can I achieve this?
Here is my js:
JavaScript
x
12
12
1
const modal = card.querySelector('.modal'),
2
modalContent = modal.innerHTML`
3
4
tippy('.card', {
5
content: modalContent,
6
allowHTML: true,
7
interactive: true,
8
animation: 'shift-away',
9
followCursor: true,
10
arrow: false
11
});
12
And here is my html:
JavaScript
1
8
1
<div class="modal">
2
<div>
3
<video loop muted playsinline autoplay>
4
<source src="http://localhost:8888/artifacts/wp-content/uploads/2022/05/Artifacts-Landing.mp4" type="video/mp4">
5
</video>
6
</div>
7
</div>
8
I tried to add
JavaScript
1
2
1
card.querySelector('video').play()
2
but it doesn’t work eather.
Thanks a lot in advance for your help,
Advertisement
Answer
It is indeed strange that a muted video would not autoplay. You can use setTimeout
and then trigger play()
using the onShow()
event. For some reason onShown()
doesn’t ever fire for me.
Here’s a working snippet:
JavaScript
1
19
19
1
const modal = document.querySelector('.modal'),
2
modalContent = modal.innerHTML;
3
4
tippy('.card', {
5
content: modalContent,
6
allowHTML: true,
7
interactive: true,
8
animation: 'shift-away',
9
followCursor: true,
10
arrow: false,
11
onShow(instance) {
12
setTimeout(() => {
13
let video = instance.popper.getElementsByTagName('video')[0];
14
video.currentTime = 0; //start from begining
15
video.play();
16
}, 1);
17
18
},
19
});
JavaScript
1
16
16
1
.modal {
2
display: none;
3
}
4
5
video {
6
width: 40vw;
7
}
8
9
.card {
10
padding-top: 10vh;
11
}
12
13
h1{
14
display:inline;
15
margin:1em;
16
}
JavaScript
1
13
13
1
<!-- Production -->
2
<h1 class="card">CARD 1</h1>
3
<h1 class="card">CARD 2</h1>
4
<script src="https://unpkg.com/@popperjs/core@2"></script>
5
<script src="https://unpkg.com/tippy.js@6"></script>
6
7
<div class="modal">
8
<div>
9
<video loop muted playsinline autoplay>
10
<source src="https://upload.wikimedia.org/wikipedia/commons/transcoded/6/6c/%22Movbild-fizika%22_falo_en_Big_Buck_Bunny.webm/%22Movbild-fizika%22_falo_en_Big_Buck_Bunny.webm.720p.vp9.webm" type="video/mp4">
11
</video>
12
</div>
13
</div>