标题:求大神帮忙看一下问题所在!
只看楼主
cqhuangbo
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-12-25
 问题点数:0 回复次数:0 
求大神帮忙看一下问题所在!
<!DOCTYPE html>
<html xmlns="http://www.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>打字游戏</title>
<style>
    .divStyle{
        width:50px;
        height:50px;
        background-color:#000;
        color:#FFF;
        font-family:"微软雅黑";
        font-size:30px;
        text-align:center;
        line-height:50px;
        position:absolute;   
    }
</style>
<script type="text/javascript">
    var flg = true;
    function createDiv(){
        var info = new Array();
        for(var i=65,j=0;i<=90;i++,j++){
            info[j] = String.fromCharCode(i);//获取字母相对应的数字
        }
        //alert(info);
        var div = document.createElement("div");
        div.setAttribute("class","divStyle");
        div.innerHTML =info[Math.floor(Math.random()*info.length)];
        
        //系统的总宽度
        var sysWidth = Math.round(Math.random()*(window.innerWidth-50));
        div.style.left = sysWidth+"px";
        div.style.top = "0px";
        
        document.body.appendChild(div);
        if(flg){
            moveDiv();
            flg = false;        
        }
   
        window.setTimeout(createDiv,1000);
    }
   
   
    function moveDiv(){
        var divs = document.getElementsByTagName("div");
        for(var i=0;i<=divs.length-1;i++){
            var divTop = divs[i].style.top;
            var newTop = parseFloat(divTop.substring(0,divTop.length-2))+10;
            divs[i].style.top = newTop+"px";
            
            var sysHeight = window.innerHeight-50;
            if(newTop>=sysHeight){
                document.body.removeChild(divs[i]);
            }
        }
        window.setTimeout(moveDiv,10);
    }
   

    function begin(){
        window.onload = createDiv;
    }
   
    function end(){
        window.location.reload();
    }
   
    //获取键盘按键的ASSII
    window.onkeyup= function(event){
        var key = event.keyCode;
        var divInfo = String.fromCharCode(key);
        var divs = document.getElementsByTagName("div");
        for(var i=0;i<=divs.length-1;i++){
            if(divInfo == divs[i].innerHTML){
                document.body.removeChild(divs[i]);
                break;
            }
        }
        
    }
   
</script>
</head>

<body>
    <script>
        var input = document.createElement("INPUT");
        input.setAttribute("type","button");
        input.setAttribute("value","开始游戏");
        input.setAttribute("onclick","begin();");
        document.body.appendChild(input);
   
    </script>
</body>
</html>
搜索更多相关主题的帖子: javascript absolute position 微软雅黑 content 
2015-12-25 13:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-460557-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.403663 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved