vue h5页面实现某块全屏后横屏

原文链接
全屏screenfull
横屏方法

全屏需要安装下面的依赖,需要加上版本,最新的会报错

npm install --save-dev screenfull@5.1.0

页面代码

<div class="map-box" ref="mapbox">
  <div class="map-mid-box" ref="mapmidbox">
    <div id="map" ref="map"></div>
    <div class="full-screen" @click="handleisScreenFull">
      <div class="icon"></div>
      <div class="text">全屏</div>
    </div>
  </div>
</div>

在页面引用全屏方法

import screenfull from "screenfull";

以下为点击全屏方法

 handleisScreenFull() {
   if (!screenfull.isEnabled) {
     // 如果不支持进入全屏,发出不支持提示
     this.$message({
       message: "您的浏览器版本过低不支持全屏显示!",
       type: "warning",
     });
     return false;
   }
   //此处填入需要全屏的ref属性值即可
   screenfull.toggle(this.$refs.mapbox);
   this.$nextTick(() => {
     this.rotateBox();
   });
 },

以下为切换横屏/竖屏方法

rotateBox() {
  this.isScreenFull = !this.isScreenFull;//是否全屏状态
  let width = document.documentElement.clientWidth,//页面宽度
    height = document.documentElement.clientHeight,//页面高度
    wrapper = this.$refs.mapmidbox,//需要横屏的块,与全屏的块要区分开,不区分全屏的块无法显示横屏效果
    style = "";//样式
  if (height < width) {//注意原来就是宽屏时不用横屏
  	 return;
  }
  if (this.isScreenFull) {
	  // 横屏
	 style += "width:" + height + "px;";
	 style += "height:" + width + "px;";
	 style += "-webkit-transform: rotate(90deg); transform: rotate(90deg);";
	 // 注意旋转中点的处理
	 style +=
	   "-webkit-transform-origin: " + width / 2 + "px " + width / 2 + "px;";
	 style += "transform-origin: " + width / 2 + "px " + width / 2 + "px;";
  } else {
	 // 竖屏
	 style += "width:100%";
	 style += "height:100%;";
	 style += "-webkit-transform: rotate(0); transform: rotate(0);";
	 style += "-webkit-transform-origin: 0 0;";
	 style += "transform-origin: 0 0;";
  }
  wrapper.style.cssText = style;
},
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值