首先是先设置根字体大小,PC端通常是16px为根字体,移动端会有不一样的,根据状况来设置css
document.querySelector('html').style.fontSize = getFontSize(); //支持拉动屏幕大小时监听屏宽等比例缩放 window.onresize = function () { document.querySelector('html').style.fontSize = getFontSize(); } function getFontSize() { return (document.documentElement.clientWidth || document.body.clientWidth) / 120 + 'px'; //这里1920/120=16px }
<style scoped lang="scss"> @function torem($px) { // $px为须要转换的字号 @return $px / 16px * 1rem; //16px为根字体大小 } //这个方法为了避免用本身一个个去计算rem是多少,其实也能够本身算好rem直接写 button { position: absolute; left: 50%; margin-left: torem(-33px); bottom: torem(20px); width: torem(67px); height: torem(50px); } </style>