回答編集履歴

3

調整

2017/11/15 03:35

投稿

yambejp
yambejp

スコア114769

test CHANGED
@@ -366,6 +366,8 @@
366
366
 
367
367
  me.addClass('active');
368
368
 
369
+ location.href=me.attr('href');
370
+
369
371
  });
370
372
 
371
373
  });

2

追記

2017/11/15 03:35

投稿

yambejp
yambejp

スコア114769

test CHANGED
@@ -229,3 +229,235 @@
229
229
  </html>
230
230
 
231
231
  ```
232
+
233
+
234
+
235
+ # クリックしたところを必ずアクティブにする
236
+
237
+
238
+
239
+ ```javascript
240
+
241
+ <html>
242
+
243
+ <head>
244
+
245
+ <style>
246
+
247
+ #menu-wrap {
248
+
249
+ position: fixed;
250
+
251
+ }
252
+
253
+ #menu{
254
+
255
+ text-align: left;
256
+
257
+ display: flex;
258
+
259
+ margin: 0px;
260
+
261
+ padding-left: 20px;
262
+
263
+ }
264
+
265
+ #menu li {
266
+
267
+ display:inline;
268
+
269
+ }
270
+
271
+ #menu li:last-child a{
272
+
273
+ border-right:1px solid #000000;
274
+
275
+ }
276
+
277
+ #menu a:hover{
278
+
279
+ background-Color:yellow;
280
+
281
+ }
282
+
283
+ #menu a{
284
+
285
+ width: 80px;
286
+
287
+ border:1px solid #000000;
288
+
289
+ border-right:0px;
290
+
291
+ padding-left: 5px;
292
+
293
+ padding-right: 5px;
294
+
295
+ display: inline-block;
296
+
297
+ text-decoration:none;
298
+
299
+ }
300
+
301
+ .contents {
302
+
303
+ height: 500px;
304
+
305
+ }
306
+
307
+ .active {
308
+
309
+ background-color: red;
310
+
311
+ }
312
+
313
+ #content_first,#content_third,#content_etc{background-Color:aqua;}
314
+
315
+ #content_second,#content_fourth{background-Color:lime;}
316
+
317
+
318
+
319
+ </style>
320
+
321
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
322
+
323
+ <script>
324
+
325
+ $(function() {
326
+
327
+ var list=[];
328
+
329
+ ['#content_first',
330
+
331
+ '#content_second',
332
+
333
+ '#content_third',
334
+
335
+ '#content_fourth',
336
+
337
+ '#content_etc'
338
+
339
+ ].map(function(x){
340
+
341
+ list.push({
342
+
343
+ "min": $(x).offset().top,
344
+
345
+ "max": $(x).next().length>0?$(x).next().offset().top-1:($(x).offset().top+$(x).height()),
346
+
347
+ "id": x,
348
+
349
+ })});
350
+
351
+ var pre_pos = 0;
352
+
353
+ $(function() {
354
+
355
+ $('#menu a[href="' + location.hash + '"]').addClass('active');
356
+
357
+ $('#menu a').on('click', function(e) {
358
+
359
+ e.preventDefault();
360
+
361
+ var me=$(this);
362
+
363
+ $.when($('html,body').animate({scrollTop:$($(this).attr("href")).offset().top }, 500, 'swing')).done(function(){
364
+
365
+ $('#menu a').removeClass('active');
366
+
367
+ me.addClass('active');
368
+
369
+ });
370
+
371
+ });
372
+
373
+ });
374
+
375
+
376
+
377
+ $(document).on('scroll', function() {
378
+
379
+ var this_pos = $(window).scrollTop();
380
+
381
+ var pre_id = get_id(pre_pos);
382
+
383
+ var this_id = get_id(this_pos);
384
+
385
+ if (pre_id !== this_id) {
386
+
387
+ if(this_id!==null){
388
+
389
+ location.hash=this_id.substr(1,this_id.length);
390
+
391
+ $(window).scrollTop(this_pos);
392
+
393
+ }else{
394
+
395
+ location.hash="";
396
+
397
+ };
398
+
399
+ $('#menu a').removeClass("active");
400
+
401
+ $('[href="' + this_id + '"]').addClass("active");
402
+
403
+ }
404
+
405
+ pre_pos = this_pos;
406
+
407
+ });
408
+
409
+ function get_id(pos) {
410
+
411
+ var arr = list.filter(function(x) {
412
+
413
+ return x.min <= pos && x.max >= pos;
414
+
415
+ });
416
+
417
+ if (typeof arr[0] == "undefined") return null;
418
+
419
+ return arr[0].id;
420
+
421
+ }
422
+
423
+ });
424
+
425
+ </script>
426
+
427
+ </head>
428
+
429
+ <body>
430
+
431
+ <nav id="menu-wrap">
432
+
433
+ <ul id="menu">
434
+
435
+ <li><a href="#content_first">Contents1</a></li>
436
+
437
+ <li><a href="#content_second">Contents2</a></li>
438
+
439
+ <li><a href="#content_third">Contents3</a></li>
440
+
441
+ <li><a href="#content_fourth">Contents4</a></li>
442
+
443
+ <li><a href="#content_etc">Contents5</a></li>
444
+
445
+ </ul>
446
+
447
+ </nav>
448
+
449
+ <div class="contents" id="content_first">1</div>
450
+
451
+ <div class="contents" id="content_second">2</div>
452
+
453
+ <div class="contents" id="content_third">3</div>
454
+
455
+ <div class="contents" id="content_fourth">4</div>
456
+
457
+ <div class="contents" id="content_etc">5</div>
458
+
459
+ </body>
460
+
461
+ </html>
462
+
463
+ ```

1

追記

2017/11/15 03:31

投稿

yambejp
yambejp

スコア114769

test CHANGED
@@ -1 +1,231 @@
1
1
  先日回答した[メニューバーで現在のページをハイライトさせたい](https://teratail.com/questions/99590)に近いかもしれません
2
+
3
+
4
+
5
+ # 改良版
6
+
7
+
8
+
9
+ スクロール時のアニメーションがほしいとのことなので、一部改良版を上げておきます
10
+
11
+ (スクロールがみやすくなるよう背景色をつけてあります)
12
+
13
+ ```javascript
14
+
15
+ <html>
16
+
17
+ <head>
18
+
19
+ <style>
20
+
21
+ #menu-wrap {
22
+
23
+ position: fixed;
24
+
25
+ }
26
+
27
+ #menu{
28
+
29
+ text-align: left;
30
+
31
+ display: flex;
32
+
33
+ margin: 0px;
34
+
35
+ padding-left: 20px;
36
+
37
+ }
38
+
39
+ #menu li {
40
+
41
+ display:inline;
42
+
43
+ }
44
+
45
+ #menu li:last-child a{
46
+
47
+ border-right:1px solid #000000;
48
+
49
+ }
50
+
51
+ #menu a:hover{
52
+
53
+ background-Color:yellow;
54
+
55
+ }
56
+
57
+ #menu a{
58
+
59
+ width: 80px;
60
+
61
+ border:1px solid #000000;
62
+
63
+ border-right:0px;
64
+
65
+ padding-left: 5px;
66
+
67
+ padding-right: 5px;
68
+
69
+ display: inline-block;
70
+
71
+ text-decoration:none;
72
+
73
+ }
74
+
75
+ .contents {
76
+
77
+ height: 500px;
78
+
79
+ }
80
+
81
+ .active {
82
+
83
+ background-color: red;
84
+
85
+ }
86
+
87
+ #content_first,#content_third,#content_etc{background-Color:aqua;}
88
+
89
+ #content_second,#content_fourth{background-Color:lime;}
90
+
91
+
92
+
93
+ </style>
94
+
95
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
96
+
97
+ <script>
98
+
99
+ $(function() {
100
+
101
+ var list=[];
102
+
103
+ ['#content_first',
104
+
105
+ '#content_second',
106
+
107
+ '#content_third',
108
+
109
+ '#content_fourth',
110
+
111
+ '#content_etc'
112
+
113
+ ].map(function(x){
114
+
115
+ list.push({
116
+
117
+ "min": $(x).offset().top,
118
+
119
+ "max": $(x).next().length>0?$(x).next().offset().top-1:($(x).offset().top+$(x).height()),
120
+
121
+ "id": x,
122
+
123
+ })});
124
+
125
+ var pre_pos = 0;
126
+
127
+ $(function() {
128
+
129
+ $('#menu a[href="' + location.hash + '"]').addClass('active');
130
+
131
+ $('#menu a').on('click', function() {
132
+
133
+ $('#menu a').removeClass('active');
134
+
135
+ $(this).addClass('active');
136
+
137
+ $('html,body').animate({scrollTop:$($(this).attr("href")).offset().top }, 500, 'swing');
138
+
139
+ });
140
+
141
+ });
142
+
143
+
144
+
145
+ $(document).on('scroll', function() {
146
+
147
+ var this_pos = $(window).scrollTop();
148
+
149
+ var pre_id = get_id(pre_pos);
150
+
151
+ var this_id = get_id(this_pos);
152
+
153
+ if (pre_id !== this_id) {
154
+
155
+ if(this_id!==null){
156
+
157
+ location.hash=this_id.substr(1,this_id.length);
158
+
159
+ $(window).scrollTop(this_pos);
160
+
161
+ }else{
162
+
163
+ location.hash="";
164
+
165
+ };
166
+
167
+ $('#menu a').removeClass("active");
168
+
169
+ $('[href="' + this_id + '"]').addClass("active");
170
+
171
+ }
172
+
173
+ pre_pos = this_pos;
174
+
175
+ });
176
+
177
+ function get_id(pos) {
178
+
179
+ var arr = list.filter(function(x) {
180
+
181
+ return x.min <= pos && x.max >= pos;
182
+
183
+ });
184
+
185
+ if (typeof arr[0] == "undefined") return null;
186
+
187
+ return arr[0].id;
188
+
189
+ }
190
+
191
+ });
192
+
193
+ </script>
194
+
195
+ </head>
196
+
197
+ <body>
198
+
199
+ <nav id="menu-wrap">
200
+
201
+ <ul id="menu">
202
+
203
+ <li><a href="#content_first">Contents1</a></li>
204
+
205
+ <li><a href="#content_second">Contents2</a></li>
206
+
207
+ <li><a href="#content_third">Contents3</a></li>
208
+
209
+ <li><a href="#content_fourth">Contents4</a></li>
210
+
211
+ <li><a href="#content_etc">Contents5</a></li>
212
+
213
+ </ul>
214
+
215
+ </nav>
216
+
217
+ <div class="contents" id="content_first">1</div>
218
+
219
+ <div class="contents" id="content_second">2</div>
220
+
221
+ <div class="contents" id="content_third">3</div>
222
+
223
+ <div class="contents" id="content_fourth">4</div>
224
+
225
+ <div class="contents" id="content_etc">5</div>
226
+
227
+ </body>
228
+
229
+ </html>
230
+
231
+ ```