[知了堂学习笔记]_JavaScript之DOM事件(许愿墙)

请关注“知了堂学习社区”,地址:http://www.zhiliaotang.com/portal.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>

    <style type="text/css">

        *{
            margin: 0;
            padding: 0;
        }
        html{
            width: 100%;
            height: 100%;
        }
        body{
            width: 100%;
            height: 100%;
            background: -webkit-linear-gradient(top,rgb(203,235,219),rgb(55,148,192));
        }
        #container{
            width: 100%;
            height: 80vh;
            position: relative;
            outline: 1px solid black;
            z-index: 0;
        }
        #input{
            width: 400px;
            height: 40px;
            display: block;
            margin: 30px auto;
            font-size: 18px;
            padding: 0 10px;
        }
        .items{
            width: 200px;
            position: absolute;
            padding: 30px 10px 10px;
            -webkit-border-bottom-left-radius: 20px 500px;
            -webkit-border-bottom-right-radius: 500px 30px;
            -webkit-border-top-right-radius: 5px 100px;
            box-shadow: 0 2px 10px 1px rgba(0,0,0,0.2);
        }
        .items p{
            width:100%;
            min-height: 80px;
            word-break: break-all;
        }
        .items span{
            float: right;
            margin: 10px 10px 0 0;
            color: white;
            font-size: 14px;
        }
        .items span:hover{
            cursor: pointer;
        }


    </style>

</head>
<body>

    <div id="container"></div>
    <input id="input" type="text" placeholder="编辑你的愿望,按回车键发送。">

    <script>

        window.onload = function(){
            var input = document.getElementById("input");
            var container = document.getElementById("container");

            input.onkeydown = function(e){
                if(e.keyCode == "13"){
                    var v = input.value;
                    if(v){
                        createItems(v);
                        input.value="";
                        input.setAttribute("placeholder","编辑你的愿望,按回车键发送。");
                    }
                }
            }

        }

        function createItems(text){

            var color = getRandomColor();
            var positionX = getRandomPosition("x");
            var positionY = getRandomPosition("y");
            var newDiv = '<div class="items" style = "background-color: '+color+';top:'+positionY+'px;left:'+positionX+'px";><p>'+text+'</p><span class="closeTabs">关闭</span></div>';
            container.innerHTML += newDiv;
        }

        function getRandomColor(){
            var c = "0123456789abcdef";
            var color = "";
            while(color.length<6){
                color += c[Math.floor(Math.random()*16)];
            }
            return "#"+color;
        }

        function getRandomPosition(p){
            var position;
            switch (p){
                case 'x':
                    position = Math.ceil(Math.random()*1700);
                    if(position>0 && position<container.clientWidth-220){
                        return position;
                    }else{
                       return getRandomPosition(p);
                    }
                    break;
                case 'y':
                    position = Math.ceil(Math.random()*600);
                    if(position>0 && position<container.clientWidth-260){
                        return position;
                    }else{
                        return getRandomPosition(p);
                    }
                    break;
                default :
                    break;
            }
        }

    </script>

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值