基于jquery實現(xiàn)動態(tài)豎向柱狀條特效
本文實例介紹了jquery實現(xiàn)的柱狀條,常用于數(shù)據(jù)統(tǒng)計,下面就對代碼做一下分享,并詳細介紹一下它的實現(xiàn)過程。
代碼如下:
<html>
<head>
<meta charset="gb2312">
<title>jquery柱狀條</title>
<style type="text/css">
.container{
width:20px;
height:50px;
border:1px solid #999;
font-size:10px;
float:left;
margin-left:5px;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script type="text/javascript">
$(function(){
var i=1;
$('#top').height(8);
$('#buttom').css('marginTop',42);
$('#buttom').css('background','#d6d6d6');
interid=setInterval(Showgao,0);
function Showgao(){
var oldH=$('#buttom').css('marginTop');
var h= oldH.substr(0,oldH.indexOf('px'));
$('#buttom').css('marginTop',h-1);
$('#buttom').height(i);
i++;
if(i==43){clearInterval(interid);}
}
var i1=1;
$('#top1').height(4);
$('#buttom1').css('marginTop',46);
$('#buttom1').css('background','red');
interid1=setInterval(Showgao1,1);
function Showgao1(){
var oldH=$('#buttom1').css('marginTop');
var h= oldH.substr(0,oldH.indexOf('px'));
$('#buttom1').css('marginTop',h-1);
$('#buttom1').height(i1);
i1++;
if(i1==30){clearInterval(interid1);}
}
});
</script>
</head>
<body>
<div class="container">
<div id="top">82%</div>
<div id="buttom"></div>
</div>
<div class="container">
<div id="top1" >62%</div>
<div id="buttom1"></div>
</div>
</body>
</html>
上面的代碼實現(xiàn)柱狀條數(shù)據(jù)的動態(tài)效果,下面介紹一下它的實現(xiàn)過程。
1.$(function(){}),當文檔結構完全加載完畢災區(qū)執(zhí)行函數(shù)中的代碼。
2.var i=1,聲明一個變量并賦初值為1,在后面會用到,這里暫時不做介紹。
3.$('#top').height(8),設置top元素的高度為8px。
4.$('#buttom').css('marginTop',42),設置buttom元素的上邊距為42px42+8=50恰好是容器元素的高度,這樣top元素就能夠恰好位于容器的頂端。
5.$('#buttom').css('background','#d6d6d6'),設置bottom元素的背景顏色。
6.interid=setInterval(Showgao,0),使用定時器函數(shù)不斷調用Showgao函數(shù)。
7.function Showgao(){},此函數(shù)沒執(zhí)行一次,都會相應的設置一次bottom元素的上外邊距和高度,從而達到,top元素始終位于頂部和柱狀條動態(tài)增加的效果。
8.var oldH=$('#buttom').css('marginTop'),獲取buttom元素的上外邊距的尺寸。9.var h= oldH.substr(0,oldH.indexOf('px')),獲取尺寸值的數(shù)字部分,比如"28px"中的28。
10.$('#buttom').css('marginTop',h-1),將上外邊距的尺寸減一。
11.$('#buttom').height(i),設置buttom元素的高度。
12.i++,i的值加1。
13.if(i==43){clearInterval(interid);},如果i的值等于43,說明buttom的高度值等于42px,恰好和top的高度和為50px,那么就停止定時器函數(shù)的執(zhí)行。
以上就是本文的詳細內容,希望對大家學習jquery程序設計有所幫助。
相關文章
jquery讀取xml文件實現(xiàn)省市縣三級聯(lián)動的方法
這篇文章主要介紹了jquery讀取xml文件實現(xiàn)省市縣三級聯(lián)動的方法,涉及jQuery操作XML文件及Ajax動態(tài)加載的技巧,需要的朋友可以參考下2015-05-05
jQuery+Ajax請求本地數(shù)據(jù)加載商品列表頁并跳轉詳情頁的實現(xiàn)方法
本文通過實例代碼給大家介紹了jQuery+Ajax請求本地數(shù)據(jù)加載商品列表頁并跳轉詳情頁,需要的朋友可以參考下2017-07-07
jQuery實現(xiàn)在下拉列表選擇時獲取json數(shù)據(jù)的方法
這篇文章主要介紹了jQuery實現(xiàn)在下拉列表選擇時獲取json數(shù)據(jù)的方法,主要涉及jQuery中getJSON方法的使用技巧,需要的朋友可以參考下2015-04-04

