<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>高度なChatGPT風チャット</title>
<style>
/* 全体のスタイル */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: var(--background-color, #f4f6f9);
transition: background-color 0.3s ease;
}
.chat-container {
width: 90%;
max-width: 600px;
background-color: var(--chat-bg-color, #ffffff);
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
overflow: hidden;
display: flex;
flex-direction: column;
transition: background-color 0.3s ease;
}
.chat-header {
padding: 15px;
text-align: center;
background-color: var(--header-bg-color, #3f51b5);
color: var(--header-text-color, #ffffff);
font-weight: bold;
position: relative;
}
.theme-toggle-btn {
position: absolute;
right: 15px;
top: 15px;
padding: 5px 10px;
cursor: pointer;
border: none;
background-color: var(--header-text-color, #ffffff);
color: var(--header-bg-color, #3f51b5);
border-radius: 5px;
transition: background-color 0.3s ease;
}
.chat-messages {
padding: 20px;
overflow-y: auto;
flex-grow: 1;
border-bottom: 1px solid #ddd;
max-height: 70vh;
}
.message {
margin-bottom: 15px;
line-height: 1.5;
display: inline-block;
max-width: 70%;
padding: 10px;
border-radius: 10px;
animation: fadeIn 0.3s ease;
}
.user-message {
text-align: right;
color: #333;
background-color: var(--user-msg-bg-color, #e0f7fa);
align-self: flex-end;
}
.bot-message {
text-align: left;
color: var(--bot-msg-color, #3f51b5);
background-color: var(--bot-msg-bg-color, #e8eaf6);
align-self: flex-start;
}
.typing-indicator {
font-style: italic;
color: #999;
display: none;
}
.chat-input {
display: flex;
padding: 10px;
background-color: #f4f6f9;
border-top: 1px solid #ddd;
}
.chat-input input {
flex: 1;
padding: 12px;
border: 1px solid #ddd;
border-radius: 4px;
outline: none;
}
.chat-input button {
margin-left: 10px;
padding: 12px 20px;
background-color: var(--button-bg-color, #3f51b5);
color: var(--button-text-color, #ffffff);
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
/* アニメーション効果 */
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
</style>
</head>
<body>
<div class="chat-container">
<div class="chat-header">
高度なChatGPT風 チャット
<button class="theme-toggle-btn" onclick="toggleTheme()">🌞 ダークモード</button>
</div>
<div class="chat-messages" id="chatMessages">
<div class="message bot-message">こんにちは!質問があればどうぞ。</div>
</div>
<div class="typing-indicator" id="typingIndicator">Botが入力中...</div>
<div class="chat-input">
<input type="text" id="userInput" placeholder="メッセージを入力..." />
<button onclick="sendMessage()">送信</button>
</div>
</div>
<script>
let currentTopic = null; // 現在のトピックを保持
let isDarkMode = false;
// テーマ切り替え
function toggleTheme() {
isDarkMode = !isDarkMode;
document.body.style.setProperty("--background-color", isDarkMode ? "#333" : "#f4f6f9");
document.body.style.setProperty("--chat-bg-color", isDarkMode ? "#444" : "#ffffff");
document.body.style.setProperty("--header-bg-color", isDarkMode ? "#555" : "#3f51b5");
document.body.style.setProperty("--header-text-color", isDarkMode ? "#fff" : "#ffffff");
document.body.style.setProperty("--user-msg-bg-color", isDarkMode ? "#666" : "#e0f7fa");
document.body.style.setProperty("--bot-msg-bg-color", isDarkMode ? "#777" : "#e8eaf6");
document.body.style.setProperty("--bot-msg-color", isDarkMode ? "#aaa" : "#3f51b5");
document.body.style.setProperty("--button-bg-color", isDarkMode ? "#888" : "#3f51b5");
document.body.style.setProperty("--button-text-color", isDarkMode ? "#fff" : "#ffffff");
document.querySelector(".theme-toggle-btn").textContent = isDarkMode ? "🌜 ライトモード" : "🌞 ダークモード";
}
function saveChatHistory(text, sender) {
const chatMessages = document.getElementById("chatMessages");
const messageElement = document.createElement("div");
messageElement.className = `message ${sender}-message`;
messageElement.textContent = text;
chatMessages.appendChild(messageElement);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
function getBotResponse(userMessage) {
if (userMessage.includes("ありがとう")) {
currentTopic = "ありがとう";
return "どういたしまして!お役に立てて嬉しいです!";
} else if (userMessage.includes("助けて")) {
currentTopic = "助けて";
return "どのようなことでお困りですか?";
} else if (userMessage.includes("質問")) {
currentTopic = "質問";
return "どのような質問でしょうか?詳しく教えてください!";
} else if (currentTopic === "ありがとう") {
return "他に質問があればいつでもどうぞ。";
} else {
currentTopic = null;
return "ご質問ありがとうございます。どのような内容でしょうか?";
}
}
function showTypingIndicator() {
document.getElementById("typingIndicator").style.display = "block";
}
function hideTypingIndicator() {
document.getElementById("typingIndicator").style.display = "none";
}
function sendMessage() {
const userInput = document.getElementById("userInput");
if (userInput.value.trim() !== "") {
saveChatHistory(userInput.value, "user");
showTypingIndicator();
setTimeout(() => {
hideTypingIndicator();
const botResponse = getBotResponse(userInput.value);
saveChatHistory(botResponse, "bot");
}, 1500);
userInput.value = "";
}
}
// Enterキーでメッセージ送信
document.getElementById("userInput").addEventListener("keypress", function(event) {
if (event.key === "Enter") {
sendMessage();
event.preventDefault();
}
});
</script>
</body>
</html>