当前位置:首页 > 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

vue实现移动端适配

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单位:

vue实现移动端适配

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防止长按弹出系统菜单:

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

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

相关文章

vue实现菜单调用

vue实现菜单调用

Vue 实现动态菜单的常见方法 基于路由配置生成菜单 Vue Router 的路由配置可以天然转化为菜单结构,适合权限与路由绑定的系统: // router/index.js const rout…

vue中登陆实现逻辑

vue中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登…

vue实现下拉框删除

vue实现下拉框删除

Vue 实现下拉框删除功能 在Vue中实现下拉框的删除功能通常涉及以下步骤: 数据绑定与渲染 在Vue组件中定义数据数组用于存储下拉框选项: data() { return {…

vue 实现

vue 实现

Vue 实现的基本步骤 安装 Vue.js 可以通过 npm 或 CDN 引入。使用 npm 安装适合大型项目,CDN 适合快速原型开发。 npm install vue 在 HTML 文件中引入…

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用…

vue实现alert

vue实现alert

实现自定义 Alert 组件 在 Vue 中可以通过组件化方式实现自定义 Alert 弹窗。以下是基于 Vue 3 的实现示例: 组件代码 (Alert.vue) <template>…