前端开发交互小练习_JS聊天界面

web前端开发面试题 校招|完整的前端开发流程|web前端开发浏览器的兼容性
具体细节看代码注释吧

这个看似简单的案例让我狠狠的感受了一把浏览器之间的差异,还有连续字母不换行的问题,

html 代码

<!DOCTYPE html>
<html lang=”cn”>
<head>
    <meta charset=”UTF-8″>
    <title>0925属性操作</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://cdn.attach.qdfuns.com/notes/pics/201710/09/093845xis8s3pd3eks8ysq.jpg”;
                    onOff = false
                } else {
                    this.src = “http://cdn.attach.qdfuns.com/notes/pics/201710/09/093845pf6rp42pn43qn4zp.jpg”;
                    onOff = true
                }
            };
            btn.onclick = function () {
                /*先判断猫还是狗,然后往里面添加内容*/
                if (onOff === true) {
                    main[0].innerHTML += “<div class=\”lineBox\”>” + “<img class=\”imgWhite\”>” + “<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 class=\”imgBlue\”>” + “<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 class=”container”>
        <div class=”main”>
            <div class=”lineBox”>
                <img src=”http://cdn.attach.qdfuns.com/notes/pics/201710/09/093845xis8s3pd3eks8ysq.jpg” alt=”” class=”imgBlue”>
                <p class=”textM textBlue”>你说人类造词怎么总往我们身上扯?
                </p>
            </div>
            <div class=”lineBox”>
                <img src=”http://cdn.attach.qdfuns.com/notes/pics/201710/09/093845pf6rp42pn43qn4zp.jpg” alt=”” class=”imgWhite”>
                <p class=”textM textWhite”>可不是嘛?就好像他们老爱说‘懒猫’‘懒猫’的,我们猫可不懒,虽然白天看起来无所事事,一到晚上我们可是都竖着耳朵听动静呢,抓老鼠抓蟑螂抓虫子的时候他们人类还不都在呼呼大睡?
                </p>
            </div>
            <div class=”lineBox”>
                <img src=”http://cdn.attach.qdfuns.com/notes/pics/201710/09/093845xis8s3pd3eks8ysq.jpg” alt=”” class=”imgBlue”>
                <p class=”textM textBlue”>是啊,其实真正懒的是他们人类。你看我们主人就知道了,经常一觉睡到日上三竿,不运动又好吃,还天天叫嚷减肥呢,真是笑死人了!
                </p>
            </div>
        </div>
        <div class=”inputBox”>
            <img src=”http://cdn.attach.qdfuns.com/notes/pics/201710/09/093845pf6rp42pn43qn4zp.jpg” alt=”” class=”img1″>
            <div class=”textBox”>
                <input type=”text” id=”text”>
            </div>
            <input type=”button” value=”发送” id=”btn”>
        </div>
    </div>
</body>
</html>

前端开发中支付开发流程|前端工程化开发流程图|前端开发各浏览器兼容问题

赞(0)
前端开发者 » 前端开发交互小练习_JS聊天界面
64K

评论 抢沙发

评论前必须登录!