案例1-微信聊天-var onOff = true


<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Document</title>

    <style>

        *{

            margin: 0;

            padding: 0;

        }

        ul{

            list-style: none;

        }

        img{

            vertical-align: top;

        }

        .wrap{

            width: 1000px;

            height: 800px;

            background: url(img/bg.jpg) no-repeat;

            position: absolute;

            left: 50%;

            top: 50%;

            margin-top: -400px;

            margin-left: -500px;

        }

        .iphone{

            width: 356px;

            height: 679px;

            background: url(img/iphone.png) no-repeat;

            position: absolute;

            left: 50%;

            top: 50%;

            margin-left: -178px;

            margin-top: -338.5px;

        }

        .list{

            width: 318px;

            height: 498px;

            margin: 47px 0 0 19px;

            padding: 20px;

            overflow: auto;

            box-sizing: border-box;

        }

        .list li::after{

            content: '';

            display: block;

            clear: both;

        }

        .list img{

            width: 35px;

            height: 40px;

        }

        .list p{

            width: 230px;

            box-sizing: border-box;

            padding: 10px;

            border-radius: 10px;

        }

        .nav{

            height: 60px;

            background: #e5e5e5;

            width: 318px;

            margin-left:19px;

        }

        .nav::after{

            content: '';

            display: block;

            clear: both;

        }

        .nav img{

            padding: 10px 0 0 16px;

            float: left;

        }

        .nav input{

            float: left;

            width: 190px;

            height: 40px;

            border: none;

            background: #fff;

            border-radius: 10px;

            margin: 11px 0 0 13px;

        }

        .nav button{

            width: 64px;

            height: 40px;

            font: 16px / 40px "微软雅黑";

            color: #e15671;

            margin-top: 11px;

            text-align: center;

            border: none;

            background: none;

        }

        .right,.left{

            margin-bottom: 20px;

        }

        .right img{

            float: right;

        }

        .right p{

            float: right;

            background: #bab3ce;

            color: #fff;

            margin-right: 13px;

        }

        .left img{

            float: left;

        }

        .left p{

            float: left;

            background: #e5e5e5;

            margin-left: 13px;

        }


    </style>

</head>

<body>

    <div class="wrap">

        <div class="iphone">

            <ul class="list">

                <li class="right">

                    <img src="img/monster2.png" alt="">

                    <p>说:哈?好呀,多少钱我都愿意~~呵呵</p>

                </li>

                <li class="left">

                    <img src="img/monster.png" alt="">

                    <p>说:这个简单,我们去妙味远程课堂学就行啦~~~</p>

                </li>

                <!-- <li class="right">

                    <img src="img/monster2.png" alt="">

                    <p>说:好呀~~~你先帮我学会JS再说~~</p>

                </li>

                <li class="left">

                    <img src="img/monster.png" alt="">

                    <p>说:咦?土豪??</p>

                </li>

                <li class="right">

                    <img src="img/monster2.png" alt="">

                    <p>说:大家好,我是土豪~</p>

                </li> -->

            </ul>

            <div class="nav">

                <img id="pic" src="img/monster2.png" alt="">

                <input id="txt" type="text">

                <button id="btn">发送</button>

            </div>

        </div>

    </div>

    <script>

        /*

        切换发送的头像

        1-获取头像的图片的元素

        2-绑定点击事件

        3-切换图片的src(注意需要判断,应该是哪个图片)

        */

        var pic = document.getElementById('pic');

        var onOff = true;

        pic.onclick = function(){

            if(onOff){

                pic.src = 'img/monster.png';

                onOff = false;

            }else{

                pic.src = 'img/monster2.png';

                onOff = true;

            }

        };

        /*

            添加内容进ul

            1-获取元素(按钮、头像、文字、list)

            2-给按钮绑定点击时间

            3-获取头像地址,以及文字内容(如果内容为空不允许发送)

            4-扔到list里面去

        */


        var btn = document.getElementById('btn');

        var txt = document.getElementById('txt');

        var list = document.querySelector('.list');

        btn.onclick = function(){

            var value = txt.value;

            if(value == ''){

                alert('内容为空,不允许发送');

            }else{

                if(onOff){

                    list.innerHTML = '<li class="left"><img src="img/monster2.png" alt=""><p>'+value+'</p></li>' + list.innerHTML;

                }else{

                    list.innerHTML = '<li class="right"><img src="img/monster.png" alt=""><p>'+value+'</p></li>' + list.innerHTML;

                }

            }

        };

    </script>

</body>

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 不会用代码框,所以看着有些乱套,,,,html部分 <!DOCTYPE html> 迅雷看看 ...
    这就是个帅气的名字阅读 5,640评论 0 0
  • 各种纯css图标 CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出。直接用CSS3画出这些图形,要比...
    剑残阅读 13,301评论 0 8
  • 我的公共CSS文件@import "normalize.css";/begin主页面//begin主页面/body...
    蓝色海洋_1982阅读 4,966评论 0 0
  • 1. tab列表折叠效果 html: 能源系统事业部 岗位名称: 工作地点 岗位名...
    lilyping阅读 5,910评论 0 1
  • 一、任务1:验证注册页面中的电子邮箱代码:1.html文件 ("#formRegister").submit...
    ZxYi阅读 4,155评论 0 0

友情链接更多精彩内容