3-29 请老师检查下代码,谢谢

来源:3-29 项目作业

不厌_

2021-07-26 12:31:03

相关代码:

​<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}

.box {
width: 20%;
/* 第一种盒子高度直接设置20vw,表示盒子高度为视口宽度的20% ,但是vw有兼容性问题*/
/* height: 20vw; */
height: 100px;
margin: 100px auto;
background-color: orange;
}

.box2 {
width: 3.75rem;
height: 3.75rem;
margin: 200px auto;
background-color: aqua;
}
</style>
</head>

<body>
<div class="box" id="box"></div>
<div class="box2" id="box2"></div>
<script>
// 第一种盒子高度直接设置20vw,表示盒子高度为视口宽度的20% ,但是vw有兼容性问题

// 第二种,每次改变屏幕大小时,获取视口宽度,再设置盒子的高度,比较麻烦
// const box = document.getElementById('box');
// window.onresize = function () {
// let vw = document.documentElement.clientWidth || window.innerWidth;
// box.style.height = vw * 0.2 + 'px';
// }

// 第三种,使用rem做单位,设置宽高
const set_rem = () => {
let vw = document.documentElement.clientWidth || window.innerWidth;
document.documentElement.style.fontSize = (vw / 375) * 20 + 'px';
}

set_rem();

window.onresize = set_rem;
</script>
</body>

</html>


写回答

1回答

好帮手慕然然

2021-07-26

同学你好,代码实现思路没问题,继续加油,祝学习愉快!

0

0 学习 · 15276 问题

查看课程