我正在构建一个网格UI,用户应该能够点击单元格来缩放它们。如何缩放多个div并留在容器内?
所需的行为:
- 细胞应不向外扩展的容器和被裁切*。
- 单元格可以重叠其他单元格(缩放时)。
- (只有一个单元需要一次缩放,这不是我们主要关心的问题)。
我的方法:
很显然,我可以transform: scale() translate()
在同一时间,以达到期望的结果。 您可以使用单元格N
(translateY(22px)
)查看此内容。
问题:
这种方法不能很好地扩展到9个细胞(更不用说90,这是我的实际使用情况)。
容器必须是overflow: hidden
。滚动等不是一种选择。
我的问题(S):
这感觉就像在解决一些必须有人已经解决了已经编程一个相当粗糙的尝试。
有没有更好的方法来构建这样的用户界面?(我打开插件或其他方法)。
如果不是,在jQuery中编写脚本的好方法是什么?
$(function() {
$(".cell").click(function() {
$(this).toggleClass("zoom-in");
});
});
.grid {
width: 300px;
height: 300px;
background: gray;
overflow: hidden;
}
.cell {
background: tomato;
width: 30%;
height: 30%;
margin: 5px;
float: left;
text-align: center;
z-index: 999;
transition: all 0.2s ease-in-out;
}
.zoom-in {
transform: scale(2);
transition: all 0.2s ease-in-out;
}
#nord {
background-color: white;
}
#nord.zoom-in {
transform: scale(2) translateY(22px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div> Click white and red cells to scale. Click again to revert.</div>
<div class="grid">
<div class="cell">NW</div>
<div id="nord" class="cell">N</div>
<div class="cell">NO</div>
<div class="cell">W</div>
<div class="cell">C</div>
<div class="cell">O</div>
<div class="cell">SW</div>
<div class="cell">S</div>
<div class="cell">SO</div>
</div>
感谢的@Akshay,它的工作原理。不幸的是,它禁用第二次点击(完全恢复“缩放”)。 –