web建站教程
     
  1. 首页
  2. 前端UI组件库
  3. AI项目和框架
  4. AIGC工具
  5. 百度echarts
  6. 地图大全
  7. 前端知识
  8. 更多
    vuejs
    js入门
    php入门
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站
    AI应用

vuejs项目如何实现移动端适配(2种方法介绍)

1123 ℃

vuejs项目如何实现移动端适配,下面web建站小编给大家简单介绍2种适配方法!

方法一:利用lib-flexible插件

安装脚手架

npm i lib-flexible --save	// 载lib-flexible

npm install px2rem-loader	// 安装px2rem-loader

在main.js中引入lib-flexible

import 'lib-flexible/flexible'

移动适配meta标签,index.html

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

在build/util.js引入px2rem相关代码

const cssLoader = {
  loader: 'css-loader',
  options: {
    minimize: process.env.NODE_ENV === 'production',
    sourceMap: options.sourceMap
  }
}
const px2remLoader = {
  loader: 'px2rem-loader',
  options: {
    //一般设置75
    remUnit: 35
  }
}

function generateLoaders(loader, loaderOptions) {
  const loaders = options.usePostCSS ? [cssLoader, postcssLoader,px2remLoader] : [cssLoader,px2remLoader]
  if (loader) {
    loaders.push({
      loader: loader + '-loader',
      options: Object.assign({}, loaderOptions, {
        sourceMap: options.sourceMap
      })
    })
  }
  // Extract CSS when that option is specified
  // (which is the case during production build)
  if (options.extract) {
    return ExtractTextPlugin.extract({
      use: loaders,
      fallback: 'vue-style-loader'
    })
  } else {
    return ['vue-style-loader'].concat(loaders)
  }
}

//1、lib-flexible:是 rem 的适配插件。(例:750px == 10rem)
//2、px2rem-loader :是为了方便在书写CSS时,输入 px 会 自动转为 rem。
//3、有时 用 ‘px2rem-loader ’ 插件,发现 rem 转换不正确。

方法二:利用rem布局

在index.html引入

<script>
(function () {
  // 在标准 375px 适配下,100px = 1rem;
  var baseFontSize = 100;  
  var baseWidth = 375;
  var set = function () {
    var clientWidth = document.documentElement.clientWidth || window.innerWidth;
    var rem = 100;
    if (clientWidth != baseWidth) {
      rem = Math.floor(clientWidth / baseWidth * baseFontSize);
    }
    document.querySelector('html').style.fontSize = rem + 'px';
  }
  set();
  window.addEventListener('resize', set);
}());
</script>

element-ui时间选择器el-date-picker默认当前时间至7天之前时间

vue实现表格的编辑,包括选择,合并,拆分,删除行/列,添加行/列

Motion for Vue:一个轻量级且功能强大的Vue3动画库

el-table合并行的通用方法(附js代码)

vue前端分页功能实现代码(附js代码)

标签: lib-flexible 移动端适配

上面是“vuejs项目如何实现移动端适配(2种方法介绍)”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_2903.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 首页 > vuejs
Trae:新一代免费的AI编程工具

在线育儿补贴计算器

快来看看你到底可以领到多少补贴!生活小工具
上一篇:
下一篇:
x 打工人ai神器