头像:文章来源:https://www.toymoban.com/news/detail-520016.html
文章来源地址https://www.toymoban.com/news/detail-520016.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>评论回车发布</title>
<style>
.wrapper {
min-width: 400px;
max-width: 800px;
display: flex;
justify-content: flex-end;
}
.avatar {
width: 48px;
height: 48px;
border-radius: 50%;
overflow: hidden;
background: url(./images/avatar.jpg) no-repeat center / cover;
margin-right: 20px;
}
.wrapper textarea {
outline: none;
border-color: transparent;
resize: none;
background: #f5f5f5;
border-radius: 4px;
flex: 1;
padding: 10px;
transition: all 0.5s;
height: 30px;
}
.wrapper textarea:focus {
border-color: #e4e4e4;
background: #fff;
height: 50px;
}
.wrapper button {
background: #00aeec;
color: #fff;
border: none;
border-radius: 4px;
margin-left: 10px;
width: 70px;
cursor: pointer;
}
.wrapper .total {
margin-right: 80px;
color: #999;
margin-top: 5px;
opacity: 0;
transition: all 0.5s;
}
.list {
min-width: 400px;
max-width: 800px;
display: flex;
}
.list .item {
width: 100%;
display: flex;
}
.list .item .info {
flex: 1;
border-bottom: 1px dashed #e4e4e4;
padding-bottom: 10px;
}
.list .item p {
margin: 0;
}
.list .item .name {
color: #FB7299;
font-size: 14px;
font-weight: bold;
}
.list .item .text {
color: #333;
padding: 10px 0;
}
.list .item .time {
color: #999;
font-size: 12px;
}
</style>
</head>
<body>
<div class="wrapper">
<i class="avatar"></i>
<textarea id="tx" placeholder="发一条友善的评论" rows="2" maxlength="200"></textarea>
<button>发布</button>
</div>
<div class="wrapper">
<span class="total">0/200字</span>
</div>
<div class="list">
<div class="item" style="display: none;">
<i class="avatar"></i>
<div class="info">
<p class="name">Big_Peng</p>
<p class="text">大家都辛苦啦,感谢各位大大的努力,能圆满完成真是太好了[笑哭][支持]</p>
<p class="time">2023-07-01 20:29:21</p>
</div>
</div>
</div>
<script>
const textarea = document.querySelector('.wrapper textarea')
const span = document.querySelector('.wrapper .total')
const item = document.querySelector('.item')
const text = document.querySelector('.text')
const btn = document.querySelector('button')
// 1.获得焦点
textarea.addEventListener('focus',function(){
span.style.opacity = 1
})
// 2.失去焦点
textarea.addEventListener('blur',function(){
span.style.opacity = 0
})
// 显示下方字数
textarea.addEventListener('input',function(){
const num = textarea.value.length
span.innerText = `${num}/200字`
})
textarea.addEventListener('keyup',function(e){
if(e.key === 'Enter') {
if(textarea.value.trim() !== '') {
item.style.display = 'block'
text.innerHTML = textarea.value
}
textarea.value = ''
}
})
btn.addEventListener('click',function(){
if(textarea.value.trim() !== '') {
item.style.display = 'block'
text.innerHTML = textarea.value
}
textarea.value = ''
})
</script>
</body>
</html>
到了这里,关于html简单实现b站评论回车发布的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!