回答編集履歴
3
文字数増加
test
CHANGED
@@ -52,7 +52,7 @@
|
|
52
52
|
|
53
53
|
font-size: 18px;
|
54
54
|
|
55
|
-
width: 9
|
55
|
+
width: 190px;
|
56
56
|
|
57
57
|
height: 20px;
|
58
58
|
|
@@ -66,8 +66,6 @@
|
|
66
66
|
|
67
67
|
.twoLines {
|
68
68
|
|
69
|
-
padding: 15px;
|
70
|
-
|
71
69
|
height: 40px;
|
72
70
|
|
73
71
|
}
|
@@ -76,8 +74,6 @@
|
|
76
74
|
|
77
75
|
.threeLines {
|
78
76
|
|
79
|
-
padding: 20px;
|
80
|
-
|
81
77
|
height: 60px;
|
82
78
|
|
83
79
|
}
|
@@ -240,7 +236,7 @@
|
|
240
236
|
|
241
237
|
let num = 0;
|
242
238
|
|
243
|
-
if (len <=
|
239
|
+
if (len <= 10) {
|
244
240
|
|
245
241
|
num = br + 1
|
246
242
|
|
@@ -248,7 +244,7 @@
|
|
248
244
|
|
249
245
|
|
250
246
|
|
251
|
-
if (len >
|
247
|
+
if (len > 10 && len <= 20) {
|
252
248
|
|
253
249
|
num = br + 2
|
254
250
|
|
@@ -256,7 +252,7 @@
|
|
256
252
|
|
257
253
|
|
258
254
|
|
259
|
-
if (len >
|
255
|
+
if (len > 20 && len <= 30) {
|
260
256
|
|
261
257
|
num = br + 3
|
262
258
|
|
@@ -264,7 +260,7 @@
|
|
264
260
|
|
265
261
|
|
266
262
|
|
267
|
-
if (len >
|
263
|
+
if (len > 30) {
|
268
264
|
|
269
265
|
num = br + 4
|
270
266
|
|
2
説明追記
test
CHANGED
@@ -6,7 +6,21 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
下記コードをコピペすると動きます
|
9
|
+
下記コードをコピペすると多分動きます。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
確認OS
|
14
|
+
|
15
|
+
mac
|
16
|
+
|
17
|
+
確認ブラウザ
|
18
|
+
|
19
|
+
safari
|
20
|
+
|
21
|
+
firefox
|
22
|
+
|
23
|
+
chrome
|
10
24
|
|
11
25
|
|
12
26
|
|
1
コード追記
test
CHANGED
@@ -1,7 +1,311 @@
|
|
1
1
|
Javascriptを使えば可能です。
|
2
2
|
|
3
|
-
改行なしの場合、改行1回の場合、改行2回の場合
|
3
|
+
改行なしの場合、改行1回の場合、改行2回の場合など
|
4
|
-
|
5
|
-
など
|
6
4
|
|
7
5
|
それぞれの場合に応じたclassを用意しそのclassを付けたり外したりすることで実現出来ます。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
下記コードをコピペすると動きます
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
```html
|
14
|
+
|
15
|
+
<!DOCTYPE html>
|
16
|
+
|
17
|
+
<html lang="en">
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
<head>
|
22
|
+
|
23
|
+
<meta charset="UTF-8">
|
24
|
+
|
25
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
26
|
+
|
27
|
+
<title>Document</title>
|
28
|
+
|
29
|
+
<link href="https://fonts.googleapis.com/css2?family=Kosugi&display=swap" rel="stylesheet">
|
30
|
+
|
31
|
+
<style>
|
32
|
+
|
33
|
+
.a {
|
34
|
+
|
35
|
+
font-family: 'Kosugi', sans-serif;
|
36
|
+
|
37
|
+
resize: none;
|
38
|
+
|
39
|
+
font-size: 18px;
|
40
|
+
|
41
|
+
width: 95px;
|
42
|
+
|
43
|
+
height: 20px;
|
44
|
+
|
45
|
+
padding: 10px;
|
46
|
+
|
47
|
+
line-height: 20px;
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
.twoLines {
|
54
|
+
|
55
|
+
padding: 15px;
|
56
|
+
|
57
|
+
height: 40px;
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
.threeLines {
|
64
|
+
|
65
|
+
padding: 20px;
|
66
|
+
|
67
|
+
height: 60px;
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
.fourLines {
|
74
|
+
|
75
|
+
height: 80px;
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
</style>
|
80
|
+
|
81
|
+
</head>
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
<body>
|
86
|
+
|
87
|
+
<textarea name="a" class="a"></textarea>
|
88
|
+
|
89
|
+
<script>
|
90
|
+
|
91
|
+
const textarea = document.getElementsByTagName("textarea")[0];
|
92
|
+
|
93
|
+
textarea.addEventListener("input", () => {
|
94
|
+
|
95
|
+
const text = replaceBr(textarea.value);
|
96
|
+
|
97
|
+
const target = event.currentTarget;
|
98
|
+
|
99
|
+
numOfLine(countBr(text), countLen(countBr(text), text), target);
|
100
|
+
|
101
|
+
});
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
const replaceBr = (value) => {
|
106
|
+
|
107
|
+
return value.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
const countBr = (text) => {
|
114
|
+
|
115
|
+
const count = (text.match(/\n/g) || []).length;
|
116
|
+
|
117
|
+
switch (count) {
|
118
|
+
|
119
|
+
case 0:
|
120
|
+
|
121
|
+
return 0;
|
122
|
+
|
123
|
+
break;
|
124
|
+
|
125
|
+
case 1:
|
126
|
+
|
127
|
+
return 1;
|
128
|
+
|
129
|
+
break;
|
130
|
+
|
131
|
+
case 2:
|
132
|
+
|
133
|
+
return 2;
|
134
|
+
|
135
|
+
break;
|
136
|
+
|
137
|
+
case 3:
|
138
|
+
|
139
|
+
return 3;
|
140
|
+
|
141
|
+
break;
|
142
|
+
|
143
|
+
default:
|
144
|
+
|
145
|
+
return 4;
|
146
|
+
|
147
|
+
break;
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
const countLen = (br, text) => {
|
156
|
+
|
157
|
+
switch (br) {
|
158
|
+
|
159
|
+
case 0:
|
160
|
+
|
161
|
+
return countVal(text);
|
162
|
+
|
163
|
+
break;
|
164
|
+
|
165
|
+
case 1:
|
166
|
+
|
167
|
+
return countVal(text.split('\n')[1]);
|
168
|
+
|
169
|
+
break;
|
170
|
+
|
171
|
+
case 2:
|
172
|
+
|
173
|
+
return countVal(text.split('\n')[2]);
|
174
|
+
|
175
|
+
break;
|
176
|
+
|
177
|
+
case 3:
|
178
|
+
|
179
|
+
return countVal(text.split('\n')[3]);
|
180
|
+
|
181
|
+
break;
|
182
|
+
|
183
|
+
default:
|
184
|
+
|
185
|
+
return 0;
|
186
|
+
|
187
|
+
break;
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
const countVal = (text) => {
|
196
|
+
|
197
|
+
let count = 0;
|
198
|
+
|
199
|
+
for (let i = 0, maxLen = text.length; i < maxLen; i++) {
|
200
|
+
|
201
|
+
const chr = text[i];
|
202
|
+
|
203
|
+
if (chr.match(/\n/)) {
|
204
|
+
|
205
|
+
count += 0;
|
206
|
+
|
207
|
+
} else if (!chr.match(/[^\x01-\x7E]/) || !chr.match(/[^\uFF65-\uFF9F]/)) {
|
208
|
+
|
209
|
+
count += 0.5;
|
210
|
+
|
211
|
+
} else {
|
212
|
+
|
213
|
+
count += 1;
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
return count;
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
const numOfLine = (br, len, target) => {
|
226
|
+
|
227
|
+
let num = 0;
|
228
|
+
|
229
|
+
if (len <= 5) {
|
230
|
+
|
231
|
+
num = br + 1
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
if (len > 5 && len <= 10) {
|
238
|
+
|
239
|
+
num = br + 2
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
if (len > 10 && len <= 15) {
|
246
|
+
|
247
|
+
num = br + 3
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
if (len > 15) {
|
254
|
+
|
255
|
+
num = br + 4
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
judgeLine(num, target);
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
const judgeLine = (num, target) => {
|
266
|
+
|
267
|
+
if (num == 1) {
|
268
|
+
|
269
|
+
target.classList.remove("twoLines");
|
270
|
+
|
271
|
+
target.classList.remove("threeLines");
|
272
|
+
|
273
|
+
target.classList.remove("fourLines");
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
if (num == 2) {
|
278
|
+
|
279
|
+
target.classList.add("twoLines");
|
280
|
+
|
281
|
+
target.classList.remove("threeLines");
|
282
|
+
|
283
|
+
target.classList.remove("fourLines");
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
if (num == 3) {
|
288
|
+
|
289
|
+
target.classList.add("threeLines");
|
290
|
+
|
291
|
+
target.classList.remove("fourLines");
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
if (num > 3) {
|
296
|
+
|
297
|
+
target.classList.add("fourLines");
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
</script>
|
304
|
+
|
305
|
+
</body>
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
</html>
|
310
|
+
|
311
|
+
```
|