大屏适配--React hook

前言

目前市场可视化大屏越来越多,百度的echarts等,或者阿里的DataV、百度的Suger、腾讯RayData等直接进行制作,适配问题大多数都是使用px2rem方法进行转换,大屏的适配基本是根据设计稿尺寸制作比例,适配各种屏幕的其实很多组件也大多是进行比例缩放;
前端的布局基本使用display: flex/gird布局方式;


代码如下:

import React, { useState, useEffect } from 'react';
import s from './index.less'

interface IndexProps {
  width: number,
  height: number
}

// 固定好16:9的宽高比,计算出最合适的缩放比,宽高比可根据需要自行更改
const getScaleHandler = (props: IndexProps) => {
  const { width, height } = props;
  let ww = window.innerWidth / width
  let wh = window.innerHeight / height
  return ww < wh ? ww : wh
}
const LargeScreen: React.FC<IndexProps> = (props) => {
  const { width, height } = props
  const [scale, setScale] = useState<number>(getScaleHandler({ width, height }));
  const setScaleHandler = () => {
    setTimeout(() => {
      // 获取到缩放比,设置它
      let scale = getScaleHandler({ width, height })
      console.log(scale);
      setScale(scale)
    }, 500)
  }
  useEffect(() => {
    window.addEventListener('resize', setScaleHandler)
  }, []);
  return (
    <div
      className={s['scale-box']}
      style={{
        transform: `scale(${scale}) translate(-50%, -50%)`,
        WebkitTransform: `scale(${scale}) translate(-50%, -50%)`,
        width: props.width,
        height: props.height
      }}
    >
      {props.children}
    </div>
  );
};

export default LargeScreen;
<LargeScreen width={1920} height={1080}><div></div></LargeScreen>

width=1920,height=1080


参考文章

直接使用的插件:
DataV-React
react-scale-box
react-big-screen
grid布局自动生成代码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值