<style type="text/css">
.demo{width:100%; height:120px;text-align:center;}
.wrap{width:90px; height:90px; margin:auto;position:relative;}
.dice{width:90px; height:90px; background:url(
http://www.codefans.net/jscss/demoimg/201404/dice.png) no-repeat; cursor:pointer;}
.dice_1{background-position:-5px -4px}
.dice_2{background-position:-5px -107px}
.dice_3{background-position:-5px -212px}
.dice_4{background-position:-5px -317px}
.dice_5{background-position:-5px -427px}
.dice_6{background-position:-5px -535px}
.dice_t{background-position:-5px -651px}
.dice_s{background-position:-5px -763px}
.dice_e{background-position:-5px -876px}
p#result{text-align:center; font-size:16px}
p#result span{font-weight:bold; color:#f30; margin:6px}
#dice_mask{width:90px; height:90px; background:#fff; opacity:0; position:absolute; top:0; left:0; z-index:999}
</style>
<script type="text/javascript" src="
http://www.codefans.net/ajaxjs/jquery-1.9.1.min.js"></script><script type="text/javascript">
$(function(){
var dice = $("#dice");
dice.click(function(){
$(".wrap").append("<div id='dice_mask'></div>");//加遮罩
dice.attr("class","dice");//清除上次动画后的点数
dice.css('cursor','default');
var num = Math.floor(Math.random()*6+1);//产生随机数1-6
dice.animate({left: '+2px'}, 100,function(){
dice.addClass("dice_t");
}).delay(200).animate({top:'-2px'},100,function(){
dice.removeClass("dice_t").addClass("dice_s");
}).delay(200).animate({opacity: 'show'},600,function(){
dice.removeClass("dice_s").addClass("dice_e");
}).delay(100).animate({left:'-2px',top:'2px'},100,function(){
dice.removeClass("dice_e").addClass("dice_"+num);
$("#result").html("您掷得点数是<span>"+num+"</span>");
dice.css('cursor','pointer');
$("#dice_mask").remove();//移除遮罩
});
});
});
</script>
<div id="main"> <div class="demo">
<div class="wrap">
<div id="dice" class="dice dice_1"></div>
</div>
<p id="result">请直接点击上面的色子!</p>
</div>
</div>