vue 如何实现手机横屏功能

功能背景

有些需求需要手动实现横屏功能,比如点击一个横屏的图标将整个页面90度翻转,再点击退出横屏回到正常页面。
在这里插入图片描述

实现思路

一拿到这个需求很可能就被吓到了,但简单来说,就是点击横屏按钮跳转一个新页面,通过 css样式 的翻转样式来实现,主要是计算横屏的宽高比较麻烦,下面来看具体的代码实现。

关键代码:

<view class="box">
	<view class="jxcLandscape" :style="{width:newHeight+'px',height:newWidth+'px'}">
		<view class="title_H">
			<view @click="handleBack" class="image_H">
				<img style="width:40rpx;height: 40rpx;margin-right: 8rpx;" src="@/static/screen.png" />
				退出横屏
			</view>
		</view>
		
		<!--主要内容区-->
	</view>
</view>

css 样式:

.box{
		position: relative;
		width: 100%;
		height: 100vh;
		overscroll-behavior: none;
	}
	.jxcLandscape{
		padding: 10px;
		transform: rotate(90deg); // 关键代码
		transform-origin: 0% 0%; // 关键代码
		position: absolute;
		top: 0%;
		left: 100%;
		margin-left: 0;
			
	}

js 方法:

onLoad(){
	let that=this
	uni.getSystemInfo({
		success: function (res) {
			that.newWidth=res.windowWidth
			that.tablenewWidth=res.windowWidth-50
			if(res.platform=='android'){
				this.getStatusBarHeight((height) => {
					that.barHeight = height
					that.newHeight=res.windowHeight-that.barHeight
				})
			}else{
				// 这是苹果操作系统
				that.newHeight=res.windowHeight
			}
		}
	})
},
methods:{
	getStatusBarHeight(){
		let barHeight = 51
		if (uni.getSystemInfoSync().platform == "ios") {
			// ios {}可传参 
			this.callhandler('getStatusBarHeight', "", callBack);
		}
		if (uni.getSystemInfoSync().platform == "android") {
			// Android
			if (window.webkit) {
				barHeight = window.webkit.getStatusBarHeight()
				callBack(barHeight)
			}
		}
	},
	callhandler(name, data, callback) {
		setupWebViewJavascriptBridge(function(bridge) {
			bridge.callHandler(name, data, callback)
		})
	},
	setupWebViewJavascriptBridge(callback) {
		if (window.WebViewJavascriptBridge) {
			return callback(window.WebViewJavascriptBridge)
		}
		if (window.WVJBCallbacks) {
			return window.WVJBCallbacks.push(callback)
		}
		window.WVJBCallbacks = [callback]
		let WVJBIframe = document.createElement('iframe')
		WVJBIframe.style.display = 'none'
		WVJBIframe.src = 'https://__bridge_loaded__'
		document.documentElement.appendChild(WVJBIframe)
		setTimeout(() => {
			document.documentElement.removeChild(WVJBIframe)
		}, 0)
	}
}

相关推荐

  1. uniapp实现手机(方法二)

    2024-03-15 05:34:07       50 阅读
  2. APP开发_ js 控制手机或竖

    2024-03-15 05:34:07       35 阅读
  3. Vue 3中实现页面锁功能

    2024-03-15 05:34:07       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 05:34:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 05:34:07       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 05:34:07       82 阅读
  4. Python语言-面向对象

    2024-03-15 05:34:07       91 阅读

热门阅读

  1. 前端框架的发展史

    2024-03-15 05:34:07       45 阅读
  2. Swift 单元测试

    2024-03-15 05:34:07       33 阅读
  3. 某小型外包—ETL工程师面试

    2024-03-15 05:34:07       38 阅读
  4. 2024年“新质生产力”是什么?新质生产力概述

    2024-03-15 05:34:07       43 阅读
  5. 在 Ubuntu 22.04 上源码安装 Podman 4

    2024-03-15 05:34:07       39 阅读
  6. 高速口光口通信

    2024-03-15 05:34:07       43 阅读
  7. 大模型在智能家居领域的应用与挑战

    2024-03-15 05:34:07       42 阅读