vue2实现全屏切换

1.安装插件

npm install screenfull --save

2.页面引入

// 全屏组件
import screenfull from 'screenfull'

3.绑定事件使用

  <!-- 全屏 -->
          <el-tooltip class="item" effect="dark" :content="isFullscreen ? '取消全屏' : '全屏'" placement="bottom">
          <i :class="isFullscreen ? 'el-icon-full-screen head-screen-news' : 'el-icon-rank head-screen'" @click="buttoncli"></i>
        </el-tooltip>

4.全屏点击事件

 // 全屏
    buttoncli() {
    
      screenfull.toggle()
    
      this.checkFull()
    
    },
    
    // 监控屏幕变化
    
    checkFull() {
    
      var isFull = document.fullscreenEnabled || window.fullScreen || document.webkitIsFullScreen || document.msFullscreenEnabled;
    
      // 是否全屏判断
    
      this.isFullscreen = !this.isFullscreen;
    
      if (this.isFullscreen) {
    
        this.$message({
    
          message: '全屏啦',
    
          type: 'success'
    
        })
    
      } else {
    
        this.$message({
    
          message: '取消全屏',
    
          type: 'warning'
    
        })
    
      }
    
      if (isFull === undefined) {
    
        isFull = false;
    
      }
    
      return isFull;
    
    },

A: 首先,需要在Vue项目中安装ElementUI组件库,用于实现弹窗的样式。 安装ElementUI: ``` npm i element-plus -S ``` 创建一个全屏弹窗组件FullScreenDialog.vue,代码如下: ```html <template> <div class="full-screen-dialog-mask" v-show="visible"> <div class="full-screen-dialog"> <div class="full-screen-dialog-header"> <span>{{ title }}</span> <i class="el-icon-close" @click="close"></i> </div> <div class="full-screen-dialog-body"> <slot></slot> </div> </div> </div> </template> <script> import { reactive } from 'vue'; export default { name: 'FullScreenDialog', props: { visible: { type: Boolean, default: false, }, title: { type: String, default: '全屏弹窗', }, }, setup(props, { emit }) { const state = reactive({}); const close = () => { emit('update:visible', false); }; return { state, close, }; }, }; </script> <style> .full-screen-dialog-mask { position: fixed; top: 0; left: 0; z-index: 1000; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); } .full-screen-dialog { position: absolute; top: 50%; left: 50%; z-index: 1001; width: 80%; max-height: 90%; transform: translate(-50%, -50%); background-color: #fff; border-radius: 5px; overflow: auto; } .full-screen-dialog-header { display: flex; justify-content: space-between; padding: 10px; border-bottom: 1px solid #ebeef5; } .full-screen-dialog-header span { font-size: 16px; } .full-screen-dialog-body { padding: 20px; } </style> ``` 然后,可以在需要使用全屏弹窗的组件中引入并使用FullScreenDialog组件,代码如下: ```html <template> <div> <el-button type="primary" @click="dialogVisible = true">打开全屏弹窗</el-button> <full-screen-dialog title="全屏弹窗" v-model:visible="dialogVisible"> <div>这是全屏弹窗的内容</div> </full-screen-dialog> </div> </template> <script> import FullScreenDialog from '@/components/FullScreenDialog.vue'; export default { components: { FullScreenDialog, }, data() { return { dialogVisible: false, }; }, }; </script> ``` 这样就实现了一个简单的全屏弹窗组件,用户点击打开按钮时,会弹出全屏弹窗,点击关闭按钮或者遮罩层时,会关闭弹窗。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值