简单实现自动聊天

在这里插入图片描述

  • 用到的知识:
    • 1.简单css布局;
    • 2.ajax交互
    • 3.跨域访问jsonp
    • 4.element.scrollTop = element.scrollHeight;//滚动条自动跳到最底部
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="font/iconfont.js"></script>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			a{
				text-decoration: none;
			}
			.icon {
			  width: 1.25em;
			  height: 1.25em;
			  vertical-align: -0.15em;
			  /* fill: currentColor; */
			  overflow: hidden;
				margin-right: 5px;
			}
			.box{
				width: 300px;
				height: 400px;
				border: 1px solid;
				margin: 100px auto;
				display: flex;
				flex-direction: column;
			}
			section{
				width: 100%;
				height: 30px;
				line-height: 30px;
				background-color: royalblue;
				color: #fff;
			}
			section a{
				color: #fff;
				float: right;
				display: block;
				border: 1px solid #fff;
				font-size: 12px;
				height: 25px;
				line-height: 25px;
				margin-top: 1px;
				margin-right: 5px;
			}
			h3{
				float: left;
				font-weight: normal;
			}
			main{
				flex: 1;
				overflow-y: auto;
			}
			.input{
				width: 100%;
				height: 100px;
				border-top: 1px solid;
			}
			.input textarea{
				width: 85%;
				height: 98px;
				float: left;
				resize: none;
			}
			.input button{
				float: right;
				margin-top: 50px;
				margin-right: 3px;
			}
			footer{
				height: 30px;
				line-height: 30px;
				width: 100%;
				background-color: skyblue;
				text-align: center;
				color: #fff;
			}
			.left,.right{
				border-radius: 5px;
				margin:3px auto;
			}
			.left{
				float: left;
				background-color: lightcoral;
			}
			.right{
				float: right;
				background-color: lavender;
			}
			.clearfix{
				clear: both;
				display: block;
			}
		</style>
	</head>
	<body>
		<div class="box">
			<section>
				<h3><svg class="icon" aria-hidden="true">
  <use xlink:href="#icon-yu"></use>
</svg>七鱼云客服</h3>
				<a href="#">人工客服</a>
			</section>
			<main></main>
			<div class="input">
				<textarea></textarea>
				<button type="button">发送</button>
			</div>
			<footer>七鱼云客服</footer>
		</div>
	</body>
</html>
<script type="text/javascript">
	let oBtn = document.querySelector("button");
	let oText = document.querySelector("textarea");
	let oMain = document.querySelector("main");
	oBtn.onclick = function(){
		if(oText.value != ""){
			let oDiv = document.createElement("div");
			oDiv.className = "right clearfix";
			oDiv.innerHTML =  `${oText.value}<svg class="icon" aria-hidden="true">
  <use xlink:href="#icon-icon-test1"></use>
</svg>`;;
			oMain.appendChild(oDiv);
			oMain.scrollTop = oMain.scrollHeight;
			receive();
			oText.value = "";
		}
	}
	function receive(){
		let xhr = new XMLHttpRequest();
		xhr.open("get","05brain.php?username=" + oText.value,true);
		xhr.send();
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
				// console.log(xhr.responseText);
				fun(xhr.responseText);
			}
		}
	}
	function fun(resText){
		let json = JSON.parse(resText);
		let oDiv = document.createElement("div");
					oDiv.className = "left clearfix";
					oDiv.innerHTML =  `<svg class="icon" aria-hidden="true">
		  <use xlink:href="#icon-icon-test"></use>
		</svg>${json.content}`;
					oMain.appendChild(oDiv);
					oMain.scrollTop = oMain.scrollHeight;//将滚动条自动跳到最底部
	}
</script>
<?php
	header("Content-type:text/html;charset=utf-8");
	$name = $_GET['username'];
	$result = file_get_contents("http://api.qingyunke.com/api.php?key=free&appid=0&msg=" . $name);
	echo "$result";
?>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值