html简单实现b站评论回车发布

心已赠人 2024-03-17 11:43 107阅读 0赞

头像:

9cb127a518f645718977be7968534776.png

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>评论回车发布</title>
  8. <style>
  9. .wrapper {
  10. min-width: 400px;
  11. max-width: 800px;
  12. display: flex;
  13. justify-content: flex-end;
  14. }
  15. .avatar {
  16. width: 48px;
  17. height: 48px;
  18. border-radius: 50%;
  19. overflow: hidden;
  20. background: url(./images/avatar.jpg) no-repeat center / cover;
  21. margin-right: 20px;
  22. }
  23. .wrapper textarea {
  24. outline: none;
  25. border-color: transparent;
  26. resize: none;
  27. background: #f5f5f5;
  28. border-radius: 4px;
  29. flex: 1;
  30. padding: 10px;
  31. transition: all 0.5s;
  32. height: 30px;
  33. }
  34. .wrapper textarea:focus {
  35. border-color: #e4e4e4;
  36. background: #fff;
  37. height: 50px;
  38. }
  39. .wrapper button {
  40. background: #00aeec;
  41. color: #fff;
  42. border: none;
  43. border-radius: 4px;
  44. margin-left: 10px;
  45. width: 70px;
  46. cursor: pointer;
  47. }
  48. .wrapper .total {
  49. margin-right: 80px;
  50. color: #999;
  51. margin-top: 5px;
  52. opacity: 0;
  53. transition: all 0.5s;
  54. }
  55. .list {
  56. min-width: 400px;
  57. max-width: 800px;
  58. display: flex;
  59. }
  60. .list .item {
  61. width: 100%;
  62. display: flex;
  63. }
  64. .list .item .info {
  65. flex: 1;
  66. border-bottom: 1px dashed #e4e4e4;
  67. padding-bottom: 10px;
  68. }
  69. .list .item p {
  70. margin: 0;
  71. }
  72. .list .item .name {
  73. color: #FB7299;
  74. font-size: 14px;
  75. font-weight: bold;
  76. }
  77. .list .item .text {
  78. color: #333;
  79. padding: 10px 0;
  80. }
  81. .list .item .time {
  82. color: #999;
  83. font-size: 12px;
  84. }
  85. </style>
  86. </head>
  87. <body>
  88. <div class="wrapper">
  89. <i class="avatar"></i>
  90. <textarea id="tx" placeholder="发一条友善的评论" rows="2" maxlength="200"></textarea>
  91. <button>发布</button>
  92. </div>
  93. <div class="wrapper">
  94. <span class="total">0/200字</span>
  95. </div>
  96. <div class="list">
  97. <div class="item" style="display: none;">
  98. <i class="avatar"></i>
  99. <div class="info">
  100. <p class="name">Big_Peng</p>
  101. <p class="text">大家都辛苦啦,感谢各位大大的努力,能圆满完成真是太好了[笑哭][支持]</p>
  102. <p class="time">2023-07-01 20:29:21</p>
  103. </div>
  104. </div>
  105. </div>
  106. <script>
  107. const textarea = document.querySelector('.wrapper textarea')
  108. const span = document.querySelector('.wrapper .total')
  109. const item = document.querySelector('.item')
  110. const text = document.querySelector('.text')
  111. const btn = document.querySelector('button')
  112. // 1.获得焦点
  113. textarea.addEventListener('focus',function(){
  114. span.style.opacity = 1
  115. })
  116. // 2.失去焦点
  117. textarea.addEventListener('blur',function(){
  118. span.style.opacity = 0
  119. })
  120. // 显示下方字数
  121. textarea.addEventListener('input',function(){
  122. const num = textarea.value.length
  123. span.innerText = `${num}/200字`
  124. })
  125. textarea.addEventListener('keyup',function(e){
  126. if(e.key === 'Enter') {
  127. if(textarea.value.trim() !== '') {
  128. item.style.display = 'block'
  129. text.innerHTML = textarea.value
  130. }
  131. textarea.value = ''
  132. }
  133. })
  134. btn.addEventListener('click',function(){
  135. if(textarea.value.trim() !== '') {
  136. item.style.display = 'block'
  137. text.innerHTML = textarea.value
  138. }
  139. textarea.value = ''
  140. })
  141. </script>
  142. </body>
  143. </html>

发表评论

表情:
评论列表 (有 0 条评论,107人围观)

还没有评论,来说两句吧...

相关阅读