JavaScript实现许愿墙效果
JavaScript实现许愿墙效果:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body p {
margin: 0;
}
.paper {
position: fixed;
left: 200px;
top: 200px;
width: 200px;
height: 200px;
padding: 20px;
background: yellow;
border-radius: 5px;
box-sizing: border-box;
cursor: pointer;
}
.close {
display: block;
width: 20px;
height: 20px;
border: 1px solid #ccc;
border-radius: 50%;
position: absolute;
top: 5px;
right: 5px;
text-align: center;
background: #008c8c;
}
.inpWords {
width: 300px;
height: 50px;
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
padding: 20px;
position: fixed;
left: 0;
right: 0;
margin: auto;
bottom: 10px;
line-height: 50px;
font-size: 20px;
}
</style>
</head>
<body>
<div>
<!-- <div>
<p>Lorem ipsum dolor sit amet.</p>
<span>X</span>
</div> -->
<input type="text" placeholder="许个愿吧">
</div>
<script>
var paperWidth = 200,
paperHeight = 200; //愿望签的宽度和高度
var zIndex = 1; //愿望签层叠关系值
var viewWidth = document.documentElement.clientwidth, //记录初始化时浏览器可视窗口的宽度
viewHeight = document.documentElement.clientHeight; //记录初始化时浏览器可视窗口的高度
var hope = document.querySelector(".inpWords");
var wall = document.querySelector(".wall");
hope.onblur = function () { //输入框失去焦点,创建愿望签
createHope(hope.value);
}
hope.onkeydown = function (e) { //按下回车键也可以创建愿望签
if (e.key === "Enter") {
createHope(hope.value);
}
}
window.onmousedown = function (e) { //为愿望签注册鼠标事件
var divHope = getHopeDiv(e.target); //获取到点击的愿望签的dom对象
if (!divHope) { //点击的不是愿望签
return;
}
divHope.style.zIndex = zIndex; //拖拽的愿望签可以覆盖其他愿望签
zIndex++;
var div = window.getComputedStyle(divHope);
var hopeLeft = parseFloat(div.left);
var hopeTop = parseFloat(div.top);
var pageX = e.pageX;
var pageY = e.pageY;
window.onmousemove = function (e) { //移动愿望签
var disX = e.pageX - pageX;
var disY = e.pageY - pageY;
var newLeft = hopeLeft + disX;
var newTop = hopeTop + disY;
//愿望签移动的边界限制,不能超出浏览器视口
if (newLeft < 0) { //最左
newLeft = 0;
}
if (newLeft > document.documentElement.clientWidth - paperWidth) { //最右
newLeft = document.documentElement.clientWidth - paperWidth; //浏览器视口宽度 - 愿望签宽度
}
if (newTop < 0) { //最上
newTop = 0;
}
if (newTop > document.documentElement.clientHeight - paperHeight - 80) { //最下
newTop = document.documentElement.clientHeight - paperHeight -
80; //浏览器视口高度 - 愿望签高度 - 底部许愿输入框占用的一定高度80
}
divHope.style.left = newLeft + "px";
divHope.style.top = newTop + "px";
}
window.onmouseup = window.onmouseleave = function () { //鼠标按键抬起或离开愿望签,愿望签不在移动
window.onmousemove = null;
}
}
//注册点击关闭愿望签事件
window.onclick = function (e) {
if (e.target.parentElement && e.target.parentElement.className === "paper" && e.target.tagName ===
"SPAN") {
e.target.parentElement.remove();
}
}
//愿望签的位置随着浏览器可视窗口大小改变
window.onresize = function () {
var viewDisX = document.documentElement.clientWidth - viewWidth, //记录浏览器可视宽度的改变量
viewDisY = document.documentElement.clientHeight - viewHeight; //记录浏览器可视高度的改变量
//所有的愿望签都要重新调整位置
for (var i = 0; i < wall.children.length; i++) {
var hope = wall.children[i];
var left = parseFloat(hope.style.left); //获取浏览器尺寸改变前,愿望签左上角距离浏览器左边边界的距离
var right = viewWidth - paperWidth - left; //获取浏览器尺寸改变前,愿望签右下角距离浏览器右边边界的距离
var newLeft = left + left / (left + right) * viewDisX; //新的left值 等于之前的left值+按比例变化的值
hope.style.left = newLeft + "px";
var top = parseFloat(hope.style.top); //获取浏览器尺寸改变前,愿望签左上角距离浏览器上边边界的距离
var bottom = viewHeight - paperHeight - top; //获取浏览器尺寸改变前,愿望签右下角距离浏览器下边边界的距离
var newTop = top + top / (top + bottom) * viewDisY;//新的top值 等于之前的top值+按比例变化的值
hope.style.top = newTop + "px";
}
viewWidth = document.documentElement.clientWidth;