Vue实现全屏缩放

本篇介绍两种实现全屏缩放的方式。

第一种:使用原生js

1、XXX.vue

<template>
	<div class="full" @click="full">
	    <el-tooltip effect="dark" content="Full Screen" placement="bottom">
            <span class="el-icon-full-screen"></span>
        </el-tooltip>
	</div>
</template>

<script>
  export default {
    data () {
      return {
        isFull: true,
      }
    },
    methods: {
      full () {
        if (this.isfullScreen) {
          var docElm = document.documentElement
          if (docElm.requestFullscreen) {
            docElm.requestFullscreen()
          } else if (docElm.mozRequestFullScreen) {
            docElm.mozRequestFullScreen()
          } else if (docElm.webkitRequestFullScreen) {
            docElm.webkitRequestFullScreen()
          } else if (elem.msRequestFullscreen) {
            elem.msRequestFullscreen()
          }
          this.isFull = false
        } else {
          if (document.exitFullscreen) {
            document.exitFullscreen()
          } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen()
          } else if (document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen()
          } else if (document.msExitFullscreen) {
            document.msExitFullscreen()
          }
          this.isFull = true
        }
      },
    }
  }
</script>

<style lang="scss">
.full {
	cursor: pointer;
}
</style>

第二种:引入screenfull插件

1、安装screenfull

npm install --save-dev screenfull@5.1.0

如果直接npm install screenfull,可能会报错,如下图所示。

2、XXX.vue

<template>
	<div class="full" @click="full">
    	<el-tooltip effect="dark" content="Full Screen" placement="bottom">
	         <span class="el-icon-full-screen"></span>
        </el-tooltip>
    </div>
</template>

<script>
import screenfull from 'screenfull'
export default {
  data () {
   	return {
   		isFull: false
   	}
  },
  methods: {
    full () {
      screenfull.toggle()
      if (!screenfull.isEnabled) {
        this.$message({
          message: 'The browser can not support screenfull',
          type: 'warning'
        })
      }
      this.isFull = !this.isFull
    }
  }
}
</script>

<style lang="scss">
.full {
	cursor: pointer;
}
</style>

你可能感兴趣的:(vue,JavaScript,vue,javascript)