实现的功能为上图所示,但是每新发送的消息必须显示在最上面。

我实现了两版,样式有是一样的。我们直接看代码。

版本一:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
} li{
list-style: none;
}
.box{
width: 253px;
height: 535px;
background: url('img/iPhone.png')no-repeat;
margin: 0 auto;
position: relative;
}
.content{
position: absolute;
width: 216px;
height: 340px;
top:78px;
left: 21px;
overflow: auto;
}
.send{
position: absolute;
width: 216px;
height: 42px;
bottom:74px;
left: 21px;
background-color: #EEEEEE;
border-top: 1px solid #ccc;
}
.send a{
position: absolute;
top: 3px;
left: 5px;
text-decoration: none;
text-align: center;
line-height: 32px;
width: 30px;
height: 32px;
border-radius: 5px;
background-color: #fff;
border: 1px solid #ccc;
}
.db{
background: url('img/qq2.gif')no-repeat center;
}
.xl{
background: url('img/qq1.gif')no-repeat center; }
.send .inp1{
position: absolute;
top: 3px;
left: 41px;
width: 126px;
height: 32px;
border-radius: 5px;
border: 1px solid #ccc;
}
.send span{
position: absolute;
top: 50%;
right:0px;
width: 40px;
height: 32px;
color: #999;
margin-top: -16px;
font: 600 16px/32px "微软雅黑";
cursor: pointer;
}
.cont{
padding: 10px 0;
width: 100%;
}
.content div{
display: block;
clear: both;
}
.content .lis_lf{
background: url('img/qq2.gif')no-repeat;
float: left;
margin: 5px 5px 0 5px;
}
.content .lis_rt{
background: url('img/qq1.gif')no-repeat right;
float: right;
margin:5px 5px 0 5px;
}
.content .lis_lf span{
display: inline-block;
margin-left: 30px;
word-break:break-all; width:100px;
word-wrap:break-word;
background-color: #e7e5eb;
border-radius: 5px;
}
.content .lis_rt span{
display: inline-block;
margin-right: 30px;
word-break:break-all; width:100px; overflow:auto;
word-wrap:break-word;
background-color: #2bc71c;
border-radius: 5px;
}
</style>
<script>
window.onload = function(){
var oContent =document.getElementById("content");
var oL1 = oContent.getElementsByTagName("ol")[0];
var oSend = document.getElementsByTagName("span")[0];
var oInp1 = document.getElementById("inp1");
var oA = document.getElementsByTagName("a")[0];
var onOff = true; oA.onclick = function(){
if(this.onOff){
oA.className = 'db';
this.onOff = false;
}else{
oA.className = 'xl';
this.onOff = true;
}
}; //发送消息
oSend.onclick = function(){
if(oInp1.value == "")
{
alert("消息不能为空");
}
else
{
if(oA.onOff)
{
var Test = "<li class ='lis_rt'>" + "<span>"+oInp1.value+"</span>" + "</li>";
// oL1.innerHTML += Test; //等价于 oL1.innerHTML = oL1.innerHTML + Test
oL1.innerHTML = Test + oL1.innerHTML;
}
else
{
var Test = "<li class ='lis_lf'>" + "<span>" + oInp1.value + "</span>" + "</li>";
oL1.innerHTML = Test + oL1.innerHTML;
}
oInp1.value = "";
}
};
} </script>
</head>
<body>
<div class="box">
<div class="content" id="content">
<ol class="cont reverse"></ol>
</div>
<div class="send">
<a href="javascript:;" class="db"></a>
<input class="inp1" type="text" value="" id="inp1">
<span>发送</span>
</div>
</div>
</body>
</html>

版本二:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
} li{
list-style: none;
}
.box{
width: 253px;
height: 535px;
background: url('img/iPhone.png')no-repeat;
margin: 0 auto;
position: relative;
}
.content{
position: absolute;
width: 216px;
height: 340px;
top:78px;
left: 21px;
overflow: auto;
}
.send{
position: absolute;
width: 216px;
height: 42px;
bottom:74px;
left: 21px;
background-color: #EEEEEE;
border-top: 1px solid #ccc;
}
.send a{
position: absolute;
top: 3px;
left: 5px;
text-decoration: none;
text-align: center;
line-height: 32px;
width: 30px;
height: 32px;
border-radius: 5px;
background-color: #fff;
border: 1px solid #ccc;
}
.db{
background: url('img/qq2.gif')no-repeat center;
}
.xl{
background: url('img/qq1.gif')no-repeat center; }
.send .inp1{
position: absolute;
top: 3px;
left: 41px;
width: 126px;
height: 32px;
border-radius: 5px;
border: 1px solid #ccc;
}
.send span{
position: absolute;
top: 50%;
right:0px;
width: 40px;
height: 32px;
color: #999;
margin-top: -16px;
font: 600 16px/32px "微软雅黑";
cursor: pointer;
}
.cont{
padding: 10px 0;
width: 100%;
}
.content div{
display: block;
clear: both;
}
.content .lis_lf{
background: url('img/qq2.gif')no-repeat;
float: left;
margin: 5px 5px 0 5px;
}
.content .lis_rt{
background: url('img/qq1.gif')no-repeat right;
float: right;
margin:5px 5px 0 5px;
}
.content .lis_lf span{
display: inline-block;
margin-left: 30px;
word-break:break-all; width:100px;
word-wrap:break-word;
background-color: #e7e5eb;
border-radius: 5px;
}
.content .lis_rt span{
display: inline-block;
margin-right: 30px;
word-break:break-all; width:100px; overflow:auto;
word-wrap:break-word;
background-color: #2bc71c;
border-radius: 5px;
}
</style>
<script>
window.onload = function(){
var oContent =document.getElementById("content");
var oL1 = oContent.getElementsByTagName("ol")[0];
var oSend = document.getElementsByTagName("span")[0];
var oInp1 = document.getElementById("inp1");
var oA = document.getElementsByTagName("a")[0];
var onOff = true;
// var num = 0;
oA.onclick = function(){
if(this.onOff){
oA.className = 'db';
this.onOff = false;
}else{
oA.className = 'xl';
this.onOff = true;
}
};
function fun(){
//1.创建li
var newItem = document.createElement("li");
//2.在li里面创建span
var newSpan = document.createElement("span");
newItem.appendChild(newSpan);
newSpan.innerHTML = oInp1.value;
if (oInp1.value == ""){
alert("消息不能为空");
}
if(oA.onOff){
newItem.className = 'lis_rt';
}else{
newItem.className = 'lis_lf';
}
oInp1.value = "";
var list = document.getElementById("cont")
list.insertBefore(newItem, list.childNodes[0]);
//insertBefore 给某个元素前添加节点
};
oSend.onclick = function(){
fun();
}
} </script>
</head>
<body>
<div class="box">
<div class="content" id="content">
<ol class="cont" id="cont">
</ol>
</div>
<div class="send">
<a href="javascript:;" class="db"></a>
<input class="inp1" type="text" value="" id="inp1">
<span>发送</span>
</div>
</div>
</body>
</html>

初学js之qq聊天实例的更多相关文章

  1. 初学js之qq聊天展开实例

    实现这样的效果. 直接看代码,html部分: <body> <div class="box"> <div class="lists" ...

  2. Js打开QQ聊天对话窗口

    function openQQ() { var qq = $(this).attr('data-qq');//获取qq号 window.open('http://wpa.qq.com/msgrd?v= ...

  3. JS简单仿QQ聊天工具的制作

    刚接触JS,对其充满了好奇,利用刚学到的一点知识,写了一个简单的仿QQ聊天的东西,其中还有很多的不足之处,有待慢慢提高. 功能:1.在输入框中输入内容,点击发送,即可在上方显示所输入内容. 2.点击‘ ...

  4. Nuxt+Vue聊天室|nuxt仿微信App界面|nuxt.js聊天实例

    一.项目简述 nuxt-chatroom 基于Nuxt.js+Vue.js+Vuex+Vant+VPopup等技术构建开发的仿微信|探探App界面社交聊天室项目.实现了卡片式翻牌滑动.消息发送/emo ...

  5. Next.js+React聊天室|Next仿微信桌面端|next.js聊天实例

    一.项目介绍 next-webchat 基于Next.js+React.js+Redux+Antd+RScroll+RLayer等技术构建的PC桌面端仿微信聊天项目.实现了消息/表情发送.图片/视频预 ...

  6. Svelte3聊天室|svelte+svelteKit仿微信聊天实例|svelte.js开发App

    基于svelte3.x+svelteKit构建仿微信App聊天应用svelte-chatroom. svelte-chatroom 基于svelte.js+svelteKit+mescroll.js+ ...

  7. Svelte3.x网页聊天实例|svelte.js仿微信PC版聊天svelte-webchat

    基于Svelte3+SvelteKit+Sass仿微信Mac界面聊天实战项目SvelteWebChat. 基于svelte3+svelteKit+sass+mescroll.js+svelte-lay ...

  8. 搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 (1)

    搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 原文地址(英文):http://www.networkcomms.net/creating ...

  9. Socket实现仿QQ聊天(可部署于广域网)附源码(1)-简介

    1.前言 本次实现的这个聊天工具是我去年c#程序设计课程所写的Socket仿QQ聊天,由于当时候没有自己的服务器,只能在机房局域网内进行测试,最近在腾讯云上买了一台云主机(本人学生党,腾讯云有个学生专 ...

随机推荐

  1. SQL中改变列的数据类型

    一.该列非主键.无default约束 直接更新: alter table 表名 alter column 列名 数据类型 二.该列为主键列.无default约束 (1)删除主键 alter table ...

  2. 一、 Spring Cloud Eureka ,咱们先跑起来

    一.Spring Cloud 简介 Spring Cloud 是一个基于Spring Boot 实现的微服务架构开发工具.是一个涉及到服务治理.分布式配置管理.负载均衡.服务容错.API网关.消息总线 ...

  3. hibernate课程 初探单表映射2-2 hibernate常用配置

    1 hibernate.cfg.xml常用配置: show_sql 控制台打印sql format_sql 控制台将sql排版 hbm2ddl.auto: create 删除表结构,重新建表并插值 u ...

  4. 前端seo基础规范

    基本规范 TDK代码规范 A: 关键词,一般3~4个最好,要与当前页面内容相关(根据实际情况,不适宜过多堆积关键词): B: 杜绝不同URL的页面标题重复现象,作为搜索结果摘要的重要选择目标之一,一定 ...

  5. vue-cli之脚手架

    一.创建VUE项目 npm install vue-cli -g vue init webpack myprject cd myproject npm run dev 补充: 组件:它是可扩展的htm ...

  6. 安装mysql-installer-community

    1.在官网上下载mysql-installer-community-5.6.25.0 2.选择MySQL Installer 3.选择Windows (x86, 32-bit), MSI Instal ...

  7. HTML 5 Web 存储提供了几种存储数据的方法

    localstorage存储对象分为两种: 1. sessionStorage: session即会话的意思,在这里的session是指用户浏览某个网站时,从进入网站到关闭网站这个时间段,sessio ...

  8. Browser History

    History 对象中包含用户(在浏览器窗口中)访问过的URL History 对象是window对象的一部分,可通过window.history属性对其进行访问. 注释:没有应用于History对象 ...

  9. Google地址

    Google的访问一直很不稳定,经常被墙,无意间发现以下一些地址,特此记录. https://g.ttlsa.com/ https://wen.lu/ http://sinaapp.co https: ...

  10. js实现排序去重计算字符次数

    /*去重*/ var arr=[1,4,4,7,3,9,0,3,2,1,"你好","你","你好","你 "]; var ...