demo頁面的實現
代碼如下:
<div class="digg" id="digg">
<div class="good"> <a href="#">
<p>這個文檔不錯</p>
<div class="bar">
<div id="g_img" style="width:70%"></div>
</div>
<span class="num" id="num">70%(7000)</span> </a> </div>
<div class="bad"> <a href="#">
<p>文檔有待改進</p>
<div class="bar">
<div id="b_img" style="width:30%"></div>
</div>
<span class="num">30%(3000)</span> </a> </div>
</div>
主要一點就是通過百分比來控制g_img的寬度,至於css代碼就不貼出來了。
演示代碼:
代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Digg</title>
<style type="text/css">
* {
padding:0;
margin:0;
}
.digg {
height: auto;
width: 190px;
font-size:12px;
font-weight:normal;
}
.digg a {
display: block;
height: 48px;
width: 189px;
background-image: url(images/mark.gif);
background-repeat: no-repeat;
position: relative;
color: #000;
text-decoration: none;
}
.digg .good {
margin-bottom:10px;
margin-top:5px;
}
.digg .good a {
background-position: -189px 0px;
}
.digg .good a:hover {
background-position: 0px 0px;
}
.digg .bad a {
background-position: -378px 0px;
}
.digg .bad a:hover {
background-position: -567px 0px;
}
.digg a p {
padding-left:30px;
line-height:25px;
}
.digg .bar {
background-color: white;
height: 5px;
left: 20px;
overflow: hidden;
position: absolute;
text-align: left;
top: 30px;
width: 55px;
}
.bar #g_img {
background-image: url(images/sprites.gif);
background-repeat: repeat-x;
height: 5px;
width: auto;
}
.bar #b_img {
background-image: url(images/sprites.gif);
background-repeat: repeat-x;
height: 5px;
width: auto;
background-position: 0px -5px;
}
.num {
color: #333;
font: normal normal 100 10px/12px Tahoma;
left: 80px;
position: absolute;
top: 26px;
}
.digg .good .bar {
border: 1px solid #40A300;
}
.digg .bad .bar {
border: 1px solid #555;
}
</style>
</head>
<body>
<div class="digg" id="digg">
<div class="good"> <a href="#">
<p>這個文檔不錯</p>
<div class="bar">
<div id="g_img" style="width:70%"></div>
</div>
<span class="num" id="num">70%(7000)</span> </a> </div>
<div class="bad"> <a href="#">
<p>文檔有待改進</p>
<div class="bar">
<div id="b_img" style="width:30%"></div>
</div>
<span class="num">30%(3000)</span> </a> </div>
</div>
</body>
</html>
有了demo,其他實現起來就方便多了,首先是頁面獲取html,頁面第一次加載,用ajax獲取後台數據,不要直接顯示。(這裡為了方便測試,就用asp作為後台語言)
下面是asp輸出html代碼
代碼如下:
function getdigshtml()'輸出html
dim rsajax,sql,str,digsnum,undigsnum,digsnumall,digsper,undigsper
Set rsajax=server.CreateObject("adodb.recordset")
sql="select * from dig where id=1"
rsajax.open sql,conn,1,1
digsnum=rsajax("digs")
undigsnum=rsajax("undigs")
if isnull(digsnum) then digsnum=0
if isnull(undigsnum) then undigsnum=0
digsnumdigsnumall=digsnum+undigsnum
if digsnumall=0 then
digsper=0
undigsper=0
else
digsper=FormatNumber(cint(digsnum)/cint(digsnumall),3)*100
undigsper=FormatNumber(cint(undigsnum)/cint(digsnumall),3)*100
end if
str="<div class='good'>"
strstr=str&"<a href=JavaScript:isdigs('digs') >"
strstr=str&"<p>這個文檔不錯</p><div class='bar'><div id='g_img' style='width:"&digsper&"%'></div></div>"
strstr=str&"<span class='num'>"&digsper&"%("&digsnum&")</span>"
strstr=str&"</a></div><div class='bad'>"
strstr=str&"<a href=JavaScript:isdigs('undigs') >"
strstr=str&"<p>文檔有待改進</p><div class='bar'><div id='b_img' style='width:"&undigsper&"%'></div></div>"
strstr=str&"<span class='num'>"&undigsper&"%("&undigsnum&")</span>"
strstr=str&"</a></div>"
getdigshtml=str
end function
輸出完了 接下來就是前台獲取,這時候我們就要用到jquery ajax,為什麼不直接用ajax,原因很簡單,我不會。。。。。看一下jquery中ajax代碼,很簡單
代碼如下:
function getdigshtml()//獲取頂一下,踩一下html
{
$.ajax({
type:'POST',
url:'digg.asp',
data:'action=getdigshtml',
success:function(msg){
$("#digg").html(msg);
}
})
}
輸出完了,接下來一步就是digs和undigs的操作了,跟獲取html的代碼差不多
代碼如下:
function isdigs(digtype)//頂一下,踩一下操作
{
$.ajax({
type:'POST',
url:'digg.asp',
data:'action=digs&digtype='+digtype,
/* beforeSend:function(){
$("#vote").hide();
$("#loadings").show();
}, ajax請求顯示loading效果*/
success:function(msg){
switch (msg)
{
/* 後台用來判斷
case '1':
$("#loadings").hide();
$("#vote").show();
alert("請先登錄!");
break;
case '2':
$("#loadings").hide();
$("#vote").show();
alert("請先下載,再操作!");
break;
case '4':
$("#loadings").hide();
$("#vote").show();
alert("您已經參與過評價!");
break;*/
case '3':
getdigshtml();//重新綁定html
//$("#loadings").hide();
//$("#vote").show();
alert("謝謝你的參與!");
break;
default:
}
}
})
}
注釋掉的代碼:一部分是後台數據合法驗證用的,beforeSend這個方法是ajax請求執行前的相關操作(用於做loading比較多)
最後一步就是,每次數據提交完並且成功返回,getdigshtml()都要重新獲取綁定下,這樣就保證了數據的實時性。
演示代碼需要asp環境,大家可以測試下。
打包下載地址: http://www.jb51.net/jiaoben/28489.html