使用SCSS完成动态rem适配的自动计算

2020-01-05  本文已影响0人  percykuang

废话不多说,直接看代码。

目录和文件

├── css
│   ├── style.css (style.scss打包后生成)
│   └── style.css.map
├── index.html
└── scss
    └── style.scss

index.html结构

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <title>Document</title>
  <script>
    var pageWidth = window.innerWidth
    document.write(`<style>html{font-size:${pageWidth}px;}</style>`)
  </script>
  <link rel="stylesheet" href="./css/style.css">
</head>
<body>
  <div class="parent clearfix">
    <div class="child">box</div>
    <div class="child">box</div>
    <div class="child">box</div>
    <div class="child">box</div>
  </div>
</body>
</html>

/scss/style.scss源码

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-size: 16px;
}

$designWidth: 1000;

@function px2rem($px) {
  @return $px/$designWidth+rem;
}


.child {
  background: #ddd;
  width: px2rem(400);
  height: px2rem(200);
  margin: px2rem(50) px2rem(50);
  border: 1px solid #000;
  float: left;
  font-size: 1.2em;
}

.clearfix::after {
  content: '';
  display: block;
  clear: both;
}
上一篇下一篇

猜你喜欢

热点阅读