【发布时间】:2015-05-30 00:56:03
【问题描述】:
试图获取动画中变化的 div 的高度。获得该高度后,我需要在文本 h4 标记中将其显示为百分比。
目前动画由 CSS & jQuery 控制。问题是,当我使用 height() 时,它只返回 div 的第一个高度,并且不显示 div 高度的变化。我想我需要以某种方式迭代高度,但我不知道该怎么做。我已将其设置为在当前高度上显示 console.log。
HTML
<div id="brain">
<div id="brain-image"></div>
<div id="circle-percent">
<p class="retention-title">Memory Retention</p>
<h4 class="brain-percentage">20%</h4>
</div>
<div class="water"></div>
</div>
CSS
#brain {
background: #f5f5f5;
-webkit-border-radius: 0px;
-moz-border-radius: 0px;
border-radius: 0px;
width: 713px;
height: 608px;
position: relative;
z-index: 90;
margin-left: 120px;
}
.water {
background-color: #5bbd97;
background-position: top right;
position: absolute;
bottom: 0px;
width: 713px;
height: 608px;
-webkit-transition: all 10s ease-out;
-moz-transition: all 10s ease-out;
-o-transition: all 10s ease-out;
transition: all 10s ease-out;
-webkit-border-radius: 0px;
-moz-border-radius: 0px;
border-radius: 0px;
}
#brain-image {
background: url('img/brain.png');
width: 713px;
height: 608px;
display:block;
z-index: 99;
position:absolute;
top:0
}
JAVASCRIPT
// *** Animation based on scroll ***
jQuery(document).ready(function ($) {
jQuery(window).scroll(function() {
if (jQuery(window).scrollTop() > 600) {
jQuery('.water').css('height','80px');
}
});
// ** Getting the height here **
jQuery('.water').height(function() {
var currentWidth = $('.water').height();
console.log(currentWidth);
});
});
【问题讨论】:
-
您希望显示的高度在 div 减小的同时减小,还是只显示动画后 div 的最终高度?
-
我希望显示的高度在 div 减小的同时减小。谢谢。
标签: javascript jquery css animation css-animations