html - 改变比例并调整外部内容

标签 html css css-transitions

I got a div and I need it to scale between 0 and 1, but the later content stays in the same place. How do I increase the div, and modify the content around it?
JSFiddle link

.transform {
  background-color: slategray;
}

.transform:hover~.div1 {
  transform: scaleY(0);
}

.div1 {
  transform: scaleY(1);
  background-color: royalblue;
  padding: 15px;
  transform: scaleY(1);
  transition: transform 2s;
  transform-origin: top center;
}

.div2 {
  background-color: springgreen;
  font-size: 20px;
}

img {
  height: 200px;
}
<div class="transform">TRANSFORM</div>
<div class="div1">
  <img src="https://via.placeholder.com/500">
</div>
<div class="div2">
  <span>TEXT</span>
</div>

最佳答案

如果您更改 .div1 的高度而不是缩放它,页面的其余部分将适应它。

.transform {
  background-color: slategray;
}

.transform:hover~.div1 {
  height:0; padding-top:0; padding-bottom:0; /* need to adjust height and padding */
}

.div1 {
  background-color: royalblue;
  padding: 15px;
  height: 200px;
  transition: padding-top 2s, padding-bottom 2s, height 2s; /* animate the padding too */
  transform-origin: top center;
}

.div2 {
  background-color: springgreen;
  font-size: 20px;
}

img {
  width:200px; height: 100%; vertical-align:top; /* and some adjustments here */
}
<div class="transform">TRANSFORM</div>
<div class="div1">
  <img src="https://via.placeholder.com/500">
</div>
<div class="div2">
  <span>TEXT</span>
</div>

希望这有帮助!


编辑:OP 的评论现在已经改变了问题,所以这里有一个新的代码片段来解决这个问题。

.transform {
  background-color: slategray;
}

.transform:hover~.div1 {
  transform: scaleY(0);
}

.transform:hover~.div2 {
  top:-230px;             /* Move div2 up by the height+padding of div1 */
}

.div1 {
  transform: scaleY(1);
  background-color: royalblue;
  padding: 15px;
  transform: scaleY(1);
  transition: transform 2s;
  transform-origin: top center;
}

.div2 {
  background-color: springgreen;
  font-size: 20px;
  position: relative;  /* use relative positioning */
  top: 0;              /* Initial position is where it normally is */
  transition: top 2s;
}

img {
  height: 200px;
  vertical-align:top;
}
<div style="margin-top: 300px">TEXT</div>
<div class="transform">TRANSFORM</div>
<div class="div1">
  <img src="https://via.placeholder.com/500">
</div>
<div class="div2">
  <span>TEXT</span>
</div>

关于html - 改变比例并调整外部内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54535460/

相关文章:

javascript - jQuery 选择器不适用于添加的类

html - css 转换规则中的问题

在我执行另一个脚本之前,jQuery 一直有效吗?

css - 如何使用高度变换 css3 进行叠加

html - Vuetify 和布局网格系统 : limiting vertically an element

java - 需要统一的浏览器引擎

html - 在可变宽度的 div 内对齐文本

javascript - 类似灯箱的叠加效果,可在单击时显示隐藏的 DIV

javascript - 在 Vue JS 中删除元素时的动画在开发中工作但不在生产中

javascript - 尝试使用 Indeed API 根据输入字段检索职位时出错