uniapp app 实现自适应宽度 input

核心原理

input 输入,存在一个 view 元素容纳输入内容,此时获取 view 元素的宽,将其设置为 input 的宽

特殊情况:回显的时候当前元素可能不存在,此时需要借助一个永远显示的元素进行宽度计算(InputWidthHelper.vue

adaption-input.vue

js 复制代码
<template>
  <view class="adaption-input-wrapper">
    <view class="adaption-input">
      <input
        type="text"
        :placeholder-style="placeholderStyle"
	    :placeholder="placeholder"
        :style="{ width: inputWidth }"
        @input="changeInputFn"
      />
    </view>
    <view :id="randomID" class="a--input">{{ modelValue || '请输入' }}</view>
  </view>
</template>

<script>
  export default {
    inheritAttrs: false,
    props: {
      modelValue: {
        type: [String, Number],
        default: '',
      },
      placeholder: {
        type: String,
        default: '请输入',
      },
      placeholderStyle: {
        type: String,
        default: 'color: #9ea5bb',
      },
    },
    data() {
      return {
        randomID: 'adaption_' + new Date().getTime(),
        inputWidth: '',
      }
    },
    watch: {
      modelValue: {
        handler() {
          this.$nextTick(() => {
            this.changeInputFn()
          })
        },
        immediate: true,
      },
    },
    options: {
      virtualHost: true,
    },
    computed: {
      inputVal: {
        set(val) {
          this.$emit('update:modelValue', val)
          this.$emit('change', val)
        },
        get() {
          return this.modelValue
        },
      },
    },
    methods: {
      changeInputFn() {
        setTimeout(() => {
          const query = uni.createSelectorQuery().in(this)
          query
            .select(`#${this.randomID}`)
            .boundingClientRect((rect) => {
              if (rect) {
                // 处于不可见状态,需要借助一个永远显示的 dom 进行处理
                if (rect.width == 0) {
                  uni.$emit('getInputWidth', this.inputVal)
                } else {
                  let rectWidth = rect.width
                  if (rectWidth > 150) {
                    rectWidth = 150
                  }
                  if (this.inputVal) {
                    this.inputWidth = rectWidth + 30 + 'px'
                  } else {
                    this.inputWidth = rectWidth + 5 + 'px'
                  }
                }
              }
            })
            .exec()
        }, 0)
      },
    },
    mounted() {
      uni.$on('returnInputWidth', (width) => {
        this.inputWidth = width
      })
    },
  }
</script>

<style lang="scss" scoped>
  .adaption-input {
    font-size: 28rpx;
  }
  .a--input {
    font: inherit;
    opacity: 0;
    position: fixed;
    top: 0;
    z-index: -1;
  }
</style>

InputWidthHelper.vue

js 复制代码
<template>
  <view :id="randomID" class="a--input">{{ inputValue || '请输入' }}</view>
</template>

<script>
  export default {
    inheritAttrs: false,
    data() {
      return {
        randomID: 'adaption_' + new Date().getTime(),
        inputValue: '',
      }
    },
    mounted() {
      uni.$on('getInputWidth', (text) => {
        this.inputValue = text

        this.$nextTick(() => {
          const query = uni.createSelectorQuery().in(this)
          query
            .select(`#${this.randomID}`)
            .boundingClientRect((rect) => {
              if (rect) {
                let rectWidth = rect.width
                if (rectWidth > 150) {
                  rectWidth = 150
                }
                if (text) {
                  uni.$emit('returnInputWidth', rectWidth + 30 + 'px')
                } else {
                  uni.$emit('returnInputWidth', rectWidth + 5 + 'px')
                }
              }
            })
            .exec()
        })
      })
    },
  }
</script>

<style lang="scss" scoped>
  .adaption-input {
    font-size: 28rpx;
  }
  .a--input {
    font: inherit;
    opacity: 0;
    position: fixed;
    top: 0;
    z-index: -1;
  }
</style>
相关推荐
weifont1 小时前
聊一聊Electron中Chromium多进程架构
javascript·架构·electron
大得3691 小时前
electron结合vue,直接访问静态文件如何跳转访问路径
javascript·vue.js·electron
水银嘻嘻3 小时前
12 web 自动化之基于关键字+数据驱动-反射自动化框架搭建
运维·前端·自动化
it_remember3 小时前
新建一个reactnative 0.72.0的项目
javascript·react native·react.js
小嘟嚷ovo4 小时前
h5,原生html,echarts关系网实现
前端·html·echarts
十一吖i4 小时前
Vue3项目使用ElDrawer后select方法不生效
前端
只可远观4 小时前
Flutter目录结构介绍、入口、Widget、Center组件、Text组件、MaterialApp组件、Scaffold组件
前端·flutter
周胡杰4 小时前
组件导航 (HMRouter)+flutter项目搭建-混合开发+分栏效果
前端·flutter·华为·harmonyos·鸿蒙·鸿蒙系统
敲代码的小吉米5 小时前
前端上传el-upload、原生input本地文件pdf格式(纯前端预览本地文件不走后端接口)
前端·javascript·pdf·状态模式
是千千千熠啊5 小时前
vue使用Fabric和pdfjs完成合同签章及批注
前端·vue.js