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>
相关推荐
编程零零七1 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
北岛寒沫2 小时前
JavaScript(JS)学习笔记 1(简单介绍 注释和输入输出语句 变量 数据类型 运算符 流程控制 数组)
javascript·笔记·学习
everyStudy2 小时前
JavaScript如何判断输入的是空格
开发语言·javascript·ecmascript
(⊙o⊙)~哦3 小时前
JavaScript substring() 方法
前端
无心使然云中漫步4 小时前
GIS OGC之WMTS地图服务,通过Capabilities XML描述文档,获取matrixIds,origin,计算resolutions
前端·javascript
Bug缔造者4 小时前
Element-ui el-table 全局表格排序
前端·javascript·vue.js
xnian_4 小时前
解决ruoyi-vue-pro-master框架引入报错,启动报错问题
前端·javascript·vue.js
麒麟而非淇淋5 小时前
AJAX 入门 day1
前端·javascript·ajax
2401_858120535 小时前
深入理解MATLAB中的事件处理机制
前端·javascript·matlab
阿树梢5 小时前
【Vue】VueRouter路由
前端·javascript·vue.js