methods: {
// 进入全屏
contorl_enter(){
this.status = true;
this.enterFullscreen();
},
// 退出全屏
contorl_quit(){
this.status = false;
this.exitFullscreen();
},
// 进入全屏方法
enterFullscreen() {
var docElm = document.documentElement;
//W3C
if(docElm.requestFullscreen) {
docElm.requestFullscreen();
}
//火狐
else if(docElm.mozRequestFullScreen) {
docElm.mozRequestFullScreen();
}
//谷歌
else if(docElm.webkitRequestFullScreen) {
docElm.webkitRequestFullScreen();
}
//IE
else if(docElm.msRequestFullscreen) {
docElm.msRequestFullscreen();
}
},
// 退出全屏方法
exitFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
}
else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
}
else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
}
else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
},
// 判断当前页面是否全屏
getFullScreenStatus() {
this.status = !(
document.fullscreen || document.mozFullScreen || document.webkitIsFullScreen || document.webkitFullScreen || document.msFullScreen
);
},
},
mounted() {
// 监听浏览器全屏事件 如果esc需要一些方法 也可以直接加入下方
let that = this;
document.addEventListener("fullscreenchange", function() {
that.getFullScreenStatus();
});
document.addEventListener("mozfullscreenchange", function() {
that.getFullScreenStatus();
});
document.addEventListener("webkitfullscreenchange", function() {
that.getFullScreenStatus();
});
document.addEventListener("msfullscreenchange", function() {
that.getFullScreenStatus();
});
},
说明:
addEventListener() 方法用于向指定元素添加监听事件。且同一元素目标可重复添加,不会覆盖之前相同事件,配合 removeEventListener() 方法来移除事件。
使用方法:
document.getElementById(元素id).addEventListener("click", function(){ console.log("目标元素被点击了"); });