Using the srcdoc
attribute is a really neat trick !
I was a bit curious and after reviewing your code it seems like the javascript is not needed :
<script>
var video = document.getElementById("myVideo");
var btn = document.getElementById("myBtn");
function myFunction() {
if (video.paused) {
video.play();
btn.innerHTML = "Pause";
} else {
video.pause();
btn.innerHTML = "Play";
}
}
</script>
myFunction
is supposed to be used by a button with the id myBtn
. A click on the button toggle the video state (.play()
or .pause()
), and change the text of the button accordingly to play or pause. What’s interesting is that there is no button in the iframe you provided.
It seems like the autoplay is working only because the video is inside an iframe - I dont know why, it probably has something to do with the way the browser load the iframe…
I also found that the only tag needed is the tag $:/tags/PageTemplate
.
With that in mind, it’s possible to get the autoplay working with the following code :
<style>
/*optional, prevent the iframe from hiding the background of the tiddler*/
[data-tiddler-title="{{!!title}}"]{
z-index:unset!important;
}
.tw-background{
border:0;
z-index:-1;
position:fixed;
top:0;
left:0;
width: 100%;
height: 100%;
}
</style>
<iframe class=tw-background srcdoc="""
<style>
body{margin:0;}
video {
width: 100%;
height: 100%;
object-fit: cover;
position:fixed;
}
</style>
<video autoplay muted loop playsinline preload=Metadata poster="http://placekitten.com/1920/1080">
<source src="https://i.imgur.com/8EjcHkv.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
"""/>
Interesting stuff !
PS: Note that we can use a low-res gif for the poster image. We could also use a data-URI to display a static image embedded in the wiki.
EDIT: forgot playsinline
, it is necessary on IOS