uniapp app 实现自适应宽度 input

核心原理

input 输入,存在一个 view 元素容纳输入内容,此时获取 view 元素的宽,将其设置为 input 的宽
特殊情况:回显的时候当前元素可能不存在,此时需要借助一个永远显示的元素进行宽度计算(InputWidthHelper.vue

adaption-input.vue

<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

<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. uniapp app 实现适应宽度 input

    2023-12-14 12:06:01       55 阅读
  2. el-dialog宽度适应

    2023-12-14 12:06:01       44 阅读
  3. Unity InputField宽度适应内容

    2023-12-14 12:06:01       44 阅读
  4. Python适应调整Excel的列宽度

    2023-12-14 12:06:01       68 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-14 12:06:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-14 12:06:01       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-14 12:06:01       82 阅读
  4. Python语言-面向对象

    2023-12-14 12:06:01       91 阅读

热门阅读

  1. sqlmap

    sqlmap

    2023-12-14 12:06:01      54 阅读
  2. uniapp todo list

    2023-12-14 12:06:01       66 阅读
  3. jvm基础知识总结

    2023-12-14 12:06:01       55 阅读
  4. Tomcat

    Tomcat

    2023-12-14 12:06:01      51 阅读
  5. 寻回初心:当初为什么学习计算机?

    2023-12-14 12:06:01       63 阅读
  6. SQL 算术运算符

    2023-12-14 12:06:01       54 阅读
  7. 36.@Import可以有几种用法?

    2023-12-14 12:06:01       79 阅读
  8. squid SSL https

    2023-12-14 12:06:01       50 阅读
  9. css做一条很细的分割线

    2023-12-14 12:06:01       66 阅读
  10. celery/schedules.py源码精读

    2023-12-14 12:06:01       53 阅读
  11. C语言第四十七弹---猜凶手

    2023-12-14 12:06:01       53 阅读
  12. Linux: 查看服务器的CPU信息

    2023-12-14 12:06:01       59 阅读