jquery添加div实现消息聊天框


Posted in jQuery onFebruary 08, 2020

本文实例为大家分享了jquery添加div实现消息聊天框的具体代码,供大家参考,具体内容如下

上代码

<html>
<head>
<style>
* {
  margin: 0;
  padding: 0;
}

.border {
  margin-left: 300px;
  width: 900px;
  background-color: white;
  position: relative;
  border: 1px solid rgb(221, 221, 221);
}

.border .border-next {
  background-color: #dcad50;
  position: relative;
  height: 23px;
  line-height: 40px;
  display: flex;
  padding: 10px 60px 10px 80px;
}

.border-next .people {
  background-color: #dcad50;
  font-size: 20px;
  color: black;
  font-family: 楷体;
  margin-left: 300px;
}

.border .border-second {
  background-color: white;
  margin-left: 0px;
  width: 700px;
  height: 530px;
  flex: 1;
  flex-direction: column;
  overflow-y: auto;
  border-right: 1px solid rgb(221, 221, 221);
  border-bottom: 1px solid rgb(221, 221, 221);
}

.border .border-img {
  background-color: white;
  margin-left: 0px;
  width: 700px;
  height: 30px;
  border-right: 1px solid rgb(221, 221, 221);
  border-bottom: 1px solid rgb(221, 221, 221);
}

.border-bottom {
  display: flex;
  width: 700px;
  height: 120px;
  background-color: white;
  overflow: auto;
  font-size: 20px;
  border-style: solid;
  border-color: #FFFFFF;
  border-right: 1px solid rgb(221, 221, 221);
}

.button {
  display: flex;
  margin-left: 530px;
}

.button .shut {
  background-color: white;
  width: 70px;
  height: 30px;
  font-size: 20px;
  text-align: center;
  border: 1px solid rgb(221, 221, 221);
}

.button .send {
  background-color: white;
  margin-left: 15px;
  width: 70px;
  height: 30px;
  font-size: 20px;
  text-align: center;
  border: 1px solid rgb(221, 221, 221);
  background-color: #DBAC50;
}

.replyChat {
  display:flex;
  width: 150px;
  background: #12B7F5;
  border-radius: 5px;
  /* 圆角 */
  position: relative;
  margin-left: 500px;
  align-content: center;
  margin-bottom: 30px;
}

.sendChat {
  display:flex;
  width: 150px;
  background: #E5E5E5;
  border-radius: 5px;
  /* 圆角 */
  position: relative;
  margin-left: 50px;
  align-content: center;
  margin-bottom: 30px;
  border-color: white white white #E5E5E5;
}

.sendChat span {
  display: inline-block;
  margin-left: 10px;
  line-height: 35px;
}

.replyChat span {
  display: inline-block;
  margin-left: 10px;
  line-height: 35px;
}

.sendChat .arrows {
  position: absolute;
  top: 5px;
  left: -16px;
  /* 圆角的位置需要细心调试哦 */
  width: 0;
  height: 0;
  font-size: 0;
  border: solid 8px;
  border-color: white #E5E5E5 white white;
}

.replyChat .arrow {
  position: absolute;
  top: 5px;
  right: -16px;
  /* 圆角的位置需要细心调试哦 */
  width: 0;
  height: 0;
  font-size: 0;
  border: solid 8px;
  border-color: white white white #12B7F5;
}

.chatTouXiang {
  width: 50px;
  height: 50px;
  border-radius: 50%; 
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  background-image: url(img/tou.png);
}
.chatCnt{

}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>聊天助手</title>
<script src="js/jquery-1.8.3.min.js"></script>
<script>
  window.οnlοad=function(){
    $(".arrow").hide();
    $(".arrows").hide();
  }
  document.onkeydown = function(e) {
    if (e.keyCode == 13 && e.ctrlKey) {
      // 这里实现换行
      document.getElementById("sendContent").value += "\n";
    } else if (e.keyCode == 13) {
      // 避免回车键换行
      e.preventDefault();
      // 下面写你的发送消息的代码
      f();
    }
  }
  function f() {
    var cnt = $("#sendContent").val();
    if(cnt == '')alert('内容不能为空');  
    if(cnt != ''){
      var node = document.createElement('div');
      node.className = 'sendChat';
      var span = document.createElement('span');
      span.innerHTML = cnt;
      var arrow = document.createElement('div');
      arrow.className = 'arrows';
      node.appendChild(span);
      node.appendChild(arrow);
      $(".border-second").append($(node));
      $("#sendContent").val('');
      $.ajax({
        data : cnt,
        type : "post",
        url : "CharServlet?id=" + cnt,
        dataType : "json",
        success : function(msg) {
          var node = document.createElement('div');
          node.className = 'replyChat';
          var span = document.createElement('span');
          span.innerHTML = msg.text;
          var arrow = document.createElement('div');
          arrow.className = 'arrow';
          node.appendChild(arrow);
          node.appendChild(span);

          $(".border-second").append($(node));

          var boderSecondDiv = $('.border-second');
          var lastChild = boderSecondDiv[0].lastChild;
          var lastChildH = lastChild.offsetTop;
          var h = 0;
          for (var i = 0, len = lastChild.children.length; i < len; i++) {

            h += lastChild.children[i].offsetHeight;
          }
          boderSecondDiv[0].scrollTop = lastChildH + h;

        },
        error : function(msg) {
          alert("请求失败");
        }
      });
    }
  }
</script>
</head>

<div class="frame">
  <div class="border">
    <div class="border-next">
      <div class="people">聊天助手</div>
    </div>
    <div class="border-second">
      <div class="chatCnt">
      <div class="chatTouXiang"></div> 
      <div class="sendChat">
        <span></span>
        <div class="arrows"></div>
      </div>
      </div>
      <div class="replyChat">
        <span></span>
        <div class="arrow"></div>
      </div>
      <br>
    </div>
    <div class="border-img"></div>
    <textarea id="sendContent" class="border-bottom"></textarea>
    <div class="button">
      <button class="shut">关闭</button>
      <button id="selectBtn" class="send" onclick="f()">发送</button>
    </div>

  </div>
</div>
</body>
</html>

jquery添加div实现消息聊天框

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

jQuery 相关文章推荐
jQuery 添加样式属性的优先级别方法(推荐)
Jun 08 jQuery
简单谈谈require模块化jquery和angular的问题
Jun 23 jQuery
详解jquery插件jquery.viewport.js学习使用方法
Sep 08 jQuery
JQuery元素快速查找与操作
Apr 22 jQuery
jQuery实现通过方向键控制div块上下左右移动的方法【测试可用】
Apr 26 jQuery
jQuery获取随机颜色的实例代码
May 21 jQuery
jQuery 获取除某指定对象外的其他对象 ( :not() 与.not())
Oct 10 jQuery
jquery选择器和属性对象的操作实例分析
Jan 10 jQuery
jQuery 选择器用法实例分析【prev + next】
May 22 jQuery
jQuery加PHP实现图片上传并提交的示例代码
Jul 16 jQuery
jQuery实现本地存储
Dec 22 jQuery
jQuery实现聊天对话框
Feb 08 #jQuery
jquery实现聊天机器人
Feb 08 #jQuery
jQuery实现获取多选框的值示例
Feb 07 #jQuery
jQuery操作选中select下拉框的值代码实例
Feb 07 #jQuery
如何使用Jquery动态生成二级选项列表
Feb 06 #jQuery
jQuery单页面文字搜索插件jquery.fullsearch.js的使用方法
Feb 04 #jQuery
9种方法优化jQuery代码详解
Feb 04 #jQuery
You might like
如何在Ubuntu下启动Apache的Rewrite功能
2013/07/05 PHP
利用yahoo汇率接口实现实时汇率转换示例 汇率转换器
2014/01/14 PHP
php将图片保存入mysql数据库失败的解决方法
2014/12/27 PHP
让ThinkPHP的模板引擎达到最佳效率的方法详解
2017/03/14 PHP
Yii2数据库操作常用方法小结
2017/05/04 PHP
JavaScript中匿名、命名函数的性能测试
2014/09/04 Javascript
javascript实现动态加载CSS
2015/01/26 Javascript
XML文件转化成NSData对象的方法
2015/08/12 Javascript
node.js cookie-parser之parser.js
2016/06/06 Javascript
JavaScript基础重点(必看)
2016/07/09 Javascript
JavaScript兼容性总结之获取非行间样式案例
2016/08/07 Javascript
js时间控件只显示年月
2017/01/08 Javascript
JavaScript三种绑定事件方式及相互之间的区别分析
2017/01/10 Javascript
vue写一个组件
2018/04/09 Javascript
JavaScript定时器设置、使用与倒计时案例详解
2019/07/08 Javascript
JavaScript中的this基本问题实例小结
2020/03/09 Javascript
vue实现输入框自动跳转功能
2020/05/20 Javascript
Python multiprocessing模块中的Pipe管道使用实例
2015/04/11 Python
Python中用于转换字母为小写的lower()方法使用简介
2015/05/19 Python
python魔法方法-属性转换和类的表示详解
2016/07/22 Python
Scrapy爬虫实例讲解_校花网
2017/10/23 Python
django定期执行任务(实例讲解)
2017/11/03 Python
CentOS7.3编译安装Python3.6.2的方法
2018/01/22 Python
Python实现的个人所得税计算器示例
2018/06/01 Python
Python代码实现删除一个list里面重复元素的方法
2019/04/02 Python
Python 脚本实现淘宝准点秒杀功能
2019/11/13 Python
CSS3圆角和渐变2种常用功能详解
2016/01/06 HTML / CSS
我有一个char * 型指针正巧指向一些int 型变量, 我想跳过它们。 为什么如下的代码((int *)p)++; 不行?
2013/05/09 面试题
自考毕业自我鉴定范文
2013/10/27 职场文书
学校运动会开幕演讲稿
2014/01/04 职场文书
养殖项目策划书范文
2014/01/13 职场文书
2014年巴西世界杯口号
2014/06/05 职场文书
小学生国庆节演讲稿
2014/09/05 职场文书
2014年采购部工作总结
2014/11/20 职场文书
2014年学校后勤工作总结
2014/12/06 职场文书
煤矿施工安全协议书
2016/03/22 职场文书