html+css+js實(shí)現(xiàn)簡(jiǎn)易版ChatGPT聊天機(jī)器人
OpenAI的一款聊天機(jī)器人模型ChatGPT爆火,本篇文章用一百行html+css+js代碼給大家制作一款簡(jiǎn)易的聊天機(jī)器人。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test</title>
<style type="text/css">
@import url("https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500;600&family=Poppins:wght@200;300&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
background: #4b5c66;
}
.container {
--light-color: #fff;
height: 580px;
background: var(--light-color);
bottom: 50px;
right: 10px;
box-shadow: 0px 0px 15px 0px black;
}
@media screen and (min-width:440px) {
.container {
position: fixed;
}
}
.chat-header {
height: 60px;
display: flex;
align-items: center;
padding: 0px 30px;
background-color: #0652c0;
color: var(--light-color);
font-size: 1.5rem;
}
.chat-header .logo {
height: 35px;
width: 35px;
box-shadow: 0px 0px 10px 0px black;
}
.chat-header img {
height: 100%;
width: 100%;
}
.chat-header .title {
padding-left: 10px;
}
.chat-body {
height: 465px;
display: flex;
flex-direction: column;
padding: 8px 10px;
align-items: flex-end;
overflow-y: auto;
}
.chat-input {
height: 60px;
display: flex;
align-items: center;
border-top: 1px solid #ccc;
}
.input-sec {
flex: 9;
}
.send {
flex: 1;
padding-right: 4px;
}
#txtInput {
line-height: 30px;
padding: 8px 10px;
border: none;
outline: none;
caret-color: black;
font-size: 1rem;
width: 100%;
}
.chatbot-message,
.user-message {
padding: 8px;
background: #ccc;
margin: 5px;
width: max-content;
border-radius: 10px 3px 10px 10px;
}
.chatbot-message {
background: #0652c0;
color: var(--light-color);
align-self: flex-start;
border-radius: 10px 10px 3px 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="chat-header">
<div class="logo"><img src="https://code.haiyong.site/wp-content/uploads/2022/10/logo.jpg" alt="cwt" /></div>
<div class="title">簡(jiǎn)易版Chat GPT</div>
</div>
<div class="chat-body"></div>
<div class="chat-input">
<div class="input-sec"><input type="text" id="txtInput" placeholder="在這里寫(xiě)" autofocus /></div>
<div class="send"><img src="https://haiyong.site/img/svg/send.svg" alt="send" /></div>
</div>
</div>
<script>
const responseObj = {
你好: "你好,我是最強(qiáng)人工智能ChatGPT,我能回答你所有問(wèn)題,快來(lái)和我聊天吧!",
五塊錢(qián)怎么花三天: "坐公交回去找媽媽",
你是小黑子嗎: "不,我不是小黑子。我是OpenAI的聊天機(jī)器人模型ChatGPT",
你為什么和我聊天: "只因你太美",
嘿: "嘿! 這是怎么回事",
今天幾號(hào): new Date().toDateString(),
幾點(diǎn)了: new Date().toLocaleTimeString(),
};
const chatBody = document.querySelector(".chat-body");
const txtInput = document.querySelector("#txtInput");
const send = document.querySelector(".send");
send.addEventListener("click", () => renderUserMessage());
txtInput.addEventListener("keyup", (event) => {
if (event.keyCode === 13) {
renderUserMessage();
}
});
const renderUserMessage = () => {
const userInput = txtInput.value;
renderMessageEle(userInput, "user");
txtInput.value = "";
setTimeout(() => {
renderChatbotResponse(userInput);
setScrollPosition();
}, 600);
};
const renderChatbotResponse = (userInput) => {
const res = getChatbotResponse(userInput);
renderMessageEle(res);
};
const renderMessageEle = (txt, type) => {
let className = "user-message";
if (type !== "user") {
className = "chatbot-message";
}
const messageEle = document.createElement("div");
const txtNode = document.createTextNode(txt);
messageEle.classList.add(className);
messageEle.append(txtNode);
chatBody.append(messageEle);
};
const getChatbotResponse = (userInput) => {
return responseObj[userInput] == undefined ?
"聽(tīng)不太懂呢試試輸點(diǎn)別的" :
responseObj[userInput];
};
const setScrollPosition = () => {
if (chatBody.scrollHeight > 0) {
chatBody.scrollTop = chatBody.scrollHeight;
}
};
</script>
</body>
</html>到此這篇關(guān)于html+css+js實(shí)現(xiàn)簡(jiǎn)易版ChatGPT聊天機(jī)器人的文章就介紹到這了,更多相關(guān)html+css+js實(shí)現(xiàn)聊天內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JavaScript基于ChatGPT?API實(shí)現(xiàn)劃詞翻譯瀏覽器腳本
- 兩天沒(méi)解決的問(wèn)題chatgpt用了5秒搞定隱藏bug
- 一文解析ChatGPT?之?Fetch?請(qǐng)求
- 讓chatGPT教你如何使用taro創(chuàng)建mbox
- LangChain簡(jiǎn)化ChatGPT工程復(fù)雜度使用詳解
- ChatGPT用于OA聊天助手導(dǎo)致訪問(wèn)量服務(wù)宕機(jī)
- 詳解微信小程序如何實(shí)現(xiàn)類(lèi)似ChatGPT的流式傳輸
- ChatGPT前端編程秀之別拿編程語(yǔ)言不當(dāng)語(yǔ)言
- 詳解如何利用chatgpt保護(hù)您的js代碼
相關(guān)文章
javascript Array.remove() 數(shù)組刪除
下面的代碼主要是實(shí)現(xiàn)了,刪除數(shù)組中指定的值。2009-08-08
(currentStyle)javascript為何有時(shí)用style得不到已設(shè)定的CSS的屬性
(currentStyle)javascript為何有時(shí)用style得不到已設(shè)定的CSS的屬性...2007-08-08
微信企業(yè)號(hào)開(kāi)發(fā)之微信考勤百度地圖定位
本文給大家介紹微信企業(yè)號(hào)開(kāi)發(fā)之微信考勤百度地圖定位,有需要的朋友參考下本篇文章2015-09-09
Javascript實(shí)現(xiàn)的StopWatch功能示例
這篇文章主要介紹了Javascript實(shí)現(xiàn)的StopWatch功能,結(jié)合具體實(shí)例形式分析了javascript自定義StopWatch實(shí)現(xiàn)測(cè)試運(yùn)行時(shí)間功能的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
HTML中用JS實(shí)現(xiàn)旋轉(zhuǎn)的圣誕樹(shù)
這篇文章介紹了HTML中用JS實(shí)現(xiàn)旋轉(zhuǎn)的圣誕樹(shù),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
javascript 判斷頁(yè)面訪問(wèn)方式電腦或者移動(dòng)端
這篇文章主要介紹了 判斷頁(yè)面訪問(wèn)方式電腦或者移動(dòng)端的相關(guān)資料,這里提供了三種方法,需要的朋友可以參考下2016-09-09

