Wishing Wall

<!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> wishing wall </ 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 = "ones who did it ... click the OK button release">
<Button class = "comfirmBtn"> click Post </ button>
<script>
// Get the DOM element
let container = document.getElementsByClassName('container')[0];
let btn = document.getElementsByClassName('comfirmBtn')[0];
// default Wishing single text
let wishes = [ 'New Year's to good luck', 'could find a good job,' 'Japanese grade examination by', 'can your girlfriend', 'New Year's to good luck', 'could find a good job' 'Japanese grade examination by', 'can your girlfriend'];
// Wishing single random background color
let bgcColors = ['#96c2f1', '#bbe1f1', '#e3e197', '#f8b3d0', '#ffcc00', 'rgba(255,123,123,1)', 'yellowgreen', 'pink', 'skyblue'];
// Render function to add a css div element configured to do after the return div
let render = function (wish) {
// generate random data
let bgcColorIndex = Math.floor(Math.random() * bgcColors.length);
let randomTop = Math.floor (Math.random () * (630 - 150)); // high container vessel 150 subtracts
let randomLeft = Math.floor (Math.random () * (window.innerWidth - 175)); // left value subtracting the width of the box 150 on the basis of multi-minus 25px
let content = `<div class="item" style="top:${randomTop}px;left:${randomLeft}px;">${bgcColors[bgcColorIndex]}">${wish}<span>关闭</span></div>`;
container.innerHTML += content;
}
// when the page started to load the default Wishing single loaded onto the page
window.onload = function () {
// loop through all the default Wishing to render a single page to the top
for (let i = 0; i < wishes.length; i++) {
render(wishes[i]);
}
}
// publish new content function
let newContent = function () {
if (res.value !== '') {
let inputText = res.value;
render(inputText);
res.value = '';
} else {
alert ( 'can not be empty!');
}
}
// There are two ways to add a new desire and press Enter or click the Publish button
document.onkeyup = function (e) {
if (e.keyCode === 13) {
newContent ();
}
}
// add button click event to confirm
btn.onclick = newContent;

// entrusted to the entire document binding event by clicking the event but only click on the span element when its parent node is removed
document.onclick = function (e) {
if (e.target.nodeName === 'SPAN') {
container.removeChild(e.target.parentNode);
}
}

// set WishList drag
container.onmousedown = function (e) {
if (e.target.className === 'item') {
let offsetX = parseInt (e.target.style.left); // get the current x-axis distance
let offsetY = parseInt (e.target.style.top); // get the current of the y-axis distance
let innerX = event.clientX - offsetX; // get the mouse x wheelbase in the box
let innerY = event.clientY - offsetY; // Get the mouse in the box the wheelbase y
let that = e.target;
document.onmousemove = function (e) {
that.style.left = event.clientX - innerX + "px";
that.style.top = event.clientY - innerY + "px";
// boundary judgment
if (parseInt (that.style.left) <= 0) // left border
{
that.style.left = "0px";
}
if (parseInt (that.style.top) <= 0) // upper boundary
{
that.style.top = "0px";
}
if (parseInt (that.style.left)> = window.innerWidth - 175) // = the right boundary of the window width - slightly more box size minus 25px
{
that.style.left = window.innerWidth - 175 + "px";
}
if (parseInt (that.style.top)> = 480) // bottom boundary = 630--150
{
that.style.top = 480 + "px";
}
}

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


}



}
</script>
</body>

</html>

Guess you like

Origin www.cnblogs.com/dbda/p/11441695.html