html按钮实现聊天功能实现原理,javascript实现简易聊天室_亦心_前端开发者

聊天室是我们经常见的,比如微信聊天界面、QQ聊天界面等等,一个简易的聊天室如下:

1.

发送

2.

.content{

border-radius: 5px;

border: 2px solid #cccccc;

width: 500px;

height: 700px;

margin: 50px auto 0;

overflow: hidden;

}

.section{

width: 500px;

height: 569px;

overflow-x: hidden;

overflow-y: auto;

border-bottom: 1px solid #cccccc;

padding-top: 30px;

}

.section::-webkit-scrollbar{

display: none;

}

form{

width: 500px;

height: 100px;

}

form textarea{

outline: none;

border: none;

display: block;

float: left;

width: 370px;

height: 100px;

font-size: 25px;

text-align: top;

line-height: 35px;

resize: none;

}

form button{

outline: none;

border: none;

display: block;

float: left;

width: 130px;

height: 100px;

}

form button:active{

background: #ccc;

}

.line{

width: 500px;

overflow: hidden;

}

.left,.right{

height: auto;

font-size: 25px;

line-height: 50px;

border-radius: 10px;

padding: 0 10px;

overflow-wrap: break-word;

margin-bottom: 20px;

}

.left{

max-width: 400px;

margin-left: 20px;

float: left;

background: rgb(243, 244, 245);

}

.right{

max-width: 400px;

float: right;

margin-right: 20px;

background: rgb(79, 230, 49);

}

3.

function $(str){

if (str[0]=='.') {

return document.getElementsByClassName(str.substring(1));

}else if (str[0]=='#') {

return document.getElementById(str.substring(1));

}else{

return document.getElementsByTagName(str);

}

}

//以上代码可以单独封装成一个函数

var count = 0;

$('#sub').οnclick=function(){

//当点击发送按钮后,创建class名为line的盒子,来盛放聊天的内容

var div = document.createElement('div');

div.className = 'line';

$('.section')[0].appendChild(div);

var str = $('#$value').value;

var p = document.createElement('p');

if (count%2==1) {

p.className = 'left';

}else{

p.className = 'right';

}

p.innerHTML = str;

$('#$value').value = '';

div.appendChild(p);

count++;

}

4.效果图

58663d27d784e3f4df58089f0e820bae.gif

好啦,一个简易的聊天室就制作完啦!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持前端开发者。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值