1 Star 0 Fork 0

郑宝迪/responsive-child-element

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
index.html 2.85 KB
一键复制 编辑 原始数据 按行查看 历史
郑宝迪 提交于 2024-06-08 17:21 . init
<!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>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zheng-baodi/responsive-child-element.git
git@gitee.com:zheng-baodi/responsive-child-element.git
zheng-baodi
responsive-child-element
responsive-child-element
master

搜索帮助