ios input 键盘收起页面底部有间隙没有自动还原

1.ios   input 键盘收起页面底部有间隙没有自动还原
解决办法: 最外层元素增加fixed 布局即可
2. ios  input输入 页面会放大,收起时页面不自动还原
解决办法 input 字体设置最小为16px即可
3.网页标签切换获取切换状态
document.addEventListener("visibilitychange", visibleChangeFun);
const visibleChangeFun= ()=>{
  if (document.visibilityState === "hidden"){
    //隐藏
  }
}


4.倒计时要用本地时间处理,不然切页面定时器不执行,时间不准确,以及离开标签页,或息屏用  document.visibilityState === "hidden" 判断,

 const [timeLeft, setTimeLeft] = useState<any>();
  const [startTime, setStartTime] = useState<any>(new Date().getTime());
  const hideTimeRef = useRef<any>();
  const timeLeftRef = useRef<any>();
  useEffect(() => {
    timer = setInterval(() => {
      setTimeLeft((prevTimeLeft: number) => prevTimeLeft - 1000);
    }, 1000);

    return () => {
      clearInterval(timer);
    };
  }, [expires]);

  useEffect(() => {
    if (expires) {
      setTimeLeft(expires);
      setStartTime(new Date().getTime());
    }
  }, [expires])

  useEffect(() => {
    if (timeLeft + startTime <= new Date().getTime()) {    也可以直接用timeLeft<=0 判断即可
      timer && clearInterval(timer);
    }
  }, [timeLeft]);

  useEffect(() => {
    //监听ios息屏
    const visibleChangeFun = function () {
      if (document.visibilityState === "hidden") {
        // 息屏时主动关闭定时器,并记录当前时间
        clearInterval(timer);
        hideTimeRef.current = getTimeNow()
        timeLeftRef.current = timeLeft
      } else {
        console.log('visible show :');
        // 用户打开手机进入页面时,记录当前时间,计算出息屏期间的时间,并重新启动定时器
        const showTime = getTimeNow();
        const diffTime = showTime - hideTimeRef.current;
        // 假设 countdown 是剩余倒计时时间
        // countdown -= diffTime;
        if (timeLeftRef.current - diffTime > 0) {
          setTimeLeft(timeLeftRef.current - diffTime);
          visibleStatusTimer = setInterval(function () {
            // 更新倒计时逻辑
            setTimeLeft((prevTimeLeft: number) => prevTimeLeft - 1000);
          }, 1000);
        } else {
          setTimeLeft(0);
        }
      }
    }
    document.addEventListener("visibilitychange", visibleChangeFun);
    return () => {
      document.removeEventListener("visibilitychange", visibleChangeFun);
      clearInterval(visibleStatusTimer);
    }
  }, [timeLeft])

  

 

 

 

 
posted @ 2024-10-30 21:00  红苹果学园  阅读(4)  评论(0编辑  收藏  举报