请老师检查一下
来源:3-4 自由编程
xiao白做大事
2021-03-17 17:46:14
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>3-4自由编程</title>
<link rel="stylesheet" href="css/base.css">
<link rel="stylesheet" href="css/index.css">
<script src="js/flexible.js" type="module"></script>
</head>
<body>
<header class="header-container">
<div class="logo-container">
<div class="logo">
<img src="img/logo.png" alt="">
</div>
</div>
<div class="btn-container">
<button type="button" class="btn">下载APP</button>
</div>
</header>
</body>
</html>
base.css
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
font-size: 12px;
color: #5d655b;
}
a {
font-size: 12px;
color: #686868;
text-decoration: none;
}
button, input {
font-size: 12px;
border: none;
background: none;
outline: none;
}
[data-dpr="2"] body,
[data-dpr="2"] a,
[data-dpr="2"] input,
[data-dpr="2"] button {
font-size: 24px;
}
[data-dpr="3"] body,
[data-dpr="3"] a,
[data-dpr="3"] input,
[data-dpr="3"] button {
font-size: 36px;
}
li {
list-style: none;
}
img {
vertical-align: top;
border: none;
height: 100%;
}
index.css
/* --布局 start-- */
html,body {
width: 100%;
height: 100%;
}
/* header */
.header-container {
height: 2.5rem;
display: flex;
background: #ccc;
}
.logo-container {
flex:2;
}
.btn-container {
flex:1;
display: flex;
align-items: center;
padding-right: .75rem;
}
.logo-container,
.btn-container {
height: 100%;
}
/* --布局 end -- */
/* --组件 start-- */
/* logo */
.logo {
height: 100%;
padding-left: 1.25rem;
}
/* button */
.btn {
height: 1.75rem;
background-color: pink;
padding: .5rem;
border-radius: 1.125rem;
}
/* button */
/* --组件 end -- */
/* --内容 start-- */
/* --内容 end -- */
.js
'use strict';
var docEl = document.documentElement,
viewportEl = document.querySelector('meta[name="viewport"]'),
dpr = window.devicePixelRatio || 1,
maxWidth = 540,
minWidth = 320;
dpr = dpr >= 3 ? 3 : (dpr >= 2 ? 2 : 1);
docEl.setAttribute('data-dpr', dpr);
docEl.setAttribute('max-width', maxWidth);
docEl.setAttribute('min-width', minWidth);
var scale = 1 / dpr,
content = `width=device-width, initial-scale=${scale}, maximum-scale=${scale},minimum-scale=${scale}, user-scalable=no`;
if (viewportEl) {
viewportEl.setAttribute('content', content);
} else {
viewportEl = document.createElement('meta');
viewportEl.setAttribute('name', 'viewport');
viewportEl.setAttribute('content', content);
document.head.appendChild(viewportEl);
}
setRemUnit();
window.addEventListener('resize', setRemUnit);
function setRemUnit() {
var ratio = 18.75;
var viewWidth = docEl.getBoundingClientRect().width || window.innerWidth;
if (maxWidth && (viewWidth / dpr > maxWidth)) {
viewWidth = maxWidth * dpr;
} else if (minWidth && (viewWidth / dpr < minWidth)) {
viewWidth = minWidth * dpr;
}
docEl.style.fontSize = viewWidth / ratio + 'px';
}
1回答
同学你好,代码可做如下优化:
按钮居右显示,效果会更好,如下:


祝学习愉快!
相似问题