如何从底部到顶部为条形图设置动画?

时间:2012-11-17 08:21:42

标签: javascript jquery css charts

我正在尝试制作一个条形图,并且条形图现在从上到下动画,但我希望它从下到上进行动画制作。我如何从下到上获取它?

CSS

.bar {
width:50px;
height:250px;
list-style-type:none;
}
.bar p {
background-color:#63F;
}
.bar span { 
padding-left:15px;
left:100%;  
}
#content ul {
list-style-type:none;
}
#content ul li {
float:left;
margin:50px;
}

HTML

<div id="content">
<ul>
  <li>
    <div class="bar">
      <p><span>50%</span></p>
    </div><br>
    Freshmen                    
  </li>
</ul>
</content>

的Javascript

<script src="../../jquery-1.6.4.min.js"></script>    
$(document).ready(function(){
    $(".bar").each(function(){
        var length = $(this).find("span").html();
        $(this).find("p").delay(500).animate({'height':length},3500,function(){$(this).find("span").fadeIn(1000);
        });
    });
});

"click here to see it in jsfiddle"

5 个答案:

答案 0 :(得分:3)

您可以将位置:绝对定义为。写得像这样:

.bar {
width:50px;
height:250px;
list-style-type:none;
    position:relative;
}
.bar p {
background-color:#63F;
    position:absolute;
    bottom:0;
    left:0;
}

选中此http://jsfiddle.net/6zqSS/1/

答案 1 :(得分:3)

http://jsfiddle.net/t4THf/5/

<强> CSS

.perfect
{
    padding-top:30px;
    width:500px;
    height:300px;
    position:relative;
    background-color:#efefef;
}
.perfect .bar
{
    float:left;
    position:relative;
    width:40px;
    height:100%;
    margin-left:5px;
}
.perfect .bar .slideup
{
    position:absolute;
    bottom:0px;
    width:40px;
    background-color:#fff000;
    border:1px solid #000;
}
.perfect .bar .slideup span
{
    display:block;
    position:absolute;
    margin:5px;
    top:-30px;
}

<强> HTML

<div class="perfect">
    <div class="bar">
        <div class="slideup">
            <span>20%</span>
        </div>
    </div>
    <div class="bar">
        <div class="slideup">
            <span>30%</span>
        </div>
    </div>
    <div class="bar">
        <div class="slideup">
            <span>10%</span>
        </div>
    </div>
    <div class="bar">
        <div class="slideup">
            <span>70%</span>
        </div>
    </div>
    <div class="bar">
        <div class="slideup">
            <span>100%</span>
        </div>
    </div>
    <div class="bar">
        <div class="slideup">
            <span>50%</span>
        </div>
    </div>
    <div class="bar">
        <div class="slideup">
            <span>60%</span>
        </div>
    </div>
    <div class="bar">
        <div class="slideup">
            <span>55%</span>
        </div>
    </div>
    <div class="bar">
        <div class="slideup">
            <span>70%</span>
        </div>
    </div>
    <div class="bar">
        <div class="slideup">
            <span>70%</span>
        </div>
    </div>
</div>

<强> JS

$(function(){
     $(".bar").each(function(){
        var length = $(this).find("span").html();
        $(this).find(".slideup").delay(500).animate({'height':length},
"slow",function(){$(this).find("span").fadeIn(1000);
        });
    });
});

点击此处查看demo

答案 2 :(得分:1)

您也可以使用css关键帧实现此目的,例如对于webkit http://jsfiddle.net/kudoslabs/YddaY/

CSS

dl{ position: relative ; height: 200px ; }
dt,dd{ position: absolute ; }
dd{ height: 70% ; width: 30px ; background: grey ; bottom: 20px ; -webkit-animation: animate-bar 1.25s 1 linear; }
dt{ bottom: 0 ; }
@-webkit-keyframes animate-bar{
    0% {height: 0% ; }
}​

HTML

<dl>
    <dt>Freshman</dt>    
    <dd>70%</dd>
</dl>​

答案 3 :(得分:0)

http://jsfiddle.net/chovy/6zqSS/2/

.bar {
    position: relative;
}


.bar p {
    position: absolute;
    bottom: 0;
}

答案 4 :(得分:0)

使用关键帧为css属性设置动画,例如宽度 设置元素的宽度(例如div)后,动画将开始播放

.animated-bar {
  animation: animateBar 0.25s ease-in;
  @keyframes animateBar {
    0% {
      width: 0;
    }
  }
}
<div class="animated-bar" style="width: 200px"><div>