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>
相关推荐
码叔义8 分钟前
Jsonpath 使用说明
android·开发语言·javascript
zl0_00_015 分钟前
xss-lab
前端·网络·xss
爱上你家菜包22 分钟前
Electron一小时快速上手
前端·javascript·electron
陈琦鹏22 分钟前
Vue3+Vite开发Electron桌面端问题记录
前端·javascript·electron
什么什么什么?1 小时前
el-input实现金额输入
javascript·vue.js·elementui
xiaoyustudiowww1 小时前
JSP + Servlet 实现 AJAX(纯JS版)
java·javascript·servlet
狼性书生2 小时前
uniapp vue3实现的一款数字动画调节器件,支持长按、单点操作,提供丝滑的增减动画效果
前端·vue.js·微信小程序·小程序·uni-app
小和尚敲代码2 小时前
推荐一款uniapp的日历插件魔改版可显示阳历阴历农历公历
uni-app·日历·uni-calendar
Jelena157795857922 小时前
爬虫获取微店商品快递费 item_feeAPI 接口的完整指南
开发语言·前端·爬虫
礼貌而已2 小时前
vue3+ts+uniapp+unibest 微信小程序(第二篇)—— 图文详解自定义背景图页面布局、普通页面布局、分页表单页面布局
微信小程序·uni-app·notepad++