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

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;
}

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值