<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">html代码转链接
<title>掷子JS</title>
<style type="text/css">
.demo{width:760px; height:120px; margin:10px auto;}
.wrap{width:90px; height:90px; margin:120px auto 30px auto; position:relative}
.dice{width:90px; height:90px; background:url(defans/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="defans/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(){
            veClass("dice_t").addClass("dice_s");
        }).delay(200).animate({opacity: 'show'},600,function(){
            veClass("dice_s").addClass("dice_e");
        }).delay(100).animate({left:'-2px',top:'2px'},100,function(){
            veClass("dice_e").addClass("dice_"+num);
            $("#result").html("您掷得点数是<span>"+num+"</span>");
            dice.css('cursor','pointer');
            $("#dice_mask").remove();//移除遮罩
        });
    });
});
</script>
</head>
<body>
<div id="main">
  <h2 class="top_title">JS掷子代码</h2>
  <div class="demo">
          <div class="wrap">
              <div id="dice" class="dice dice_1"></div>
        </div>
        <p id="result">请直接点击上面的子!</p>
  </div>
</div>
</body>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。