本文实例为大家分享了js实现微信聊天界面的具体代码,供大家参考,具体内容如下
效果如图:
代码部分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<!doctype html> <html lang= "en" > <head> <meta charset= "utf-8" > <title>模拟微信聊天</title> <link rel= "stylesheet" href= "../css/模拟微信聊天.css" rel= "external nofollow" > </head> <body> <div class= "box" > <div class= "phone" > <div class= "header" ></div> <div class= "content" > <ul class= "list" > </ul> <div class= "content-bottom" > <img src= "../image/one.jpeg" width= "45px" height= "45px" id= "pic" > <input type= "text" placeholder= "请输入聊天内容" class= "myinput" > <input type= "button" value= "发送" class= "out" > </div> </div> <div class= "footer" ></div> </div> </div> </body> <script src= "../js/模拟微信聊天.js" ></script> </html> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
*{ margin:0; padding:0; } body{ margin:0; padding:0; } .box{ margin:100px auto; height:900px; width:1200px; background:linear-gradient(to right,violet,pink); } .phone{ top:50px; position:relative; width:500px; height:800px; background: #f6f6f6; left:50%; margin-left:-250px; border-radius: 30px; } .phone .header{ position: absolute; width:135px; height:5px; background:rgba(0,0,0,0.8); -webkit-border-radius: 80px; -moz-border-radius: 80px; border-radius: 80px; left:50%; margin-left:-67.5px; top:40px; } .phone .content{ position: absolute; top:75px; width:460px; height: 640px; background: #fff; left:50%; margin-left:-230px; border-radius: 1px; border:2px solid gray; } .phone .footer{ position: absolute; width:50px; height:50px; background:pink; -webkit-border-radius: 50%; -moz-border-radius: 50%; border-radius: 50%; bottom:15px; left:50%; margin-left:-25px; } .content .list{ height:575px; list-style-type: none; overflow: hidden; } .list li{ margin-top:30px; } .content .list .left img{ display:inline-block; margin: 0; float: left; margin-left:15px; } .content .list .left p{ color: #333333; margin-left:78px; width:320px; padding:20px 10px; font-size:20px; background:pink; border-radius: 6px; text-align: left; } .content .list .right img{ display:inline-block; margin: 0; float:right; margin-right:15px; } .content .list .right p { text-align: right; color: #333333; margin-left: 37px; width: 320px; padding: 20px 10px; font-size: 20px; background: pink; border-radius: 6px; } .content-bottom{ overflow: hidden; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; height: 65px; background:rgba(0,0,0,0.2); padding:10px 30px; position: relative; } .content-bottom .myinput{ display: inline-block; height:30px; width:250px; font-size:20px; border-radius:5px; border:1px solid #999999; text-indent:10px; outline:none; position:absolute; top:50%; margin-top: -15px; margin-left:20px; } .content-bottom .out{ height:32px; width: 60px; display: inline-block; position: absolute; right:30px; top:50%; border:1px solid #999999; margin-top: -16px; font-size:17px; outline: none; background:none; cursor:pointer; } .content-bottom .out:hover{ background-color: pink; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
var out = document.queryselector( ".out" ); //获取发送按钮 var myinput = document.queryselector( ".myinput" ); var pic = document.getelementbyid( "pic" ); //获取图片 var onoff = true ; var list = document.queryselector( ".list" ); //获取ul pic.onclick= function (){ if (onoff){ pic.src = "../image/two.jpeg" ; onoff = false ; } else { pic.src = "../image/one.jpeg" ; onoff = true ; } }; out.onclick= function () { var value = myinput.value; //获取文字内容 if (value == "" ){ alert( "请输入内容" ); } else { if (onoff){ list.innerhtml += "<li class='left'><img src='../image/one.jpeg'/><p>" +value+ "</p></li>" ; } else { list.innerhtml += "<li class='right'><img src='../image/two.jpeg'/><p>" +value+ "</p></li>" ; } } myinput.blur(myinput.value= '' ); //失去焦点,文本框清空 }; |
学习收获:加深对if语句的理解
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_45596380/article/details/106044058