使用JS来实现回车评论功能

简介: 使用JS来实现回车评论功能

首先我们来制作b站的回车框


HTML部分


 <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">清风徐来</p>
        <p class="text">大家都辛苦啦,感谢各位大大的努力,能圆满完成真是太好了[笑哭][支持]</p>
        <p class="time">2022-10-10 20:29:21</p>
      </div>
    </div>
  </div>


然后就是我们的css部分,我们要实现点击文本框使文本框进行均匀下拉的操作,可以使用focus伪类选择器,


focus是css的一个伪类选择器,可以用来选取获得焦点的元素,然后为这些获得焦点的元素设置样式。


 只要是可以接收键盘事件或其他用户输入的元素都可以:focus选择器,大多数情况下:focus选择器都是被使用在链接和表单元素上的。


然后再下面添加运动的路径即可


.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;
    }


接下来就是我们JS部分


我来解析一下下面的思路


还是第一步我们先得获取里面的元素,只有这样才能进行接下来的操作


然后我们来写文本焦点:tx这个文本域制作一个事件,这个事件是获得焦点,让下面的0/200字的文字显示出来


tx.addEventListener('focus',function(){
      total.style.opacity=1
    })


当我们点击空白的时候得隐藏起来,同意复制上面的代码只需要改成blur就行


tx.addEventListener('blur',function(){
      total.style.opacity=0
    })


3.然后我们得做一个监听事件,检测用户输入多少字,然后让下面的0/200行来跟着用户变化


tx.addEventListener('focus',function(){
      total.innerHTML=`${tx.value.length}/200字`
    })


第四步我们开始正式操作


实现按下回车发布评论


这里的keyup是最新版语法已经淘汰之前的keycoad,意思是键盘弹出事件,然后这里的e在keyup里面是回车的意思,所以要写在方法里面


然后我们进行判断,如果用户输入不为空就会显示打印,这边的trim是消除两边空格的意思


item.style.display='block'
            text.innerHTML=tx.value


意思是显示文本中的内容,然后最后等我们按下回车结束后,就得清空文本域,所以我们的tx的最后得为空,然后发现我们下面的没有复原,这时候我们还需要加total.innerHtml='0/200字'才能复原成之后的样子


tx.addEventListener('keyup',function(e){
        //只有按下回车才会触发
        if(e.key === 'Enter'){
            //如果用户输入不为空就会显示打印
            if(tx.value.trim()!==''){
                item.style.display='block'
            text.innerHTML=tx.value
            }
            //等我们按下回车,结束后,清空文本域
            tx.value=''
            //按下回车就得复原
            total.innerHTML='0/200字'
        }
    })


这是JS源码


<script>
    const tx=document.querySelector('#tx');
    const total=document.querySelector('.total')
    const item=document.querySelector('.item');
    const text=document.querySelector('.text')
    //1.当我们文本获得焦点,让total显示出来
    tx.addEventListener('focus',function(){
      total.style.opacity=1
    })
    //1.当我们文本失去焦点,让total隐藏出来
    tx.addEventListener('blur',function(){
      total.style.opacity=0
    })
    // 3.监测用户输入
    tx.addEventListener('focus',function(){
      total.innerHTML=`${tx.value.length}/200字`
    })
    //4.按下回车发布评论
    tx.addEventListener('keyup',function(e){
        //只有按下回车才会触发
        if(e.key === 'Enter'){
            //如果用户输入不为空就会显示打印
            if(tx.value.trim()!==''){
                item.style.display='block'
            text.innerHTML=tx.value
            }
            //等我们按下回车,结束后,清空文本域
            tx.value=''
            //按下回车就得复原
            total.innerHTML='0/200字'
        }
    })
  </script>


还是老规矩,为了大家看的清除,我一边进行学习,一边写注释让我更加明白进行的操作,也是为了方便写文章


<!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">清风徐来</p>
        <p class="text">大家都辛苦啦,感谢各位大大的努力,能圆满完成真是太好了[笑哭][支持]</p>
        <p class="time">2022-10-10 20:29:21</p>
      </div>
    </div>
  </div>
  <script>
    const tx=document.querySelector('#tx');
    const total=document.querySelector('.total')
    const item=document.querySelector('.item');
    const text=document.querySelector('.text')
    //1.当我们文本获得焦点,让total显示出来
    tx.addEventListener('focus',function(){
      total.style.opacity=1
    })
    //1.当我们文本失去焦点,让total隐藏出来
    tx.addEventListener('blur',function(){
      total.style.opacity=0
    })
    // 3.监测用户输入
    tx.addEventListener('focus',function(){
      total.innerHTML=`${tx.value.length}/200字`
    })
    //4.按下回车发布评论
    tx.addEventListener('keyup',function(e){
        //只有按下回车才会触发
        if(e.key === 'Enter'){
            //如果用户输入不为空就会显示打印
            if(tx.value.trim()!==''){
                item.style.display='block'
            text.innerHTML=tx.value
            }
            //等我们按下回车,结束后,清空文本域
            tx.value=''
            //按下回车就得复原
            total.innerHTML='0/200字'
        }
    })
  </script>
</body>
</html>
相关文章
|
17天前
|
JavaScript 前端开发
js实现点击音频实现播放功能
js实现点击音频实现播放功能
|
17天前
|
前端开发 JavaScript
使用JavaScript实现复杂功能:构建一个自定义的拖拽功能
使用JavaScript实现复杂功能:构建一个自定义的拖拽功能
|
2月前
|
JavaScript 前端开发
js制作九宫格抽奖功能
js制作九宫格抽奖功能
21 0
|
3月前
|
JavaScript 前端开发
|
10天前
|
JavaScript 安全 前端开发
|
16天前
|
JavaScript 前端开发
如何用JS实现选项卡功能
如何用JS实现选项卡功能
13 0
|
17天前
|
存储 前端开发 JavaScript
使用JavaScript实现复杂功能——一个交互式音乐播放器
使用JavaScript实现复杂功能——一个交互式音乐播放器
|
17天前
|
存储 JavaScript 前端开发
JavaScript复杂功能实现:实时数据可视化图表
JavaScript复杂功能实现:实时数据可视化图表
|
24天前
|
JavaScript 前端开发
JavaScript数组的功能内置类型
数组是JavaScript的内置类型,JavaScript数组的功能特别强大。下面简单介绍一下JavaScript数组。
|
1月前
|
JavaScript 前端开发
购物车的功能——JS源码
购物车的功能——JS源码
15 2