• H5页面自定义 pxTorem 函数进行单位转换


    一、css部分(mixin.scss):

    $browser-default-font-size: 16px !default;
    
    @function pxTorem($px) {
    
      //$px为需要转换的字号
      @if (unitless($px)) {
        @return pxTorem($px + 0px);
      }
    
      @else if (unit($px)==em) {
        @return $px;
      }
    
      @return ($px / $browser-default-font-size) * 1rem;
    }

    二、js部分(main.js):

    // 基准大小
    const baseSize = 32
    
    // 设置 rem 函数
    function setRem() {
        // 当前页面宽度相对于 750 宽的缩放比例,可根据自己需要修改。
        let scale = document.documentElement.clientWidth / 750
        if (scale > 1) {
            scale = 1.024
        }
        // 设置页面根节点字体大小
        document.documentElement.style.fontSize = (baseSize * Math.min(scale, 2)) + 'px'
    }
    
    // 初始化
    setRem()
    // 改变窗口大小时重新设置 rem
    window.onresize = function () {
        setRem()
    }

    三、补充知识点:

    1、sass-unitless() 函数

    unitless() 函数相对来说简单明了些,只是用来判断一个值是否带有单位,如果不带单位返回的值为 true,带单位返回的值为 false:

    >> unitless(100)
    true
    >> unitless(100px)
    false
    >> unitless(100em)
    false
    >> unitless(100%)
    false
    >> unitless(1 /2 )
    true
    >> unitless(1 /2 + 2 )
    true
    >> unitless(1px /2 + 2 )
    false

    例如:

    // 判断如果没有单位则加上单位"px"@mixin adjust-location($x, $y) {
      @if unitless($x) {    
        $x: 1px * $x;
      }
      @if unitless($y) {    
        $y: 1px * $y;
      }
      position: relative; 
      left: $x; 
      top: $y;
    }
    
    .botton{
        @include adjust-location(20px, 30);
    }

    2、sass-unit() 函数

    unit() 函数主要是用来获取一个值所使用的单位,碰到复杂的计算时,其能根据运算得到一个“多单位组合”的值,不过只充许乘、除运算:

    >> unit(100)
    ""
    >> unit(100px)
    "px"
    >> unit(20%)
    "%"
    >> unit(1em)
    "em"
    >> unit(10px * 3em)
    "em*px"
    >> unit(10px / 3em)
    "px/em"
    >> unit(10px * 2em / 3cm / 1rem)
    "em/rem"

    但加、减碰到不同单位时,unit() 函数将会报错,除 px 与 cm、mm 运算之外

    >> unit(1px + 1cm)
    "px"
    >> unit(1px - 1cm)
    "px"
    >> unit(1px + 1mm)
    "px"
    >> unit(10px * 2em - 3cm / 1rem)
    SyntaxError: Incompatible units: 'cm' and 'px*em'.
    >> unit(10px * 2em - 1px / 1rem)
    SyntaxError: Incompatible units: '' and 'em'.
    >> unit(1px - 1em)
    SyntaxError: Incompatible units: 'em' and 'px'.
    >> unit(1px - 1rem)
    SyntaxError: Incompatible units: 'rem' and 'px'.
    >> unit(1px - 1%)
    SyntaxError: Incompatible units: '%' and 'px'.
    >> unit(1cm + 1em)
    SyntaxError: Incompatible units: 'em' and 'cm'.

    unit() 函数对于单位运算相对来说也没有规律,而且有些单位也无法整合成一个单位,对于我们在 CSS 中运用中并不适合,比如:

    >> unit(10px * 3em)
    "em*px"
    >> unit(10px / 3em)
    "px/em"
    >> unit(10px * 2em / 3cm / 1rem)
    "em/rem"

    换句话说,上面运算出来的单位,对于在 CSS 中使用将是没有任何意义的。

  • 相关阅读:
    比特币脚本及交易分析
    分析比特币网络:一种去中心化、点对点的网络架构
    比特币如何挖矿(挖矿原理)-工作量证明
    比特币所有权及隐私问题-非对称加密应用
    区块链记账原理
    用Python从零开始创建区块链
    基础语法-算术运算符
    基础语法-数据类型转换
    基础语法-数据类型介绍及使用
    基础语法-变量介绍
  • 原文地址:https://www.cnblogs.com/ysx215/p/14990567.html
Copyright © 2020-2023  润新知