当前位置: 首页 > 工具软件 > fullscreen.js > 使用案例 >

vue项目中使用screenfull.js实现全屏,缩放功能

祝灼光
2023-12-01
  1. 第一步安装依赖 cnpm install --save screenfull
  2. 封装组件 components下创建文件夹Screenfull 下创建文件index.vue
			//index.vue中代码
			<template>
			  <div>
			    <svg-icon :icon-class="isFullscreen?'exit-fullscreen':'fullscreen'" @click="click" />
			  </div>
			</template>
			
			<script>
			import screenfull from 'screenfull'
			
			export default {
			  name: 'Screenfull',
			  data() {
			    return {
			      isFullscreen: false
			    }
			  },
			  mounted() {
			    this.init()
			  },
			  beforeDestroy() {
			    this.destroy()
			  },
			  methods: {
			    click() {
			      if (!screenfull.enabled) {
			        this.$message({
			          message: 'you browser can not work',
			          type: 'warning'
			        })
			        return false
			      }
			      screenfull.toggle()
			    },
			    change() {
			      this.isFullscreen = screenfull.isFullscreen
			    },
			    init() {
			      if (screenfull.enabled) {
			        screenfull.on('change', this.change)
			      }
			    },
			    destroy() {
			      if (screenfull.enabled) {
			        screenfull.off('change', this.change)
			      }
			    }
			  }
			}
			</script>
			
			<style scoped>
			.screenfull-svg {
			  display: inline-block;
			  cursor: pointer;
			  fill: #5a5e66;;
			  width: 20px;
			  height: 20px;
			  vertical-align: 10px;
			}
			</style>

3.页面中使用

//script中首先引入
import Screenfull from '@/components/Screenfull'
 components: {
    Screenfull
  },
//template页面中使用
<screenfull id="screenfull"  class="right-menu-item hover-effect" />

!注意exit-fullscreen/fullscreen svg图片放入icons下的svg文件夹下可自定义

 类似资料: