PC 打开 vue h5移动端 页面 不变形《转载》

有时候,我们想让pc打开适配手机的h5,并且不变形,在不使用媒体查询的情况下,我们可以使用以下方式:

实现功能如下

pc打开

在这里插入图片描述

周围留空白,居中显示

当浏览器切换为手机模式/手机打开网址时

在这里插入图片描述

会变为手机模式

以下为主要代码

public 文件夹下 index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <script async="" src="https://www.google-analytics.com/analytics.js"></script>
    <script async="" src="https://www.google-analytics.com/analytics.js"></script>
    <script type="text/javascript">window.alert = console.log;</script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <title>Title</title>
    <meta http-equiv="Cache-Control" content="max-age=180">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <link rel="dns-prefetch" href="//m.360buyimg.com">
    <meta name="format-detection" content="telephone=no">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, shrink-to-fit=no, viewport-fit=cover">
    <meta http-equiv="x-dns-prefetch-control" content="on">

    <script type="text/javascript">
        // 如果判断下来是pc 那么跳转嵌入 iframe h5
        ~function () {
            // 是不是移动端
            var isMobile = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent);
            // 在iframe内
            var inIframe = window.frames.length != parent.frames.length;
            if (!isMobile && !inIframe) {
                window.location.href = "pc.html"
            }
        }()
    </script>

</head>
<body>

<div id="app"></div>

<script type="module" src="/src/main.ts"></script>
</body>
</html>

 同级新建pc.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PC</title>

    <script type="text/javascript">
        // 如果判断下来是pc 那么跳转嵌入 iframe h5
        ~function () {
            // 是不是移动端
            var isMobile = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent);
            console.log(isMobile);
            if (isMobile) {
                window.location.href = "index.html"
            }
        }()
    </script>


</head>
<body style="background: #42b983">

<iframe style="background: white; display: block; margin: 0 auto;" width="414" height="716" src="./index.html" frameborder="0"></iframe>

</body>
</html>

主要实现是

index.html 在 iframe 内时就不要跳转了,上面逻辑写的很明白

---------------------------------------------------------------------------------------------------------------------------------

个人理解,光看js大概就是判断是什么设备,然后切换html。导入的那些我都不懂

<think>好的,我现在需要处理用户关于Vue屏幕适配插件的询问。用户希望得到推荐的插件,并且根据提供的引用资料来支持回答。首先,我需要回顾用户提供的引用内容,看看有哪些插件被提到。 引用[1]提到了大屏适配方案,但并没有具体插件名称。引用[2]提到动态rem+动态scale的策略,适用Vue2/Vue3。引用[3]推荐了一个Vue-Adaptive-plugin,专门针对数据可视化大屏,可能是一个错的推荐。引用[4]讨论了使用插件将px转rem,可能涉及postcss-pxtorem或类似工具,同时配置vue.config.js。引用[5]提到了lib-flexible,这是一个移动端适配的插件,但可能更适合移动端而非大屏。 接下来,我需要整理这些信息,确定哪些插件适用于同的场景。用户可能同时需要大屏和移动端的适配,所以应该区分推荐。例如,对于数据大屏,Vue-Adaptive-plugin(引用3)和动态rem+scale方案(引用2)比较合适。对于移动端,lib-flexible(引用5)和postcss-pxtorem(引用4)是常见选择。 然后,需要确保回答结构清晰,分点推荐,并附上每个插件的适用场景、技术方案和引用来源。同时,按照用户要求生成相关问题,比如比较同插件、移动端适配方法等。 最后,检查是否符合所有系统级指令:正确使用LaTeX格式,虽然这个问题可能涉及数学公式,但需要确认是否有遗漏。回答使用中文,引用标识正确添加在引用内容末尾,并生成相关问题。</think>以下是为您整理的Vue屏幕适配插件推荐及适配方案详解,根据同的应用场景提供针对性解决方案: ### 一、数据可视化大屏适配推荐 1. **Vue-Adaptive-Plugin** - 技术方案:基于动态rem+动态scale的双重策略,通过监听窗口变化自动调整缩放比例 - 优势:完美解决异形屏幕展示问题,保持图表/文字在同比例下变形 - GitHub:[Vue-Adaptive-plugin](https://github.com/zhanghaoshan2000/Vue-Adaptive-plugin)[^3] 2. **动态rem+scale方案** - 实现步骤: 1. 通过postcss-pxtorem插件实现px到rem转换 ```javascript // postcss.config.js module.exports = { plugins: { 'postcss-pxtorem': { rootValue: 192, // 设计稿宽度/10 propList: ['*'] } } } ``` 2. 添加全局缩放监听 ```javascript // main.js const setScale = () => { const scale = document.documentElement.clientWidth / 1920 document.body.style.transform = `scale(${scale})` } window.addEventListener('resize', setScale) ``` - 适用场景:4K屏/超宽屏等非常规分辨率适配[^2][^4] ### 二、移动端适配推荐 1. **lib-flexible + postcss-pxtorem** - 经典组合: - lib-flexible自动设置rem基准值 - postcss-pxtorem实现单位转换 - 配置示例: ```javascript // vue.config.js module.exports = { css: { loaderOptions: { postcss: { plugins: [ require('postcss-pxtorem')({ rootValue: 75, // 750px设计稿对应1rem=75px propList: ['*'] }) ] } } } } ``` - 适配效果:实现同移动设备的等比缩放[^5] 2. **vw/vh方案** - 技术原理: $$ 元素尺寸 = (设计稿尺寸 / 设计稿宽度) \times 100vw $$ - 实现方式: 使用postcss-px-to-viewport插件自动转换单位 ### 三、综合解决方案对比 | 方案类型 | 适用场景 | 核心插件 | 缩放策略 | |----------------|----------------|------------------------|--------------------| | 动态scale | 大屏展示 | Vue-Adaptive-plugin | 整体容器缩放 | | rem+scale | 响应式PC端 | postcss-pxtorem | 单位转换+比例缩放 | | flexible | 移动端H5 | lib-flexible | 动态rem基准值 | | viewport单位 | 移动端/PC | postcss-px-to-viewport | 视窗百分比 |
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值