我目前有一个全屏幕背景视频,我已经循环播放音频,我想制作一个自定义按钮,以便访问者访问该网站时可以将音频静音/取消静音。
我尝试过使用此代码......
<video id="bg" loop autoplay preload="auto" poster="../img/still.jpg">
<source src="vid/vidbg2.mp4" type="video/mp4" />
<source src="vid/vidbg.webm" type="video/webm" />
<source src="vid/vidbg.ogv" type="video/ogg" />
Your browser does not support this video
</video>
<div id="video_controls">
<button id="mutebtn">mute</button>
</div>
和JavaScript
<script>
var mutebtn;
function intitializePlayer(){
//set object references
vid = document.getElementById("bg");
mutebtn = document.getElementById("mutebtn");
//add event listener
mutebtn.addEventListener("click,vidmute,false");
}
function.vidmute(){
if(vid.muted){
vid.muted = false;
mutebtn.innerHTML = "mute";
} else {
vid.muted = true;
mutebtn.innerHTML = "Unmute";
}
}
</script>
想想我可能会咆哮错误的树?
任何帮助都会非常感谢!
答案 0 :(得分:0)
在需要的地方看起来像三个小的变化
1 /仅引用click
而不是整个addEventListener参数
2 /调用初始化
3 /从function.vidmute
<script>
var mutebtn;
intitializePlayer() // **don't forget to call the initialize**
function intitializePlayer(){
//set object references
vid = document.getElementById("bg");
mutebtn = document.getElementById("mutebtn");
//add event listener
mutebtn.addEventListener("click",vidmute,false); // **quotes only around the event**
}
function vidmute(){ // **there was an extra '.' between function and vidmute**
if(vid.muted){
vid.muted = false;
mutebtn.innerHTML = "mute";
} else {
vid.muted = true;
mutebtn.innerHTML = "Unmute";
}
}
</script>