前端小练习:原生JS实现许愿墙

原生js实现许愿墙

实现效果:
在这里插入图片描述
HTML代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./css/index.css">
</head>

<body>
    <div class="container">
        <!-- <div class="item">
            世界和平
            <span class="close">X</span>
        </div> -->
    </div>
    <input type="text" class="txt" placeholder="许个愿望吧">
    <script src="./js/index.js"></script>
</body>

</html>

CSS代码:

* {
    padding: 0;
    margin: 0;
}

html {
    background-color: lightcyan;
    width: 100%;
}

.txt {
    position: absolute;
    bottom: 30px;
    width: 250px;
    /* 水平居中 */
    left: 0;
    right: 0;
    /* auto吸收剩余空间 */
    margin: auto;
    height: 30px;
    border: 1px solid #ccc;
    border-radius: 5px;
    /* 首行缩进 */
    text-indent: 1em;
    font-size: 13px;
}

.item {
    width: 170px;
    height: 170px;
    padding: 20px;
    box-sizing: border-box;
    /* border: 1px solid black; */
    background: red;
    border-radius: 5px;
    border-bottom-right-radius: 80px 20px;
    /* 盒子阴影 */
    box-shadow: 0 0 5px 2px rgba(0, 0, 0, .2);
    position: absolute;
    left: 100px;
    top: 80px;
    /* 在文字处换行 */
    word-wrap: break-word;
    overflow: hidden;
}

.item .close {
    position: absolute;
    right: 5px;
    top: 2px;
    cursor: pointer;
}

javascrip代码:

var container = document.querySelector(".container");
var zindex = 1;
// 创建一个愿望
function creatWish(words) {
    var div = document.createElement("div");
    // 设置文字
    div.innerHTML = words;
    div.className = "item";

    // 点击事件,让下层的盒子成为第一层的盒子
    div.onclick = function() {
            div.style.zIndex = zindex;
            zindex++;
        }
        // 关闭按钮
    var span = document.createElement("span");
    span.className = "close";
    span.innerHTML = "X";
    div.appendChild(span);


    // 颜色随机
    div.style.background = `rgb(${getRandom(150,256)},${getRandom(150,256)},${getRandom(150,256)})`;
    // 位置随机
    var max_X = window.innerWidth - div.offsetWidth;
    var max_Y = window.innerHeight - div.offsetWidth;
    div.style.left = `${getRandom(0,max_X)}px`;
    div.style.top = `${getRandom(0,max_Y)}px`;
    // 关闭事件
    span.onclick = function() {
        container.removeChild(div);
    }
    container.appendChild(div);

}
//产生随机数
function getRandom(min, max) {
    var dec = max - min;
    return Math.floor(Math.random() * dec + min);

}
// 生成默认愿望
function init() {
    var wishes = [
        "高考加油",
        "生日快乐",
        "必胜"
    ]
    for (let i = 0; i < wishes.length; i++) {
        var wish = wishes[i];
        creatWish(wish);
    }
}

//文本框的回车事件
var txt = document.querySelector(".txt");
txt.onkeydown = function(e) {
    if (e.keyCode !== 13) { //判断是否回车
        return;
    }
    if (txt.value) {
        creatWish(txt.value);
    } else {
        init();
    }
}
  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值