代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>元素自适应容器</title>
<style>
body {
text-align: center;
font-size: 18px;
padding-top: 2vh;
}
.responsive-ref1,
.responsive-ref2 {
width: 40vw;
height: 50vh;
border: 1px solid;
padding: 20px;
margin: 40px 0 20px 0;
box-sizing: border-box;
}
.responsive-ref1 {
margin-right: 20px;
display: inline-block;
vertical-align: top;
position: relative;
}
.responsive-ref2 {
position: static;
vertical-align: top;
display: inline-flex;
align-items: center;
justify-content: center;
}
.flex1,
.flex2 {
flex: 0 0 auto;
width: 300px;
display: flex;
flex-flow: row wrap;
gap: 15px;
}
.item {
flex: 0 0 auto;
width: 90px;
height: 50px;
line-height: 50px;
text-align: center;
border: 1px solid;
box-sizing: border-box;
font-size: 16px;
cursor: pointer;
}
.item:hover {
font-size: 26px;
background-color: antiquewhite;
}
</style>
</head>
<body>
<h1>元素尺寸自适应容器</h1>
<p>
核心思路:根据元素和其参照节点的宽高计算出合适的缩放比例(计算逻辑与
<a
href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/object-fit"
target="_blank"
>
object-fit:contain </a
>一样),然后缩放目标元素
</p>
<p>下面两个容器盒子的宽高单位分别使用vw和vh,缩放浏览器窗口可以查看效果</p>
<p>
左边的盒子使用transform进行缩放(建议使用),右边的盒子通过修改目标DOM树中所有节点的属性进行缩放
</p>
<div class="responsive-ref1">
<p></p>
<div class="flex1">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
</div>
</div>
<div class="responsive-ref2">
<div class="flex2">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
</div>
</div>
<script src="./responsive1.js"></script>
<script src="./responsive2.js"></script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。