# uni.rpx2px(number) GitCodeGitHub

将rpx单位值转换成px

# rpx2px 兼容性

Web 微信小程序 Android iOS HarmonyOS
4.02 4.41 4.02 4.11 4.61

# 参数

名称 类型 必填 默认值 兼容性 描述
number number -
-

# 返回值

类型
number

# 示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见

扫码体验(手机浏览器跳转到App直达页)

示例

<template>
  <!-- #ifdef APP -->
  <scroll-view class="page-scroll-view">
  <!-- #endif -->
    <view class="page">
      <page-head :title="title"></page-head>
      <view>
        <view class="item">
          <text class="item-k">输入:</text>
          <text class="item-v">{{rpxValue}}rpx</text>
        </view>
        <view class="item">
          <text class="item-k">返回:</text>
          <text class="item-v">{{pxValue}}px</text>
        </view>
      </view>
      <view>
        <button id="convert" @click="rpx2px">转换</button>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>
<script>
  export default {
    data() {
      return {
        title: 'rpx2px',
        rpxValue: 750,
        pxValue: 0,
        result: false
      }
    },
    methods: {
      rpx2px: function () {
        this.pxValue = uni.rpx2px(this.rpxValue);

        // 仅限自动化测试
        const windowInfo = uni.getWindowInfo();
        if (windowInfo.windowWidth == this.pxValue) {
          this.result = true
        }
      }
    }
  }
</script>

<style>
  .page {
    padding: 15px;
  }

  .item {
    flex-direction: row;
  }

  .item-k {
    width: 72px;
    line-height: 2;
  }

  .item-v {
    font-weight: bold;
    line-height: 2;
  }
</style>

# 参见

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 兼容性 描述
errMsg string -
错误信息