职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 458|回复: 7

[原创]纯css模拟液晶式数字显示

[复制链接]
走失的猫咪 发表于 2011-7-18 09:09 | 显示全部楼层 |阅读模式
首先是借鉴了yiminghe大虾的创意和思路(http://www.iteye.com/topic/705529),可惜看到帖子的时候里面的demo已经打不开了,所以决定自己也做一个。

我觉得他画的数字不好看,所以我还是按照经典样式做的。另外变换数字的原理和思路也他的也不一样,我是全都交给css来完成了,我个人认为渲染效率更高些,实现起来也更简单些。

特点如下:
1,纯css实现,无图片
2,以em为长度单位,支持缩放
3,以不同类名来控制显示数字,方便控制

已知问题:
1,在ff下设为奇数大小时比较杯具,因为ff在计算em长度时四舍五入问题导致棱角与线条会分裂开,ie反到不会。不过这个问题是可以妥善解决的,以后再说了。

  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">   
<html xmlns="http://www.w3.org/1999/xhtml">   
<head>   
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
<meta http-equiv="Content-Language" content="zh-CN" />   
<title></title>   
<style type="text/css" title="">   
/*  
* Description:  
* Author: dragonball  
* Create Date:2011-2-22  
* Copyright 2011  
* Dual licensed under the MIT and GPL licenses.  
*/  
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,blockquote,th,td{margin:0;padding:0;}   
#mydemo{margin:100px auto;width:600px;}   
  
.clock{font-size:18px;width:9em;height:14em;border:1px solid #ddd;position:relative;}   
.clock div{position: absolute;border-style:solid;}   
.clock .l,.clock .r,.clock .u,.clock .d{width:0;height:0;overflow:hidden;}   
  
.clock .v{width:0;height:5em;border-width:0 0.5em;border-color:#565656;}   
.clock .v .u{border-style:dotted dotted solid dotted;border-width:0 0.5em 0.5em;border-color:transparent transparent #565656 transparent;top:-0.5em;left:-0.5em;}   
.clock .v .d{border-style:solid dotted dotted dotted;border-width:0.5em 0.5em 0 0.5em;border-color:#565656 transparent transparent transparent;left:-0.5em;bottom:-0.5em}   
  
.clock .h{width:5.5em;height:0;border-width:0.5em 0;border-color:#565656;}   
.clock .h .l{border-style:dotted solid dotted dotted;border-width:0.5em 0.5em 0.5em 0;border-color:transparent #565656 transparent transparent;top:-0.5em;left:-0.5em;}   
.clock .h .r{border-style:dotted dotted dotted solid;border-width:0.5em 0 0.5em 0.5em;border-color:transparent transparent transparent #565656;top:-0.5em;right:-0.5em;}   
  
.clock .n1{left:0.5em;top:1.5em;}   
.clock .n2{left:0.5em;top:7.5em;}   
.clock .n4{left:7.5em;top:7.5em;}   
.clock .n5{left:7.5em;top:1.5em;}   
  
.clock .n6{top:0.4em;left:1.8em;}   
.clock .n3{top:12.5em;left:1.8em;}   
.clock .n7{top:6.5em;left:1.8em;}   
  
.c1 .n1,.c1 .n2,.c1 .n3,.c1 .n6,.c1 .n7{display:none}   
.c2 .n1,.c2 .n4{display:none}   
.c3 .n1,.c3 .n2{display:none}   
.c4 .n2,.c4 .n3,.c4 .n6{display:none}   
.c5 .n2,.c5 .n5{display:none}   
.c6 .n5{display:none}   
.c7 .n1,.c7 .n2,.c7 .n3,.c7 .n7{display:none}   
.c8{}   
.c9 .n2{display:none}   
.c0 .n7{display:none}   
  
</style>   
<script type="text/javascript" language="javascript" >   
var num=0,size=1;   
  
function addNumber(){   
    var clock=document.getElementById("mydemo").children[0];   
    clock.className="clock c"+(++num % 10);   
    return false;   
}   
  
function resize(){   
    var clock=document.getElementById("mydemo").children[0];   
    clock.style.fontSize=(++size % 20)+"px";   
    return false;   
}   
</script>   
</head>   
  
<body>   
<div id="mydemo">   
    <div class="clock c0">   
  
        <div class="v n1"><div class="u"></div><div class="d"></div></div>   
        <div class="v n2"><div class="u"></div><div class="d"></div></div>   
        <div class="h n3"><div class="l"></div><div class="r"></div></div>   
        <div class="v n4"><div class="u"></div><div class="d"></div></div>   
        <div class="v n5"><div class="u"></div><div class="d"></div></div>   
        <div class="h n6"><div class="l"></div><div class="r"></div></div>   
        <div class="h n7"><div class="l"></div><div class="r"></div></div>   
    </div>   
</div>   
  
<input type="button" value="改变大小" onclick="resize()" />   
<input type="button" value="数字累加" onclick="addNumber()" />   
</body>   
  
</html>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-CN" />
<title></title>
<style type="text/css" title="">
/*
* Description:
* Author: dragonball
* Create Date:2011-2-22
* Copyright 2011
* Dual licensed under the MIT and GPL licenses.
*/
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,blockquote,th,td{margin:0;padding:0;}
#mydemo{margin:100px auto;width:600px;}

.clock{font-size:18px;width:9em;height:14em;border:1px solid #ddd;position:relative;}
.clock div{position: absolute;border-style:solid;}
.clock .l,.clock .r,.clock .u,.clock .d{width:0;height:0;overflow:hidden;}

.clock .v{width:0;height:5em;border-width:0 0.5em;border-color:#565656;}
.clock .v .u{border-style:dotted dotted solid dotted;border-width:0 0.5em 0.5em;border-color:transparent transparent #565656 transparent;top:-0.5em;left:-0.5em;}
.clock .v .d{border-style:solid dotted dotted dotted;border-width:0.5em 0.5em 0 0.5em;border-color:#565656 transparent transparent transparent;left:-0.5em;bottom:-0.5em}

.clock .h{width:5.5em;height:0;border-width:0.5em 0;border-color:#565656;}
.clock .h .l{border-style:dotted solid dotted dotted;border-width:0.5em 0.5em 0.5em 0;border-color:transparent #565656 transparent transparent;top:-0.5em;left:-0.5em;}
.clock .h .r{border-style:dotted dotted dotted solid;border-width:0.5em 0 0.5em 0.5em;border-color:transparent transparent transparent #565656;top:-0.5em;right:-0.5em;}

.clock .n1{left:0.5em;top:1.5em;}
.clock .n2{left:0.5em;top:7.5em;}
.clock .n4{left:7.5em;top:7.5em;}
.clock .n5{left:7.5em;top:1.5em;}

.clock .n6{top:0.4em;left:1.8em;}
.clock .n3{top:12.5em;left:1.8em;}
.clock .n7{top:6.5em;left:1.8em;}

.c1 .n1,.c1 .n2,.c1 .n3,.c1 .n6,.c1 .n7{display:none}
.c2 .n1,.c2 .n4{display:none}
.c3 .n1,.c3 .n2{display:none}
.c4 .n2,.c4 .n3,.c4 .n6{display:none}
.c5 .n2,.c5 .n5{display:none}
.c6 .n5{display:none}
.c7 .n1,.c7 .n2,.c7 .n3,.c7 .n7{display:none}
.c8{}
.c9 .n2{display:none}
.c0 .n7{display:none}

</style>
<script type="text/javascript" language="javascript" >
var num=0,size=1;

function addNumber(){
        var clock=document.getElementById("mydemo").children[0];
        clock.className="clock c"+(++num % 10);
        return false;
}

function resize(){
        var clock=document.getElementById("mydemo").children[0];
        clock.style.fontSize=(++size % 20)+"px";
        return false;
}
</script>
</head>

<body>
<div id="mydemo">
        <div class="clock c0">

                <div class="v n1"><div class="u"></div><div class="d"></div></div>
                <div class="v n2"><div class="u"></div><div class="d"></div></div>
                <div class="h n3"><div class="l"></div><div class="r"></div></div>
                <div class="v n4"><div class="u"></div><div class="d"></div></div>
                <div class="v n5"><div class="u"></div><div class="d"></div></div>
                <div class="h n6"><div class="l"></div><div class="r"></div></div>
                <div class="h n7"><div class="l"></div><div class="r"></div></div>
        </div>
</div>

<input type="button" value="改变大小" onclick="resize()" />
<input type="button" value="数字累加" onclick="addNumber()" />
</body>

</html>



紫衿 发表于 2011-7-18 09:09 | 显示全部楼层
说白了 就是巧用边框

郁闷小男人 发表于 2011-7-18 09:09 | 显示全部楼层
恩,是的,原理一点都不复杂。

gz-vps 发表于 2011-7-18 09:10 | 显示全部楼层
虽然不复杂,但是有这个耐心写出来真的很不容易。挺楼主一下呵呵

已经来了吗 发表于 2011-7-18 09:10 | 显示全部楼层
呵呵,谢谢!

hxy 发表于 2011-7-18 09:10 | 显示全部楼层
变态  这有会功夫PS做一个字了 
好钢要用在刀刃上 


木已 发表于 2011-7-19 10:42 | 显示全部楼层
貌似我没看懂那~~~
yoyo 发表于 2011-7-20 09:52 | 显示全部楼层
挺好啊!!!!!!!!!!!!!!!
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-3-29 04:05 , Processed in 0.127460 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表