<style>
* {
padding: 0;
margin: 0;
}
body {
width: 100%;
height: 100%;
background: rgb(65, 65, 63);
}
.bacground {
width: 700px;
height: 100%;
background: white;
margin: auto;
margin-top: 20px;
}
.head,
.pop-head {
height: 50px;
font-size: 20px;
text-align: center;
line-height: 50px;
}
.name {
width: 640px;
height: 40px;
font-size: 20px;
margin: 10px 28px;
line-height: 50px;
border-radius: 8px;
border: 2px solid rgb(139, 137, 137);
outline: none;
}
.content,
.pop-reply {
width: 640px;
height: 150px;
font-size: 22px;
margin: 10px 28px;
border: 2px solid rgb(139, 137, 137);
outline: none;
border-radius: 8px;
resize: none;
}
.btn,
.pop-btn {
float: right;
height: 30px;
margin-right: 28px;
border-radius: 6px;
outline: none;
font-size: 20px;
padding: 0 20px;
background: rgb(169, 238, 255);
}
h3 {
font-size: 20px;
color: rgb(102, 102, 102);
background: rgb(205, 221, 248);
margin-top: 50px;
line-height: 50px;
text-indent: 30px;
font-weight: 545;
}
li {
list-style: none;
width: 640px;
font-size: 22px;
margin: 15px 30px;
}
.message-head {
display: flex;
}
.message-head .photo {
width: 70px;
height: 70px;
background: rgb(6, 109, 134);
display: inline-block;
border-radius: 50%;
text-align: center;
line-height: 70px;
overflow: hidden;
}
.message-head .time {
margin-left: 12px;
}
.liuyan,
.reply p {
width: 560px;
line-height: 50px;
display: block;
background: rgb(205, 221, 248);
font-size: 20px;
margin-left: 80px;
border-radius: 8px;
text-indent: 15px;
}
.delete {
width: 60px;
height: 30px;
display: block;
line-height: 30px;
margin-left: 580px;
border-radius: 6px;
outline: none;
font-size: 15px;
background: rgb(169, 238, 255);
}
.answer {
width: 60px;
height: 30px;
display: block;
line-height: 30px;
margin-top: -29px;
margin-left: 515px;
border-radius: 6px;
outline: none;
font-size: 15px;
background: rgb(169, 238, 255);
}
.popup {
width: 100vw;
height: 100vh;
position: fixed;
background: rgba(0, 0, 0, .3);
left: 0;
top: 0;
z-index: 10;
display: flex;
align-items: center;
justify-content: center;
display: none;
}
.pop-content {
width: 700px;
background: #fff;
border-radius: 10px;
overflow: hidden;
padding-bottom: 20px;
}
.reply p {
margin-top: 10px;
background: rgba(100, 100, 100, .1);
}
</style>
<div class="bacground">
<div class="head">留言板</div>
<input class="name" type="text" placeholder="请输入您的昵称">
<textarea class="content" placeholder="请保持言论文明......"></textarea>
<button class="btn">留言</button>
<h3>大家在说</h3>
<ul class="text">
</ul>
</div>
<div class="popup">
<div class="pop-content">
<div class="pop-head">回复板</div>
<textarea class="pop-reply" placeholder="请保持言论文明......"></textarea>
<button class="pop-btn">回复</button>
</div>
</div>
<script>
var oBtn = document.querySelector('.btn');
oBtn.onclick = function () {
var oName = document.querySelector('.name').value;
var oContent = document.querySelector('.content').value;
if (createMsg(oName, oContent)) {
startTimer()
}
}
function startTimer() {
oBtn.disabled = true;
count = 10;
oBtn.innerHTML = count + '秒后可留言';
var t = setInterval(function () {
count--;
oBtn.innerHTML = count + '秒后可留言';
if (count < 0) {
clearInterval(t)
oBtn.disabled = false;
oBtn.innerHTML = '留言'
}
}, 200)
}
function createMsg(oName, oContent) {
if (!oName || !oContent) {
alert('输入不能为空')
return false
}
var oLi = document.createElement('li');
var oText = document.querySelector('.text');
oLi.innerHTML =
`
<div class="message-head">
<span class="photo">${oName}</span>
<p class="time">${formatDate(new Date())}</p>
</div>
<p class="liuyan">${oContent}</p>
<div class="reply">
</div>
<button class="delete">Delete</button>
<button class="answer">Answer</button>
`
oText.appendChild(oLi)
oText.insertBefore(oLi, oText.children[0]);
var dele = document.querySelectorAll('.delete');
for (var i = 0; i < dele.length; i++) {
dele[i].onclick = function () {
this.parentNode.remove()
}
}
var oAnswer = document.querySelectorAll('.answer');
var oPopup = document.querySelector('.popup');
for (var i = 0; i < oAnswer.length; i++) {
oAnswer[i].onclick = function () {
var oReply = this.previousElementSibling.previousElementSibling;
oPopup.style.display = 'flex';
var btnp = document.querySelector('.pop-btn');
btnp.onclick = function () {
var pupse = document.querySelector('.pop-reply').value;
if (!pupse) {
alert('回复不能为空');
oPopup.style.display = 'none';
return
}
var op = document.createElement('p');
op.innerHTML = pupse;
oReply.appendChild(op);
oPopup.style.display = 'none';
}
}
}
return true
}
function formatDate(date) {
var date = new Date;
var y = date.getFullYear();
var m = date.getMonth();
m = m >= 10 ? m : '0' + m;
var d = date.getDate();
d = d >= 10 ? d : '0' + d;
var h = date.getHours();
h = h >= 10 ? h : '0' + h;
var min = date.getMinutes();
min = min >= 10 ? min : '0' + min;
var s = date.getSeconds();
s = s >= 10 ? s : '0' + s;
return y + '年' + m + '月' + d + '日' + ' ' + h + ':' + min + ':' + s
}
</script>