Home Web Front-end JS Tutorial JS imitation chat page

JS imitation chat page

Nov 20, 2017 am 10:08 AM
javascript chat page

JSChat interface, source code is provided for free, friends who are interested in JS can study it, this is still of great help to our JS knowledge Oh~

JS imitation chat page

Code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>PHP中文网</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        /*chrome下能隐藏滚动条*/
        ::-webkit-scrollbar{
            display: none;
        }

        @keyframes text {
            0% {
                top:50px;
                opacity: 0;
            }
            100%{
                top: 0;
                opacity: 1;
            }
        }
        body{
            background-color: #e1e1e1;
        }
        .container{
            position: relative;
            height: 700px;
            width: 400px;
            margin: 50px auto;
            background-color: #f0f0f0;
            border-radius: 6px;
            overflow: hidden;
        }
        .main{
            overflow: auto;
            border-bottom: 60px solid #fff;
            padding: 20px 10px;
            width: 100%;
            height: 100%;
        }

        /*这个伪元素解决不同浏览器padding裁剪内容不一致的问题!!*/
        .main:after{
            display: block;
            content: "";
            background-color: transparent;
            width: 100%;
            height: 50px;
        }
        .lineBox{
            overflow: hidden;
            min-height: 40px;
            clear: both;
            position: relative;

        }
        .imgWhite{
            position: absolute;

            left: 10px;
            width: 40px;
            height: 40px;
            border-radius: 50%;
        }
        .imgBlue{
            position: absolute;
            right: 10px;
            width: 40px;
            height: 40px;
            border-radius: 50%;
        }
        .textWhite{

            /*!*解决连续字母不换行的问题*!
            max-width: 300px;
            word-wrap:break-word;*/

            min-height: 40px;
            margin-left: 60px;
            margin-right: 10px;
            margin-bottom: 15px;
            padding: 10px 10px;
            display: inline-block;
            position: relative;
            border-radius: 6px;
            background-color: #ffffff;
            box-shadow: 0 5px 15px 0 rgba(0, 0, 0, 0.05);
        }
        .textWhite:before{
            display: block;
            content: "";
            width: 0;
            height: 0;
            position: absolute;
            left:-6px;
            top: 14px;
            border-top: solid 6px transparent;
            border-right: 6px solid #fff;
            border-bottom: solid 6px transparent;
        }
        .textBlue{
            min-height: 40px;
            position: relative;
            float: right;
            margin-left: 10px;
            margin-right: 60px;
            margin-bottom: 15px;
            padding: 10px 10px;
            display: inline-block;
            border-radius: 6px;
            background-color: #2970ff;
            box-shadow: 0 5px 15px 0 rgba(41, 75, 255, 0.3);
            color: #fff;
        }
        .textBlue:before{
            display: block;
            content: "";
            width: 0;
            height: 0;
            position: absolute;
            right:-6px;
            top: 14px;
            border-top: solid 6px transparent;
            border-left: 6px solid #2970ff;
            border-bottom: solid 6px transparent;
        }
        .inputBox{
            position: absolute;
            bottom: 0;
            left: 0;
            height: 60px;
            line-height: 60px;
            width: 100%;
            background-color: #ffffff;

        }
        .img1{
            margin-left: 10px;
            margin-top: -3px;
            display: inline-block;
            vertical-align: middle;
            width: 40px;
            height: 40px;
            box-sizing: border-box;
            background-color: #fff;
            border-radius: 6px;
        }
        .textBox{
            display: inline-block;
            line-height: 60px;
            right: 100px;
            left: 60px;
            position: absolute;
        }
        #text{
            display: inline-block;
            font-size: 16px;
            padding: 0 0 0 10px;
            width: 100%;
            height: 40px;
            border-radius: 6px;
            border: 1px solid #e1e1e1;
        }
        #btn{
            cursor: pointer;
            font-size: 16px;
            margin: 10px;
            display: inline-block;
            vertical-align: middle;
            float: right;
            height: 40px;
            box-sizing: border-box;
            width: 80px;
            border-radius: 6px;
            border: 1px solid #e1e1e1;
            background-color: #fff;
            outline: none;
        }
        #btn:hover{
            color: #fff;
            background-color: #2970ff;
        }
        #btn:active{
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.2) inset;
            background-color: #3668ff;
        }
    </style>
    <script>
        window.onload = function () {
            var img1 = document.getElementsByClassName("img1");
            var imgWhite = document.getElementsByClassName("imgWhite");
            var imgBlue = document.getElementsByClassName("imgBlue");
            var textM = document.getElementsByClassName("textM");
            var textWhite = document.getElementsByClassName("textWhite");
            var textBlue = document.getElementsByClassName("textBlue");
            var text = document.getElementById("text");
            var btn = document.getElementById("btn");
            var lineBox = document.getElementsByClassName("lineBox");
            var main = document.getElementsByClassName("main");

            /*用来判断猫和狗的状态*/
            var onOff = true;

            /*页面载入时聊天内容自动滚动到最下面*/
            main[0].scrollTop = main[0].scrollHeight;

            /*切换猫狗角色*/
            img1[0].onclick = function () {
                if (onOff === true) {
                    this.src = "http://www.php.cn/tpl/Index/Static/img/2017_index/logo.png";
                    onOff = false
                } else {
                    this.src = "http://www.php.cn/tpl/Index/Static/img/banner4.jpg";
                    onOff = true
                }
            };

            btn.onclick = function () {

                /*先判断猫还是狗,然后往里面添加内容*/
                if (onOff === true) {
                    main[0].innerHTML += "<div class=\"lineBox\">" + "<img  src="/static/imghw/default1.png"  data-src="http://www.php.cn/tpl/Index/Static/img/2017_index/logo.png"  class="lazy"  class=\"imgWhite\" alt="JS imitation chat page" >" + "<p class=\"textM textWhite\">" + "</p>" + "</div>";
                    imgWhite[imgWhite.length - 1].src = img1[0].src;
                    textWhite[textWhite.length - 1].innerHTML = text.value;
                } else {
                    main[0].innerHTML += "<div class=\"lineBox\">" + "<img  src="/static/imghw/default1.png"  data-src="http://www.php.cn/tpl/Index/Static/img/2017_index/logo.png"  class="lazy"  class=\"imgBlue\" alt="JS imitation chat page" >" + "<p class=\"textM textBlue\">" + "</p>" + "</div>";
                    imgBlue[imgBlue.length - 1].src = img1[0].src;
                    textBlue[textBlue.length - 1].innerHTML = text.value;
                }

                /*清除原有动画*/
                for (var i=0;i<textM.length;i++)
                {
                    textM[i].style.animation = "";
                    textM[i].style.animationFillMode = "";
                }

                /*给最新的一条聊天记录绑定CSS3帧动画*/
                textM[textM.length - 1].style.animation = "text 0.3s";
                textM[textM.length - 1].style.animationFillMode = "forwards";
                text.value = "";

                /*2个滚动方法*/
                /*lineBox[lineBox.length - 1].scrollIntoView();*/
                main[0].scrollTop = main[0].scrollHeight;
            };

            /*键盘回车事件*/
            document.onkeyup = function (e) {
                if (window.event)
                    e = window.event;
                var code = e.charCode || e.keyCode;
                if (code === 13) {
                    btn.onclick();
                }
            }
        }
    </script>
</head>
<body>
<div>
    <div>
        <div>
            <img src="/static/imghw/default1.png"  data-src="http://www.php.cn/tpl/Index/Static/img/2017_index/logo.png"  class="lazy"   alt="">
            <p class="textM textBlue">PHP中文网推出的那个60天的成为大牛的课程你报了嘛?
            </p>
        </div>
        <div>
            <img src="/static/imghw/default1.png"  data-src="http://www.php.cn/tpl/Index/Static/img/banner4.jpg"  class="lazy"   alt="">
            <p class="textM textWhite">当然报啦!你也赶快报吧!马上就截止了!PHP中文网的教育实力我还是很相信的!!
            </p>
        </div>
        <div>
            <img src="/static/imghw/default1.png"  data-src="http://www.php.cn/tpl/Index/Static/img/2017_index/logo.png"  class="lazy"   alt="">
            <p class="textM textBlue">嗯嗯~这个我知道,我也赶紧去报名了!!
            </p>
        </div>
    </div>
    <div>
        <img src="/static/imghw/default1.png"  data-src="http://www.php.cn/tpl/Index/Static/img/banner4.jpg"  class="lazy"   alt="">
        <div>
            <input type="text" id="text">
        </div>
        <input type="button" value="发送" id="btn">
    </div>

</div>
</body>
</html>
Copy after login

The above is the source code of simulated chat~Friends who are interested can use it to study~ For more articles and source codes about js, please go to PHP中文 and search

Related recommendations:

JavaScript native code to implement slideshow

##js to implement background animation split


45 Useful JavaScript Library


The above is the detailed content of JS imitation chat page. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to copy a page in Word How to copy a page in Word Feb 20, 2024 am 10:09 AM

Want to copy a page in Microsoft Word and keep the formatting intact? This is a smart idea because duplicating pages in Word can be a useful time-saving technique when you want to create multiple copies of a specific document layout or format. This guide will walk you through the step-by-step process of copying pages in Word, whether you are creating a template or copying a specific page in a document. These simple instructions are designed to help you easily recreate your page without having to start from scratch. Why copy pages in Microsoft Word? There are several reasons why copying pages in Word is very beneficial: When you have a document with a specific layout or format that you want to copy. Unlike recreating the entire page from scratch

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

How to restore the chat if WeChat does not display it? WeChat does not display the chat recovery method How to restore the chat if WeChat does not display it? WeChat does not display the chat recovery method Mar 13, 2024 pm 01:50 PM

WeChat, as one of the most popular social software nowadays, provides rich chat functions. But sometimes, we may encounter "Don't show this chat" situation, causing some important conversations to be hidden. To restore these chats, it's actually very simple. As long as you follow these steps, you can easily restore hidden chats and continue to enjoy the convenient communication experience brought by WeChat. How to restore the chat if WeChat does not display it? WeChat does not display the chat recovery method. Method 1: Try to directly search for the name or keyword of the chat partner in the WeChat message list. If the search is found, click to enter the chat interface, so that the chat can be restored and displayed. Method two, restore through friend chat: Open WeChat, click on the address book, find the friend who is displayed in the hidden chat, and click to send a message

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to quickly refresh a web page? How to quickly refresh a web page? Feb 18, 2024 pm 01:14 PM

Page refresh is very common in our daily network use. When we visit a web page, we sometimes encounter some problems, such as the web page not loading or displaying abnormally, etc. At this time, we usually choose to refresh the page to solve the problem, so how to refresh the page quickly? Let’s discuss the shortcut keys for page refresh. The page refresh shortcut key is a method to quickly refresh the current web page through keyboard operations. In different operating systems and browsers, the shortcut keys for page refresh may be different. Below we use the common W

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to implement page jump in 3 seconds: PHP Programming Guide How to implement page jump in 3 seconds: PHP Programming Guide Mar 25, 2024 am 10:42 AM

Title: Implementation method of page jump in 3 seconds: PHP Programming Guide In web development, page jump is a common operation. Generally, we use meta tags in HTML or JavaScript methods to jump to pages. However, in some specific cases, we need to perform page jumps on the server side. This article will introduce how to use PHP programming to implement a function that automatically jumps to a specified page within 3 seconds, and will also give specific code examples. The basic principle of page jump using PHP. PHP is a kind of

See all articles