当前位置:首页 > VUE

vue实现移动端适配

2026-01-20 12:05:31VUE

移动端适配方案

使用Vue实现移动端适配需要结合多种技术手段,确保页面在不同尺寸设备上正常显示。以下是几种常用方法:

viewport meta标签配置public/index.html或模板文件中添加:

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">

rem适配方案 安装postcss-pxtoremamfe-flexible

npm install postcss-pxtorem amfe-flexible -D

main.js中引入:

import 'amfe-flexible'

配置postcss.config.js

module.exports = {
  plugins: {
    'postcss-pxtorem': {
      rootValue: 37.5,
      propList: ['*'],
      selectorBlackList: ['.norem']
    }
  }
}

vw/vh方案 安装postcss-px-to-viewport

npm install postcss-px-to-viewport -D

配置postcss.config.js

module.exports = {
  plugins: {
    'postcss-px-to-viewport': {
      viewportWidth: 375,
      unitPrecision: 5,
      viewportUnit: 'vw',
      selectorBlackList: [],
      minPixelValue: 1,
      mediaQuery: false
    }
  }
}

媒体查询适配 在CSS中针对不同屏幕尺寸设置样式:

@media screen and (max-width: 320px) {
  .container {
    width: 100%;
  }
}
@media screen and (min-width: 321px) and (max-width: 414px) {
  .container {
    width: 90%;
  }
}

动态设置根字体大小 在Vue组件中监听窗口变化:

export default {
  mounted() {
    this.setRem()
    window.addEventListener('resize', this.setRem)
  },
  methods: {
    setRem() {
      const docEl = document.documentElement
      const width = docEl.clientWidth
      const rem = width / 10
      docEl.style.fontSize = rem + 'px'
    }
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.setRem)
  }
}

第三方UI库适配 使用专为移动端设计的UI库如Vant,需按文档配置rem或vw单位:

import { createApp } from 'vue'
import Vant from 'vant'
import 'vant/lib/index.css'

const app = createApp()
app.use(Vant)

图片和媒体适配 使用响应式图片处理:

<picture>
  <source media="(min-width: 768px)" srcset="large.jpg">
  <source media="(min-width: 320px)" srcset="medium.jpg">
  <img src="small.jpg" alt="响应式图片">
</picture>

触摸事件处理 在Vue中使用@touchstart@touchmove等替代部分点击事件:

<button @touchstart="handleTouch">按钮</button>

1px边框问题解决 使用伪元素和transform解决高清屏1px显示问题:

.border-1px {
  position: relative;
}
.border-1px::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 1px;
  background: #000;
  transform: scaleY(0.5);
  transform-origin: 0 0;
}

禁止长按菜单 添加CSS防止长按弹出系统菜单:

vue实现移动端适配

* {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

标签: vue端适配
分享给朋友:

相关文章

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.def…

vue 实现打印

vue 实现打印

Vue 实现打印功能的方法 在Vue项目中实现打印功能,可以通过以下几种方式实现: 使用window.print()方法 通过调用浏览器的原生打印API实现基础打印功能,适用于简单内容打印。 //…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…

vue实现评分

vue实现评分

Vue 实现评分功能 使用组件库实现 安装 element-ui 或 ant-design-vue 这类 UI 库,它们已经内置了评分组件。 以 element-ui 为例: <templa…

vue实现阻塞

vue实现阻塞

Vue 实现阻塞的方法 在 Vue 中实现阻塞操作通常涉及异步控制、状态管理或生命周期钩子的使用。以下是几种常见方法: 使用 async/await 处理异步阻塞 通过 async/await 可…

vue 水印 实现

vue 水印 实现

Vue 水印实现方法 使用 canvas 动态生成水印 在 Vue 组件中通过 canvas 绘制水印,并将其作为背景图片插入到目标元素中。这种方法灵活性高,支持动态调整水印内容、角度和透明度。 &…