H5-横屏提示显示组件

10 篇文章 0 订阅
该文章介绍了如何利用ReactHooks来封装一个横屏显示提示的兼容组件。通过监听`orientationchange`事件,结合状态管理,自动判断设备方向并在横屏时显示提示,同时处理了PC和移动端的屏幕滚动问题。
摘要由CSDN通过智能技术生成

目录

一、基于react-hooks封装横屏显示提示兼容组件

二、效果展示图

三、封装的代码、直接引入根路由、一键兼容使用


一、基于react-hooks封装横屏显示提示兼容组件

二、效果展示图

三、封装的代码、直接引入根路由、一键兼容使用

import React, { useEffect, useState } from "react";
import utils from "../../../utils/utils";
import "./index.less";

const LandscapeMessage = () => {
  const [isLandscape, setIsLandscape] = useState(false);

  const handleOrientationChange = () => {
    setIsLandscape(
      (window.orientation === 90 || window.orientation === -90) && !utils.IsPC()
    );
 // 禁止屏幕滑动,并隐藏滚动条
    if (isLandscape && !utils.IsPC()) {
        document.body.style.overflow = "hidden";
        document.documentElement.style.overflow = "hidden";
        document.body.style.touchAction = "none";
      } else {
        document.body.style.overflow = "auto";
        document.documentElement.style.overflow = "auto";
        document.body.style.touchAction = "auto";
      }
    
  };

  useEffect(() => {
    handleOrientationChange();
    window.addEventListener("orientationchange", handleOrientationChange);

    return () =>
      window.removeEventListener("orientationchange", handleOrientationChange);
  }, []);

  return (
    <div
      id="landscape-message"
      style={{ display: isLandscape ? "block" : "none" }}
    >
      <div className="conents_page">
        <img
          className="landscape_icon"
          src="https://images.artvrpro.com/images/image/landscape.png"
          alt=""
        />
        <h3 className="title">请将设备旋转至竖屏模式体验</h3>
      </div>
    </div>
  );
};

export default LandscapeMessage;
#landscape-message{ 
    position: fixed;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 1);
    z-index: 999;
    .conents_page{ 
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        font-size: 20px;

        .landscape_icon{ 
            width: 100px;
        }

        .title{ 
            color: rgb(245, 239, 239);
        }
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

web_icon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值