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

个人名片:
 😊作者简介:一名大一在校生,web前端开发专业
 🤡 个人主页:几何小超
 🐼
座右铭:懒惰受到的惩罚不仅仅是自己的失败,还有别人的成功。
 🎅**学习目标: 坚持每一次的学习打卡,掌握更多知识!虽然都说前端已死,但是就算不靠这个吃饭,学一点东西总比啥也不知道的好

首先我们来制作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>

谢谢大家,可能刚开始接触没多久APl,写的地方有些许错误,还是求大家提意见我来改正,哈哈哈,最后记得一键三连哦!!

  • 37
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 35
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 35
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

几何小超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值