回答編集履歴

2

修正

2017/12/15 14:31

投稿

退会済みユーザー
test CHANGED
@@ -354,11 +354,11 @@
354
354
 
355
355
  if (data[id] >= scrollTop) {
356
356
 
357
- console.log(id);
357
+ // console.log(id);
358
358
 
359
359
  removeListClass();
360
360
 
361
- $('.navigation a[href^="' + id + '"]').addClass('current');
361
+ $('.navigation li:has(a[href="' + id + '"])').addClass('current');
362
362
 
363
363
  exitLoop = true;
364
364
 

1

追記

2017/12/15 14:31

投稿

退会済みユーザー
test CHANGED
@@ -163,3 +163,269 @@
163
163
  </html>
164
164
 
165
165
  ```
166
+
167
+
168
+
169
+ ---
170
+
171
+
172
+
173
+ ## 要件が足りなかったようなので修正
174
+
175
+
176
+
177
+ ```html
178
+
179
+ <!doctype html>
180
+
181
+ <!--[if lt IE 7]> <html class="ie6 oldie"> <![endif]-->
182
+
183
+ <!--[if IE 7]> <html class="ie7 oldie"> <![endif]-->
184
+
185
+ <!--[if IE 8]> <html class="ie8 oldie"> <![endif]-->
186
+
187
+ <!--[if gt IE 8]><!-->
188
+
189
+ <html class="">
190
+
191
+ <!--<![endif]-->
192
+
193
+ <head>
194
+
195
+ <meta charset="utf-8">
196
+
197
+ <meta name="viewport" content="width=device-width, initial-scale=1">
198
+
199
+ <title>Untitled Document</title>
200
+
201
+ <style type="text/css">
202
+
203
+ .current a {
204
+
205
+ color:red;
206
+
207
+ }
208
+
209
+ section {
210
+
211
+ height: 300px;
212
+
213
+ }
214
+
215
+ </style>
216
+
217
+ </head>
218
+
219
+ <body>
220
+
221
+ <header>
222
+
223
+ <nav>
224
+
225
+ <ul class="navigation">
226
+
227
+ <li class="current"><a href="#top">1番</a></li>
228
+
229
+ <li><a href="/hoge/">2番</a></li>
230
+
231
+ <li><a href="#sec03">3番</a></li>
232
+
233
+ <li><a href="#sec04">4番</a></li>
234
+
235
+ <li><a href="#sec05">5番</a></li>
236
+
237
+ <li><a href="#sec06">6番</a></li>
238
+
239
+ </ul>
240
+
241
+ </nav>
242
+
243
+ </header>
244
+
245
+ <main>
246
+
247
+ <article>
248
+
249
+ <!-- bodyのidはこちらに移動 -->
250
+
251
+ <section class="col1" id="top">セクション01</section>
252
+
253
+ <section class="col2">セクション02</section>
254
+
255
+ <section class="col3" id="sec03">3番</section>
256
+
257
+ <section class="col4" id="sec04">4番</section>
258
+
259
+ <section class="col5" id="sec05">5番</section>
260
+
261
+ <section class="col1">セクション</section>
262
+
263
+ <section class="col2">セクション</section>
264
+
265
+ <section class="col3">セクション</section>
266
+
267
+ <section class="col4" id="sec06">6番</section>
268
+
269
+ <section class="col5">セクション</section>
270
+
271
+ </article>
272
+
273
+ </main>
274
+
275
+ <script
276
+
277
+ src="//code.jquery.com/jquery-1.12.4.js"></script>
278
+
279
+ <script>
280
+
281
+ $(function () {
282
+
283
+
284
+
285
+ function removeListClass() {
286
+
287
+ $('.navigation li.current').removeClass('current');
288
+
289
+ }
290
+
291
+
292
+
293
+ $('.navigation a[href^="#"]').on('click', function () {
294
+
295
+ // 一旦currentを全部消す
296
+
297
+ removeListClass();
298
+
299
+
300
+
301
+ // クリックしたa要素の親liにcurrentをつける
302
+
303
+ var li = $(this).parent('li');
304
+
305
+ li.addClass('current');
306
+
307
+
308
+
309
+ // スムーズスクロール
310
+
311
+ var href_val = $(this).attr('href');
312
+
313
+ var t = $(href_val).offset().top;
314
+
315
+ $('html,body').animate({
316
+
317
+ scrollTop: t
318
+
319
+ }, 800, 'swing');
320
+
321
+ return false;
322
+
323
+ });
324
+
325
+
326
+
327
+ function getElementByWindowTop(scrollTop) {
328
+
329
+ var anchors = $('.navigation a[href^="#"]');
330
+
331
+ var data = {};
332
+
333
+ anchors.each(function (i) {
334
+
335
+ var id = $(this).attr('href');
336
+
337
+ var t = $(id).offset().top;
338
+
339
+ data[id] = t;
340
+
341
+ });
342
+
343
+
344
+
345
+ var exitLoop = false;
346
+
347
+ $.each(data, function (id) {
348
+
349
+ if (exitLoop) {
350
+
351
+ return;
352
+
353
+ }
354
+
355
+ if (data[id] >= scrollTop) {
356
+
357
+ console.log(id);
358
+
359
+ removeListClass();
360
+
361
+ $('.navigation a[href^="' + id + '"]').addClass('current');
362
+
363
+ exitLoop = true;
364
+
365
+ }
366
+
367
+ });
368
+
369
+ }
370
+
371
+
372
+
373
+ // 変数宣言
374
+
375
+ var timerId;
376
+
377
+
378
+
379
+ // スクロール停止イベントのバインド
380
+
381
+ $(window).bind("scrollFinish", function (event, scrollTop) {
382
+
383
+ // メニュー移動処理呼び出し(後に掲載するサンプルを参照して下さい)
384
+
385
+ getElementByWindowTop(scrollTop);
386
+
387
+ });
388
+
389
+
390
+
391
+ // スクロールイベントのバインド
392
+
393
+ $(window).bind("scroll", function () {
394
+
395
+ var scrollTop = $(document).scrollTop();
396
+
397
+ if (timerId) {
398
+
399
+ clearTimeout(timerId);
400
+
401
+ }
402
+
403
+ // 1秒間スクロールしない場合はscrollFinishイベントを呼び出し
404
+
405
+ timerId = setTimeout(function () {
406
+
407
+ timerId = null;
408
+
409
+ $(window).trigger("scrollFinish", [scrollTop]);
410
+
411
+ }, 100);
412
+
413
+ });
414
+
415
+
416
+
417
+ $(window).unload(function () {
418
+
419
+ $(window).unbind("scroll scrollFinish");
420
+
421
+ });
422
+
423
+ });
424
+
425
+ </script>
426
+
427
+ </body>
428
+
429
+ </html>
430
+
431
+ ```