如何实现移动端页面默认横屏显示
思路分析
创建项目
vue init webpack vue-horizontal-demo
具体步骤
- 安装命令
npm i postcss-pxtorem --save-dev
- 打开 build/vue-loader.conf.js 加入 px2rem 配置
"use strict";
const utils = require("./utils");
const config = require("../config");
const px2rem = require("postcss-pxtorem");
const isProduction = process.env.NODE_ENV === "production";
const sourceMapEnabled = isProduction
? config.build.productionSourceMap
: config.dev.cssSourceMap;
module.exports = {
loaders: utils.cssLoaders({
sourceMap: sourceMapEnabled,
extract: isProduction
}),
cssSourceMap: sourceMapEnabled,
cacheBusting: config.dev.cacheBusting,
transformToRequire: {
video: ["src", "poster"],
source: "src",
img: "src",
image: "xlink:href"
},
postcss: function() {
return [
px2rem({
rootValue: 75,
propList: ["*", "!border"],
minPixelValue: 1
})
];
}
};
- 在 index.html 中加入计算 font-size 代码
<script>
window.calcFontSize = () => {
document.documentElement.style.fontSize =
Math.min(
document.documentElement.clientWidth,
document.documentElement.clientHeight
) /
10 +
"px";
};
window.calcFontSize();
</script>
- 增加 horizontal-screen 全局指令
Vue.directive("horizontal-screen", {
bind(el, binding, vnode) {
let self = vnode.context;
let getDocumentSize = () => [
document.documentElement.clientWidth,
document.documentElement.clientHeight
];
// 设备开启竖屏锁定,强制横屏模式
let vertical = () => {
let [width, height] = getDocumentSize();
el.style.transform = `rotate(90deg)`;
el.style.transformOrigin = width / 2 + 'px center';
el.style.width = height + 'px';
el.style.height = width + 'px';
};
// 设备关闭竖屏锁定,横屏时,还原成正常模式
let reset = () => {
let [width, height] = getDocumentSize();
el.style.transform = `rotate(0deg)`;
el.style.width = `${width}px`;
el.style.height = `${height}px`;
};
el.resize = function() {
if (document.activeElement.nodeName === "INPUT") return; // 兼容安卓
window.calcFontSize();
if ([null, 180, 0].includes(window.orientation)) {
vertical();
} else if ([90, -90].includes(window.orientation)) {
reset();
}
};
el.resize();
el.click = e => {
if (e.target.nodeName === "INPUT") {
reset();
} else if (![90, -90].includes(window.orientation)) {
vertical();
}
};
window.addEventListener("click", el.click, false);
window.addEventListener("resize", el.resize, false); // 兼容安卓
window.addEventListener("orientationchange", el.resize, false);
},
unbind(el, binding, vnode) {
window.removeEventListener("click", el.click, false);
window.removeEventListener("resize", el.resize, false);
window.removeEventListener("orientationchange", el.resize, false);
}
});
- 在页面最外层的容器加上 'v-horizontal-screen' 即可
欢迎交流,QQ: 997494167
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构