回答編集履歴

1

ソースをひとまとめにして追記

2019/10/06 09:47

投稿

siruku6
siruku6

スコア1382

test CHANGED
@@ -201,3 +201,253 @@
201
201
 
202
202
 
203
203
  ```
204
+
205
+
206
+
207
+ ### 追記
208
+
209
+
210
+
211
+ 次のような一つのhtmlファイルの状態にして、ファイルをダブルクリックしたところ、動作しました
212
+
213
+
214
+
215
+ 残念なところとしては、隠れた部分を表示したとき、カーソルの位置と文字の位置が合っていないところです。
216
+
217
+ あくまで、できたのは「表示するだけ」になってしまっております....
218
+
219
+
220
+
221
+ ```html
222
+
223
+ <!DOCTYPE html>
224
+
225
+ <html lang="ja">
226
+
227
+
228
+
229
+ <head>
230
+
231
+ <title>Team1 Website</title>
232
+
233
+ <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
234
+
235
+
236
+
237
+ <style>
238
+
239
+ .box {
240
+
241
+ position: relative;
242
+
243
+ width: 200px;
244
+
245
+ height: 40px;
246
+
247
+ margin: 0;
248
+
249
+ }
250
+
251
+ .dammy,
252
+
253
+ .copy {
254
+
255
+ border: none;
256
+
257
+ padding: 0.75rem 1rem;
258
+
259
+ width: 100%;
260
+
261
+ height: 40px;
262
+
263
+ line-height: 1.5;
264
+
265
+ min-height: 40px;
266
+
267
+ font-size: 1.5rem;
268
+
269
+ font-family: sans-serif;
270
+
271
+ white-space: nowrap;
272
+
273
+ overflow: hidden;
274
+
275
+ }
276
+
277
+ .dammy {
278
+
279
+ position: absolute;
280
+
281
+ z-index: 1;
282
+
283
+ caret-color: blue;
284
+
285
+ border: none;
286
+
287
+ background: transparent;
288
+
289
+ color: transparent;
290
+
291
+ resize: none;
292
+
293
+ }
294
+
295
+ .copy {
296
+
297
+ height: auto;
298
+
299
+ position: relative;
300
+
301
+ z-index: 0;
302
+
303
+ background: #ddd;
304
+
305
+ }
306
+
307
+ .copy .within {
308
+
309
+ color: blue;
310
+
311
+ white-space: nowrap;
312
+
313
+ display: inline;
314
+
315
+ }
316
+
317
+ .copy .overed {
318
+
319
+ color: red;
320
+
321
+ white-space: nowrap;
322
+
323
+ display: inline;
324
+
325
+ }
326
+
327
+
328
+
329
+ /* style-sheet 追加 */
330
+
331
+ .right-aligned {
332
+
333
+ position: sticky;
334
+
335
+ right: 0;
336
+
337
+ }
338
+
339
+ </style>
340
+
341
+
342
+
343
+ <script>
344
+
345
+ // 入力
346
+
347
+ $( document ).on( 'input', '.dammy', function( e ){
348
+
349
+ const $dammy = $( this );
350
+
351
+ // 改行禁止
352
+
353
+ enter( $dammy, e );
354
+
355
+ // コピー
356
+
357
+ const lim = 5;
358
+
359
+ copy( $dammy, lim );
360
+
361
+ });
362
+
363
+
364
+
365
+ function enter( $dammy, e ){
366
+
367
+ $dammy.on( 'keydown', function( e ) {
368
+
369
+ // 改行禁止
370
+
371
+ if ( e.which == 13 ) {
372
+
373
+ return false;
374
+
375
+ // 横矢印キー押下時の動作
376
+
377
+ } else if ( e.which == 39 ) {
378
+
379
+ $('.within').addClass('right-aligned');
380
+
381
+ } else if ( e.which == 37 ) {
382
+
383
+ $('.within').removeClass('right-aligned');
384
+
385
+ }
386
+
387
+ });
388
+
389
+ const val = $dammy.val();
390
+
391
+ const reg = new RegExp( "[\r\n]", "g" );
392
+
393
+ if ( val.match(reg) ) {
394
+
395
+ const replace = val.replace( reg, '' );
396
+
397
+ $dammy.val( replace );
398
+
399
+ }
400
+
401
+ }
402
+
403
+
404
+
405
+ // コピー
406
+
407
+ function copy( $dammy, lim ){
408
+
409
+ const $copy = $dammy.next( '.copy' );
410
+
411
+ const val = $dammy.val();
412
+
413
+ // if ( val.length > lim ) {
414
+
415
+ const within = val //.substr( 0, lim );
416
+
417
+ // const overed = val.slice( lim );
418
+
419
+ $copy.html('<div class="within">' +within+ '</div>');
420
+
421
+ // $copy.append('<div class="overed">' +overed+ '</div>');
422
+
423
+ // } else {
424
+
425
+ // $copy.html('<div><div class="within">' +val+ '</div></div>');
426
+
427
+ // }
428
+
429
+ }
430
+
431
+ </script>
432
+
433
+ </head>
434
+
435
+
436
+
437
+ <body>
438
+
439
+ <div class="box">
440
+
441
+ <textarea class="dammy"></textarea>
442
+
443
+ <div class="copy"></div>
444
+
445
+ </div>
446
+
447
+ </body>
448
+
449
+
450
+
451
+ </html>
452
+
453
+ ```