jquery - Css 过渡不适用于子元素

标签 jquery html css css-transitions

我目前面临以下问题。 我有一个 <section>元素有一些 <div>作为 child 。 <section> 的高度设置为父 div 的百分比,<div>的高度也设置为 <section> 的百分比.

我正在使用 Jquery 向 <section> 添加一个类为了在两个高度之间过渡。问题是 children 没有更新他们的高度。

我不想使用 ye olde display: none 刷新 css和 removeAttr("style")技巧,因为我也希望将过渡应用于 child 。

我画了几个草图来帮助理解这个问题。

初始状态 Initial state 过渡后 After the transition

  • 编辑

<section>标签在初始状态下具有以下类: fullWidth halfHeight expandableTransition

.fullWidth {
  width: 100%;
}

.halfHeight {
  height: 48%;
}

.expandableTransition {
  -webkit-transition: height 1s; /* Safari */
  transition: height 1s;
}

child 的 CSS <div>是:

.myDiv {
  border: 1px solid;
  height: 82%;
  width: 300px;
  display: inline-block;
  vertical-align: middle;
}

Jquery为了过渡到新的高度添加的类很简单:

.expandedSection {
  height: 96%;
}

添加类的Jquery代码如下:

function expandSection() {
    $("#prioritySection").toggleClass("expandedSection");
}

最佳答案

我认为你使用了更多的动画类

希望对你有帮助

fiddle 在这里 https://jsfiddle.net/rameezrami/1axvcoeh/3/

<html>
<head>
<style>
.fullWidth {
  width: 100%;background: green;
  height: 48%;
}

.myDiv {
  border: 1px solid;
  height: 82%;
  width: 300px;
  display: inline-block;
  vertical-align: middle;
  background: red;
}

.expandedSection {
  height: 96%;
  -webkit-transition: height 1s; /* Safari */
  transition: height 1s;
}
</style>
</head>
<body>
<section class="fullWidth">

<div class="myDiv"></div>
<div class="myDiv"></div>
</section>
<input type="button" onclick="expandSection();" value="click to expand">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
function expandSection() {
    $(".fullWidth").toggleClass("expandedSection");
}
</script>
</body>
</html>

关于jquery - Css 过渡不适用于子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33669741/

相关文章:

javascript - 增加 jQuery 对象中的值

html - 如果没有 display "list-item"CSS display "list"有什么用?

html - 如何使用菜单和 iphone 在 ie7 上解决此问题?

html - 对齐网格底部的按钮

html - 为什么黑莓手机的 td 中没有正确的填充

javascript - Bootstrap 3 日期选择器 v4 : Select custom week automatically while moving the mouse

javascript - 使用 Javascript/jQuery 读取 JSON

javascript - 悬停过渡在加载后第一次点击时不起作用,但在此之后有效?

javascript - 如何在 jQuery 中检查表格行是否包含某个按钮?

javascript - 输入按钮 - Onclick 换色器