jquery - 如何将此 div 转换为底部并添加一些随机弹出方 block

标签 jquery css

我不知道如何将此 div 转换为底部。而且我不知道如何在随机方向生成会在 50 毫秒后消失的弹出方 block (简单的 div)。

我试图将此框 float 到底部,但没有帮助。

$("#button").click(function() {
  $('.transform').toggleClass('transform-active');
});
.field {
  height: 340px;
  width: 200px;
}

.box {
  background-color: #218D9B;
  height: 320px;
  width: 160px;
  float: bottom;
  background-image: url("img/tree.png");
  background-size: 100%;
}

.transform {
  -webkit-transition: all 300ms ease;
  -moz-transition: all 300ms ease;
  -o-transition: all 300ms ease;
  -ms-transition: all 300ms ease;
  transition: all 300ms ease;
}

.transform-active {
  height: 0px;
  width: 160px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="field">
  <div class="box transform"></div>
</div>

<input type="button" id="button" value="Click Me" />

我期待木头藏在地下的效果。

最佳答案

为了将div高度变换到底部,可以使用定位。 将 position: relative 添加到父级,然后您可以使用 position: absolutebottom: 20px 编辑框元素。

.field {
    height: 340px;
    width: 200px;
    position: relative;
}
.box {
    background-color: #218D9B;
    height: 320px;
    background-image: url(img/tree.png);
    background-size: 100%;
    bottom: 20px;
    position: absolute;
}

根据情况,另一种解决方案是使用 transform: scale() 而不是 height 来制作动画。
您只能垂直缩放并设置 transform-origin: bottom

.box {
    background-color: #218D9B;
    height: 320px;
    width: 160px;
    background-image: url(img/tree.png);
    background-size: 100%;
    transform-origin: bottom;
}
.transform-active {
    transform: scale(1, 0);
    width: 160px;
}

关于jquery - 如何将此 div 转换为底部并添加一些随机弹出方 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57875399/

相关文章:

php - javascript循环数组中的奇数/偶数

javascript - jquery添加的内联样式为空

html - 下拉菜单 css - 我不知道如何在不影响 child 的情况下将填充应用到 ul 父链接的底部

css - 如何在视口(viewport)中定位 HTML 表格中的 anchor

java - 富脸 : Trigger event after table is rendered

javascript - 使用 javascript 函数加载 View

html - 区分css中的单选按钮,我无法区分两个不同的单选按钮

javascript - 在mousemove上平移指定范围内的div位置

jquery - 在 jQuery UI Accordion 中定义事件展开状态

javascript - 如果 Obj 中存在数组,则获取数组项,如果不存在则获取字段