vue 实现全屏预览功能; 以及js 实现预览功能

一. vue 实现全屏预览

通过点击图片按钮,动态切换全屏预览

  1. 引入screenfull 模块,建议使用5.2.0版本,更高版本有可能会出现编译错误.
    npm install screenfull@5.2.0
  2. 实现
<template>
	<img src="../img/fullScreen" @click="fullScreenClick()">
</template>
<script>
import screenfull from 'screenfull'
export default {
	data() {
		return {
			isFullscreen: false // 是否全屏
		}
	},
	mounted(){
		this.initScreenFull()
	},
	beforeDestroy(){
		this.initScreenDistroy()
	},
	methods:{
		initScreenFull(){
			if(screenfull.isEnabled) {
				screenfull.on('change',this.change)
			}
		},
		change(){
			this.isFullscreen= screenfull.isFullscreen
		},
		initScreenDistroy(){
			if(screenfull.isEnabled) {
				screenfull.off('change',this.change)
			}
		},
		/**
		* 全屏预览事件处理
		*/
		fullScreenClick(){
			if(!screenfull.isEnabled) {
				this.$message.info('您的浏览器版本过低,不支持全屏浏览')
				return false
			} else {
				screenfull.toggle()
			}
		}
		
	}
}

</script>

二. js 实现全屏预览

function fullScreen(el) {  
    var rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen,  
        wscript;  
   
    if(typeof rfs != "undefined" && rfs) {  
        rfs.call(el);  
        return;  
    }  
   
    if(typeof window.ActiveXObject != "undefined") {  
        wscript = new ActiveXObject("WScript.Shell");  
        if(wscript) {  
            wscript.SendKeys("{F11}");  
        }  
    }  
}  
  
function exitFullScreen(el) {  
    var el= document,  
        cfs = el.cancelFullScreen || el.webkitCancelFullScreen || el.mozCancelFullScreen || el.exitFullScreen,  
        wscript;  
   
    if (typeof cfs != "undefined" && cfs) {  
      cfs.call(el);  
      return;  
    }  
   
    if (typeof window.ActiveXObject != "undefined") {  
        wscript = new ActiveXObject("WScript.Shell");  
        if (wscript != null) {  
            wscript.SendKeys("{F11}");  
        }  
  }  
}

<button id='btn'>全屏按钮</button>  
        <div id="content" style="background:yellow;width:500px;height:500px;">sljfsdlfj  
            <div id="quite" class="btn">退出全屏</div>  
        </div>
		var btn = document.getElementById('btn');  
        var content = document.getElementById('content');  
        btn.onclick = function(){  
            fullScreen(content);  
        }  
        var quite = document.getElementById('quite');  
        quite.onclick = function(){  
            exitFullScreen();  
        } 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值