如何实现移动端适配以及1px处理

移动端适配

目标

能跟通过配置实现自动适配移动端项目

适配概述

为什么要适配?

  • 为了让我们开发的移动端项目页面,在不同尺寸大小的移动端设备(手机)中,保持相同的比例

适配原理

  • 选择某个手机的尺寸大小作为基准,其他手机进行等比例缩放
  • 一般选择 iPhone 6(2倍屏幕),屏幕宽度为:375px

适配方式

  • rem:需要手动修改 html 元素的 font-size;额外设置 body 元素的字体大小为正常值
  • vw: 1 vw 等于屏幕宽度的 1%

image.png

步骤

postcss-px-to-viewport 文档

  1. 安装 px 转 vw 的包:npm i -D postcss-px-to-viewport

    • 包的作用:将 px 转化为 vw,所以有了该工具,只需要在代码中写 px 即可
  2. craco.config.js 添加相应配置

  3. 重启项目,让配置生效

核心代码

craco.config.js

const pxToViewport = require('postcss-px-to-viewport')
const vw = pxToViewport({
  // 视口宽度,一般就是 375( 设计稿一般采用二倍稿,宽度为 375 )
  viewportWidth: 375
})
​
module.exports = {
  // 此处省略 webpack 配置
  webpack: {},
​
  // 这里补充style配置
  style: {
    postcss: {
      plugins: [vw]
    }
  }
}
复制代码

项目准备-移动端 1px 像素处理

目标

能够展示1px像素的线条

问题描述

image.png

css的1px在PC端就是1px; 在移动端就往往就>1px了

产生原因

  • 设备像素比:dpr=window.devicePixelRatio,也就是设备的物理像素与逻辑像素的比值。
  • retina屏的手机上, dpr23css里写的1px宽度映射到物理像素上就有2px3px宽度。
  • 例如:iPhone6dpr2,物理像素是750(x轴),它的逻辑像素为375。也就是说,1个逻辑像素,在x轴和y轴方向,需要2个物理像素来显示,即:dpr=2时,表示1个CSS像素由4个物理像素点组成。

解决方式

实现原理:伪元素 + transform 缩放

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="author" content="[email protected]">
    <title>移动端 1px(线条/边框) 解决方案</title>
    <style type="text/css">
        body{
            margin: 0;
            padding: 0;
            font-size: 14px;
            color: #333;
            font-family: 'Microsoft YaHei', 'Times New Roman', Times, serif;
        }
 
        /* 线条 */
        .list{
            margin: 0 20px;
            list-style: none;
            line-height: 42px;
            padding: 0;
        }
        .list>li{
            padding: 0;
            position: relative;
        }
        .list>li:not(:first-child):after{   /* CSS匹配非第一个直接子元素 */
            content: "";
            display: block;
            height: 0;
            border-top: #999 solid 1px;
            width: 100%;
            position: absolute;
            top: 0;
            right: 0;
            transform: scaleY(0.5); /* 将 1px 的线条缩小为原来的 50% */
        }
 
        /* 边框 */
        .button{
            line-height: 42px;
            text-align: center;
            margin: 20px;
            background-color: #f8f8f8;
            position: relative;
            border-radius: 4px;
        }
        .button:after{
            content: "";
            position: absolute;
            top: -50%;
            right: -50%;
            bottom: -50%;
            left: -50%;
            border: 1px solid #999;
            transform: scale(0.5);
            transform-origin: 50% 50% 0;
            box-sizing: border-box;
            border-radius: 8px; /* 尺寸缩小 50%,即圆角半径设置为按钮的2倍 */
        }
    </style>
</head>
<body>
<ul class="list">
    <li>线条 1px</li>
</ul>
<div class="button">边框 1px</div>
</body>
</html>
复制代码

伪元素::after::before独立于当前元素,可以单独对其缩放而不影响元素本身的缩放

参考:blog.csdn.net/xgangzai/ar…

参考:blog.csdn.net/xgangzai/ar…

创建公共样式

实现参考 antd-mobile

创建公共样式hairline.scss

// src/assets/styles/hairline.scss
​
@mixin scale-hairline-common($color, $top, $right, $bottom, $left) {
  content: '';
  position: absolute;
  display: block;
  z-index: 1;
  top: $top;
  right: $right;
  bottom: $bottom;
  left: $left;
  background-color: $color;
}
​
​
@mixin hairline($direction, $color: #000, $radius: 0) {
  @if $direction == top {
    border-top: 1px solid $color;
​
    // min-resolution 用来检测设备的最小像素密度
    @media (min-resolution: 2dppx) {
      border-top: none;
​
      &::before {
        @include scale-hairline-common($color, 0, auto, auto, 0);
        width: 100%;
        height: 1px;
        transform-origin: 50% 50%;
        transform: scaleY(0.5);
​
        @media (min-resolution: 3dppx) {
          transform: scaleY(0.33);
        }
      }
    }
  } @else if $direction == right {
    border-right: 1px solid $color;
​
    @media (min-resolution: 2dppx) {
      border-right: none;
​
      &::after {
        @include scale-hairline-common($color, 0, 0, auto, auto);
        width: 1px;
        height: 100%;
        background: $color;
        transform-origin: 100% 50%;
        transform: scaleX(0.5);
​
        @media (min-resolution: 3dppx) {
          transform: scaleX(0.33);
        }
      }
    }
  } @else if $direction == bottom {
    border-bottom: 1px solid $color;
​
    @media (min-resolution: 2dppx) {
      border-bottom: none;
​
      &::after {
        @include scale-hairline-common($color, auto, auto, 0, 0);
        width: 100%;
        height: 1px;
        transform-origin: 50% 100%;
        transform: scaleY(0.5);
​
        @media (min-resolution: 3dppx) {
          transform: scaleY(0.33);
        }
      }
    }
  } @else if $direction == left {
    border-left: 1px solid $color;
​
    @media (min-resolution: 2dppx) {
      border-left: none;
​
      &::before {
        @include scale-hairline-common($color, 0, auto, auto, 0);
        width: 1px;
        height: 100%;
        transform-origin: 100% 50%;
        transform: scaleX(0.5);
​
        @media (min-resolution: 3dppx) {
          transform: scaleX(0.33);
        }
      }
    }
  } @else if $direction == all {
    border: 1px solid $color;
    border-radius: $radius;
​
    @media (min-resolution: 2dppx) {
      position: relative;
      border: none;
​
      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        width: 200%;
        height: 200%;
        border: 1px solid $color;
        border-radius: $radius * 2;
        transform-origin: 0 0;
        transform: scale(0.5);
        box-sizing: border-box;
        pointer-events: none;
      }
    }
  }
}
​
// 移除边框
@mixin hairline-remove($position: all) {
  @if $position == left {
    border-left: 0;
    &::before {
      display: none !important;
    }
  } @else if $position == right {
    border-right: 0;
    &::after {
      display: none !important;
    }
  } @else if $position == top {
    border-top: 0;
    &::before {
      display: none !important;
    }
  } @else if $position == bottom {
    border-bottom: 0;
    &::after {
      display: none !important;
    }
  } @else if $position == all {
    border: 0;
    &::before {
      display: none !important;
    }
    &::after {
      display: none !important;
    }
  }
}
复制代码

使用格式

在某个scss文件中:

  1. 导入。导入上面封装的hairline.scss

  2. 使用:给某个选择器添加1px边框

    选择器{
      @include hairline(参数,xxxx)
    }
    复制代码

示例

@import '~@scss/hairline.scss';
​
.box1 {
  margin: 10px 0;
  position:relative;
  @include hairline(bottom, #000); // 添加边框
  @include hairline-remove(bottom); // 移除边框
}
​
复制代码

猜你喜欢

转载自juejin.im/post/7036898350918008840