CSS滑动式条状图表如何实现?实例

本文介绍了一种利用CSS实现的滑动式条状图表,通过设置HTML元素样式及背景图片,可以直观展示数据变化与对比关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 在前面的文章中,我们介绍了如何实现CSS基本条状图表: http://www.52css.com/article.asp?id=362。现在我们介绍如何实现CSS滑动式条状图表。效果如下图:


  这种形式的条状图在实际应用中有着非常好的表现力,有现实使用意义。我们可以通过这样的方式形象的表述出数值的变化与对比关系。
  这样的CSS滑动式条状图表如何实现?我们看下面的代码:

div css xhtml xml Example Source Code Example Source Code [www.52css.com]
<dl>
    <dt>Love Life</dt>
    <dd>
        <div style="width:25%;"><strong>25%</strong></div>
    </dd>
    <dt>Education</dt>
    <dd>
        <div style="width:55%;"><strong>55%</strong></div>
    </dd>
    <dt>War Craft 3 Rank</dt>
    <dd>
        <div style="width:75%;"><strong>75%</strong></div>
    </dd>
</dl>

  我们了解到,dl 标签是HTML自定义列表,dt 表示列表元素的标题,dd 描述列表中元素的内容。在dd中,我们设置了容器div,以及子元素strong。我们将通过CSS对其表现进行定义,使其成为我们想要的CSS滑动式条状图表。我们看下面的CSS代码:

div css xhtml xml Example Source Code Example Source Code [www.52css.com]
    dl { 
        margin: 0; 
        padding: 0;                     
    }
    dt { 
        position: relative; /* IE is dumb */
        clear: both;
        display: block; 
        float: left; 
        width: 104px; 
        height: 20px; 
        line-height: 20px;
        margin-right: 17px;              
        font-size: .75em; 
        text-align: right; 
    }
    dd { 
        position: relative; /* IE is dumb */
        display: block;                 
        float: left;     
        width: 197px; 
        height: 20px; 
        margin: 0 0 15px; 
        background: url("g_colorbar.jpg"); 
     }
     * html dd { float: none; } 
    /* IE is dumb; Quick IE hack, apply favorite filter methods for 
    wider browser compatibility */

     dd div { 
        position: relative; 
        background: url("g_colorbar2.jpg"); 
        height: 20px; 
        width: 75%; 
        text-align:right; 
     }
     dd div strong { 
        position: absolute; 
        right: -5px; 
        top: -2px; 
        display: block; 
        background: url("g_marker.gif"); 
        height: 24px; 
        width: 9px; 
        text-align: left;
        text-indent: -9999px; 
        overflow: hidden;
     }

  在这些代码中,实现滑动式条状图表的思路在于:

div css xhtml xml Example Source Code Example Source Code [www.52css.com]
  dd中我们设置了背景g_colorbar.jpg。
  在dd的div容器中我们设置了背景图片g_colorbar2.jpg。并对此容器的width宽度进行设置。
  在div容器内部的strong里我们设置了滑动的“游标”g_marker.gif。或许大家有疑问,为什么看不到strong元素内的文字?因为设置了text-indent: -9999px; overflow: hidden;将其隐藏起来了。


  我们的目标是实现图形化数字,更直观而易于理解。所以数字并不是非常重要了。
  我们看最终的效果:
<!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=gb2312" />
<title>www.52css.com</title>
<style type="text/css">
    dl {
        margin: 0;
        padding: 0;                    
    }
    dt {
        position: relative; /* IE is dumb */
        clear: both;
        display: block;
        float: left;
        width: 104px;
        height: 20px;
        line-height: 20px;
        margin-right: 17px;             
        font-size: .75em;
        text-align: right;
    }
    dd {
        position: relative; /* IE is dumb */
        display: block;                
        float: left;    
        width: 197px;
        height: 20px;
        margin: 0 0 15px;
        background: url("attachments/month_0703/363g_colorbar.jpg");
     }
     * html dd { float: none; }
    /* IE is dumb; Quick IE hack, apply favorite filter methods for
    wider browser compatibility */

     dd div {
        position: relative;
        background: url("attachments/month_0703/363g_colorbar2.jpg");
        height: 20px;
        width: 75%;
        text-align:right;
     }
     dd div strong {
        position: absolute;
        right: -5px;
        top: -2px;
        display: block;
        background: url("attachments/month_0703/363g_marker.gif");
        height: 24px;
        width: 9px;
        text-align: left;
        text-indent: -9999px;
        overflow: hidden;
     }
</style>
</head>
<body>
<dl>
    <dt>Love Life</dt>
    <dd>
        <div style="width:25%;"><strong>25%</strong></div>
    </dd>
    <dt>Education</dt>
    <dd>
        <div style="width:55%;"><strong>55%</strong></div>
    </dd>
    <dt>War Craft 3 Rank</dt>
    <dd>
        <div style="width:75%;"><strong>75%</strong></div>
    </dd>
</dl>
</body>
</html> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值