react大屏项目使用rem

一、添加rem.js文件

在公共文件夹中创建rem.js文件,配置rem计算功能

export default function() {
    (function(win, lib) {
        var doc = win.document;
        var docEl = doc.documentElement;
        var tid;
        win.flexible = lib.flexible || (lib.flexible = {});
        console.log(doc);
        console.log(docEl);
        function refreshRem(){
            var width = docEl.getBoundingClientRect().width>4600?docEl.getBoundingClientRect().width:4600
            var rem = width / 10;
            docEl.style.fontSize = rem + 'px';
        }
        win.addEventListener('resize', function() {
            clearTimeout(tid);
            tid = setTimeout(refreshRem, 300);
        }, false);
        win.addEventListener('pageshow', function(e) {
            if (e.persisted) {
                clearTimeout(tid);
                tid = setTimeout(refreshRem, 300);
            }
        }, false);
        refreshRem();
    })(global.window, global.window['lib'] || (global.window['lib'] = {}));
}

二、引入rem.js

在index.html(可选其他文件)引入rem.js

import React from 'react';
import { withRouter, Link } from 'react-router-dom';
import rem from '@utils/rem';
import './index.less';

class BaseComponents extends React.Component {
	componentDidMount = () => {
        rem();
    }
	render() {
	<div className={'mainBox'}>
		<div className={'conentBox'}>123</div>
	</div>}
}
export default withRouter(BaseComponents);

三、使用less编辑样式

@rem: 288rem; //  根据设计稿等比缩放;(设计稿宽度为2880px)

.mainBox{
  	height: 540/@rem;
   	width: 1920/@rem;
   	fontsize:18/@rem;// 根据设计稿计算(设计稿为18px)
   	padding: 10px;
  	background:lightblue;
   	.conentBox{
   		background:lightcoral;
   	}
  }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值