forked from zhangmengxue/Fragment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js计数发布.html
160 lines (157 loc) · 3.59 KB
/
js计数发布.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>js类似微博发布框demo</title>
<style type="text/css">
body{
margin:0;
padding:0;
border:0;
}
#wrapper{
width:620px;
height:160px;
padding:15px;
margin:0 auto;
position:relative;
border:2px #261d3b solid;
border-radius: 2px;
background-color: #261d3b;
}
#share{
display:block;
padding:5px;
color:#b377e3;
}
#num{
display:block;
position:absolute;
right:36px;
top:18px;
padding:5px;
color:#b377e3;
}
textarea{
display:block;
width:600px;
height:100px;
outline: none;
overflow:auto;/* 去掉ie下textarea的默认滚动条 */
border-width: 1px;
background-color: #5b577c;
}
.dis,.able{
display:block;
float:right;
margin-right: 16px;
margin-top: 5px;
width:76px;
height:32px;
background-color: #ffc09f;
color:#fff;
border:2px #ffc09f solid;
border-radius: 4px;
cursor: pointer;
}
.dis{
background-color: #ffc09f;
border:2px #ffc09f solid;
}
.able{
background-color: #f77c3d;
border:2px #f77c3d solid;
}
</style>
</head>
<body>
<div id="wrapper">
<span id="share">分享新鲜事儿</span>
<span id="num"><span id="count">0</span>/150</span>
<textarea placeholder="请输入你的分享150字以内"></textarea>
<button class="dis">发布</button>
</div>
<script type="text/javascript">
window.onload = function(){
var she = new share();
she.getFocus();
she.getBlur();
she.getPost();
she.Post();
}
function share(){
this.count = document.getElementById('count');
this.txt = document.getElementsByTagName('textarea')[0];
this.btn = document.getElementsByTagName('button')[0];
}
share.prototype = {
/*textarea获得焦点*/
getFocus : function(){
var that = this;
this.txt.onfocus = function(){
this.style.borderColor = '#f77c3d' ;
}
},
/*textarea失去焦点*/
getBlur : function(){
this.txt.onblur = function(){
this.style.borderColor = '#5b577c';
}
},
/*兼容ie输入改变事件*/
getPost : function(){
var ie = !-[1,];
var that = this;
if(ie){
this.txt.onpropertychange = function(){
that.getInputlen();
}
}else{
this.txt.oninput = function(){
that.getInputlen();
}
}
},
/*计数输入长度*/
getInputlen : function(){
var len = this.txt.value.length;
this.count.innerHTML = len;
if(len>=1&&len<150){
this.btn.className = 'able';
}else if(len == 0){
this.btn.className = 'dis';
}else if(len >= 150){
this.btn.className = 'dis';
this.count.innerHTML = '已超出'+(len-150)+'个字';
}
},
/*发布控制*/
Post : function(){
var timer = '';
var num = 0;
var that = this;
this.btn.onclick = function(){
if(that.btn.className =='dis'){
clearInterval(timer);
timer = setInterval(function(){
if(num == 5){
clearInterval(timer);
num = 0;
}else
num++;
if(num%2){
that.txt.style.backgroundColor = '#ffc09f';
}else{
that.txt.style.backgroundColor = '';
}
},200);
}else{
alert('发布成功!');
that.txt.value = '';
}
}
}
}
</script>
</body>
</html>