移动端输入框比较靠页面下部,输入时键盘会遮挡input输入框,处理方法.

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 19:15   1115   0

Android:input 输入框比较靠页面下部,输入时键盘会遮挡input输入框,处理方法.

使用系统自带键盘时,只安卓会遮盖输入框,原因: 页面高度由'视口高度'变成了'视口高度 - 键盘高度'.

使用antd-mobile money自带键盘时,安卓和iOS都会遮盖输入框.

解决方法:

1. input聚焦时,页面外框设置margin-bottom: 200px(键盘的高度),//使用系统自带键盘时不需要设置此项

2.计算可以使输入框进入可视区的scrollTop值,

3.设置容器scrollTop值让输入框进入可视区.

import React, { FC, useEffect, useState, useRef } from 'react';
import { InputItem as AntdInputItem } from 'antd-mobile';

const InputComponent: FC<any> = (props) => {
  const [amountInput, setAmountInput] = useState<number | undefined>(undefined);
 const [isFocus, setIsFocus] = useState<boolean>(false);
 // const windowRef = useRef<any>();
  
  useEffect(() => {
    if(props.gasId && props.oilNumber !== undefined) {
      getAmounts();
      // 记录初始窗口高度
      // windowRef.current = {
      //   outerHeight: window.outerHeight,
      //   innerHeight: window.innerHeight,
      //   topN: Math.floor(120/667*window.innerHeight) || 100
      // };
      // console.log('windowRef.current',windowRef.current); 
      // window.addEventListener('resize', windowResize); // 监听安卓手动关闭系统键盘操作
    }
    // return () => {
    //   window.removeEventListener('resize', windowResize);
    // }; 
  }, [props.gasId, props.oilNumber]);

  // const windowResize = (e) => {
  //   console.log('resize .outerHeight .innerHeight', e.target.outerHeight, e.target.innerHeight);
 //  // 监听安卓手动关闭系统键盘操作
  //   if(e.target.outerHeight < windowRef.current?.outerHeight || e.target.innerHeight < windowRef.current?.innerHeight) {
  //     setContainerScrolltop();
  //   }
  // }; 

  const setContainerScrolltop = () => {
    //使用系统自带键盘时,只需设置安卓的即可
  //const u = navigator.userAgent;
    //const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    //if(isAndroid) { 
  // 使用antd-mobile InputItem['money']的键盘时,安卓和iOS都需要设置scrollTop
  const container = document.querySelector('#container');//设置了overflow-y:auto;的外容器
    const inputContent = document.querySelector('#inputContent');//input元素(由于antd-mobile InputItem不能设置id,所以加了个外框)
  const wrapOffsetTop = container?.offsetTop; // 容器到页面顶部的距离
  const inputOffsetTop = inputContent?.offsetTop; // input框到页面顶部的距离
  const sTop = inputOffsetTop - wrapOffsetTop; //input框到容器顶部的距离,滚动距离设置为此值,即可使input刚好出现在容器顶部
    console.log('sTop', sTop);
  // 使用 antd-mobile money 自带的键盘时,安卓,iOS都需要滑动
      document.querySelector('#refuelDetailContainer')?.scrollTo(0, sTop);
      console.log('滑出高度',container?.scrollTop);
    // } 
  };

  const inputOnFocus = () => {
  setIsFocus(true);
  
    setTimeout(() => {
      setContainerScrolltop();
    }, 300);
  };
 
  const inputOnBlur = () => {
  setIsFocus(false);
    console.log('blur');
  };

 const inputChange = (val) => {
    const val = value;
    // 只能输入整数或2位小数
    const isMatch = val.match(/^(\d{0,12}|0)((\.{0,1}\d{0,2})?)$/);
    if(isMatch) {
      console.log('test true');
    }else {
      return false;
    }
    if(!String(val).length){
      setAmountInput(undefined);
      return false;
    }
    setAmountInput(val);
 }

 return (
  <div id="container" className={classnames(styles.container, isFocus ? styles.containerMargin : '')}>
   <div style={{width: '100%', height: '500px', background: 'lightblue'}}></div>
   <div  id="inputContent">
    <AntdInputItem
     type={'money'}
     placeholder=""
     value={amountInput}
     extra={'金额'}
     maxLength={15}
     onChange={inputChange}
     onFocus={inputOnFocus}
     onBlur={inputOnBlur}
     autoAdjustHeight
    >
     {amountInput !== undefined ? '' : ''}
    </AntdInputItem>
   </div>
  </div>
 ); 
}
export default InputComponent;


.containerMargin {
 margin-bottom: 200px;
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP