您好,登錄后才能下訂單哦!
花了倆小時折騰出來的,jQuery人工智能聊天室長這樣:
主要功能:
1.當然是聊天啦~點擊飛機按鈕或者回車可以發送消息到面板。
2.輸入特定的內容,系統會給你相應的回復(這里我只設置了Hello,How are you和詢問時間的自動回復)。
3.點擊叉叉可以清除面板上的所有聊天記錄。
4.問時間的時候,根據現在的時間,會給你相應的不同的回復,比如現在是22-23點,系統會回復你“good night”。
5.隨著聊天的進行,聊天面板右側的滾動條會一直維持在最底部。
HTML:
<div class="chat-box"> </div> <form class="form-inline chat-form"> <input type="text" class="form-control chat-message" placeholder="Say Something"> <button type="button" class="btn btn-primary chat-send" title="Send Message"> <i class="fa fa-paper-plane" aria-hidden="true"> </i> </button> <button type="reset" class="btn btn-success chat-reset" title="Reset Message"> <i class="fa fa-refresh" aria-hidden="true"> </i> </button> <button type="button" class="btn btn-danger chat-clear" title="Clear the Chat Box"> <i class="fa fa-times" aria-hidden="true"> </i> </button> </form> <hr> <footer> Designed By <a rel="external nofollow" target="_blank"> Alen Hu </a> </footer>
*使用了Bootstrap3框架。
JQuery:
$(document).ready(function() { //send the message by click $(".chat-send").click(sendMsg); //press enter to send $("form").keypress(function(event) { if (event.keyCode === 13) { event.preventDefault(); sendMsg(); } }); //clear the chat box $(".chat-clear").click(clearChatBox); }); //send message to chat box function sendMsg() { var msg = $(".chat-message"); var msgVal = msg.val(); var chatBox = $(".chat-box"); if (msgVal) { var msgAppend = "<p><span id='you'>You: </span>" + msgVal + "</p><hr class='you-hr'>"; chatBox.append(msgAppend); } else {} //dialog reply dialog(msgVal); //empty input msg.val(""); //keep the scroll in bottom chatBox.scrollTop(chatBox[0].scrollHeight); } //set up the AI dialog function dialog(msg){ var replyArr = ["Hi, how's it going :)","I'm good, thx, U? :)"]; msg = msg.toLowerCase(); var time = new Date(); var hour = time.getHours(); var minute = time.getMinutes(); var currentTime = plusZero(hour) + ":" + plusZero(minute); var chatBox = $(".chat-box"); if(msg.indexOf("hello") != -1){ chatBox.append("<p><span id='ai'>AI: </span>" + replyArr[0] + "</p><hr class='ai-hr'>"); } else if(msg.indexOf("how are you") != -1 || msg.indexOf("how are u") != -1){ chatBox.append("<p><span id='ai'>AI: </span>" + replyArr[1] + "</p><hr class='ai-hr'>"); } else if(msg.indexOf("time") != -1){ chatBox.append("<p><span id='ai'>AI: </span>Current Time: " + currentTime + ". " + timeGreeting(hour) + "~ :)</p><hr class='ai-hr'>"); } else {} } //add 0 if time number is <10 function plusZero(x){ if(x < 10){ x = "0" + x; } else { x = x; } return x; } //greeting by hour function timeGreeting(h){ var greeting = ["U need to sleep","Good morning","Lunch time now","Feel asleep? Have some coffee","Free time~Yeah","Good night"]; if(h>=0&&h<=6){ return greeting[0]; } else if(h>=7&&h<=10){ return greeting[1]; } else if(h>=11&&h<=13){ return greeting[2]; } else if(h>=14&&h<=18){ return greeting[3]; } else if(h>=19&&h<=21){ return greeting[4]; } else if(h>=22&&h<=23){ return greeting[5]; } else { return ""; } } //clear the chat box function clearChatBox() { $(".chat-box").html(""); }
DEMO在這兒,歡迎FORK:AI Chat Box。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。