码迷,mamicode.com
首页 > 其他好文 > 详细

许愿墙

时间:2019-09-02 11:19:59      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:near   round   let   key   client   onkeyup   event   inter   font   

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

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>许愿墙</title>
<style>
* {
margin: 0;
padding: 0;
}

html {
height: 100%;
background: linear-gradient(0deg, rgb(171, 212, 238), deepskyblue);
}

body {
position: relative;
}

.container {
width: 100%;
height: 630px;
position: relative;
}

input[id="res"] {
width: 300px;
height: 40px;
position: fixed;
bottom: 30px;
left: 50%;
margin-left: -150px;
padding-left: 15px;
border: 1px solid #ccc;
outline: none;
border-radius: 5px;
}

.item {
width: 150px;
height: 150px;
position: absolute;
top: 100px;
left: 100px;
box-shadow: 0 2px 10px 1px rgba(0, 0, 0, 0.2);
padding: 10px;
border-radius: 15px/5px 10px 10px 0;
transform: skew(-1deg);
cursor: move;
}

.item span {
position: absolute;
right: 6px;
bottom: 4px;
font-size: 12px;
color: #eee;
cursor: pointer;
}

.comfirmBtn {
width: 100px;
height: 40px;
position: fixed;
bottom: 31px;
left: 64%;
border: none;
outline: none;
border: 1px solid;
border-radius: 10px;
cursor: pointer;
}
</style>
</head>

<body>
<div class="container"></div>
<input type="text" name="res" id="res" placeholder="随便说说吧...点击确认按钮发布">
<button class="comfirmBtn">点击发布</button>
<script>
let container = document.getElementsByClassName(‘container‘)[0];
let btn = document.getElementsByClassName(‘comfirmBtn‘)[0];
let wishes = [‘新年来好运‘, ‘能找一份好工作‘, ‘日语等级考试通过‘, ‘能交上女朋友‘, ‘新年来好运‘, ‘能找一份好工作‘, ‘日语等级考试通过‘, ‘能交上女朋友‘];
let bgcColors = [‘#96c2f1‘, ‘#bbe1f1‘, ‘#e3e197‘, ‘#f8b3d0‘, ‘#ffcc00‘, ‘rgba(255,123,123,1)‘, ‘yellowgreen‘, ‘pink‘, ‘skyblue‘];
let render = function (wish) {
let bgcColorIndex = Math.floor(Math.random() * bgcColors.length);
let randomTop = Math.floor(Math.random() * (630 - 150));
let randomLeft = Math.floor(Math.random() * (window.innerWidth - 175));
let content = `<div class="item" style="top:${randomTop}px;left:${randomLeft}px;">${bgcColors[bgcColorIndex]}">${wish}<span>关闭</span></div>`;
container.innerHTML += content;
}
window.onload = function () {
for (let i = 0; i < wishes.length; i++) {
render(wishes[i]);
}
}
let newContent = function () {
if (res.value !== ‘‘) {
let inputText = res.value;
render(inputText);
res.value = ‘‘;
} else {
alert(‘不能为空!‘);
}
}
document.onkeyup = function (e) {
if (e.keyCode === 13) {
newContent();
}
}
btn.onclick = newContent;
document.onclick = function (e) {
if (e.target.nodeName === ‘SPAN‘) {
container.removeChild(e.target.parentNode);
}
}
container.onmousedown = function (e) {
if (e.target.className === ‘item‘) {
let offsetX = parseInt(e.target.style.left);
let offsetY = parseInt(e.target.style.top);
let innerX = event.clientX - offsetX;
let innerY = event.clientY - offsetY;
let that = e.target;
document.onmousemove = function (e) {
that.style.left = event.clientX - innerX + "px";
that.style.top = event.clientY - innerY + "px";
if (parseInt(that.style.left) <= 0) {
that.style.left = "0px";
}
if (parseInt(that.style.top) <= 0) {
that.style.top = "0px";
}
if (parseInt(that.style.left) >= window.innerWidth - 175) {
that.style.left = window.innerWidth - 175 + "px";
}
if (parseInt(that.style.top) >= 480) {
that.style.top = 480 + "px";
}
}

document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
}


}



}
</script>
</body>

</html>

许愿墙

标签:near   round   let   key   client   onkeyup   event   inter   font   

原文地址:https://www.cnblogs.com/yeyuyuni/p/11445256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!