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环境,大家可以测试下。
打包下载地址:
https://www.jb51.net/jiaoben/28489.html