回答編集履歴
4
調整版
test
CHANGED
@@ -193,3 +193,225 @@
|
|
193
193
|
|
194
194
|
|
195
195
|
```
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
# 調整版
|
200
|
+
|
201
|
+
以下ベタに貼ります
|
202
|
+
|
203
|
+
挙動確認下さい
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
```HTML
|
208
|
+
|
209
|
+
<html>
|
210
|
+
|
211
|
+
<head>
|
212
|
+
|
213
|
+
<style>
|
214
|
+
|
215
|
+
#menu-wrap {
|
216
|
+
|
217
|
+
position: fixed;
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
#menu{
|
222
|
+
|
223
|
+
text-align: left;
|
224
|
+
|
225
|
+
display: flex;
|
226
|
+
|
227
|
+
margin: 0px;
|
228
|
+
|
229
|
+
padding-left: 20px;
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
#menu li {
|
234
|
+
|
235
|
+
display:inline;
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
#menu li:last-child a{
|
240
|
+
|
241
|
+
border-right:1px solid #000000;
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
#menu a:hover{
|
246
|
+
|
247
|
+
background-Color:yellow;
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
#menu a{
|
252
|
+
|
253
|
+
width: 80px;
|
254
|
+
|
255
|
+
border:1px solid #000000;
|
256
|
+
|
257
|
+
border-right:0px;
|
258
|
+
|
259
|
+
padding-left: 5px;
|
260
|
+
|
261
|
+
padding-right: 5px;
|
262
|
+
|
263
|
+
display: inline-block;
|
264
|
+
|
265
|
+
text-decoration:none;
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
.contents {
|
270
|
+
|
271
|
+
height: 500px;
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
.active {
|
276
|
+
|
277
|
+
background-color: red;
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
</style>
|
282
|
+
|
283
|
+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
|
284
|
+
|
285
|
+
<script>
|
286
|
+
|
287
|
+
$(function() {
|
288
|
+
|
289
|
+
var list=[];
|
290
|
+
|
291
|
+
['#content_first',
|
292
|
+
|
293
|
+
'#content_second',
|
294
|
+
|
295
|
+
'#content_third',
|
296
|
+
|
297
|
+
'#content_fourth',
|
298
|
+
|
299
|
+
'#content_etc'
|
300
|
+
|
301
|
+
].map(function(x){
|
302
|
+
|
303
|
+
list.push({
|
304
|
+
|
305
|
+
"min": $(x).offset().top,
|
306
|
+
|
307
|
+
"max": $(x).next().length>0?$(x).next().offset().top-1:($(x).offset().top+$(x).height()),
|
308
|
+
|
309
|
+
"id": x,
|
310
|
+
|
311
|
+
})});
|
312
|
+
|
313
|
+
var pre_pos = 0;
|
314
|
+
|
315
|
+
$(function() {
|
316
|
+
|
317
|
+
$('#menu a[href="' + location.hash + '"]').addClass('active');
|
318
|
+
|
319
|
+
$('#menu a').on('click', function() {
|
320
|
+
|
321
|
+
$('#menu a').removeClass('active');
|
322
|
+
|
323
|
+
$(this).addClass('active');
|
324
|
+
|
325
|
+
});
|
326
|
+
|
327
|
+
});
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
$(document).on('scroll', function() {
|
332
|
+
|
333
|
+
var this_pos = $(window).scrollTop();
|
334
|
+
|
335
|
+
var pre_id = get_id(pre_pos);
|
336
|
+
|
337
|
+
var this_id = get_id(this_pos);
|
338
|
+
|
339
|
+
if (pre_id !== this_id) {
|
340
|
+
|
341
|
+
if(this_id!==null){
|
342
|
+
|
343
|
+
location.hash=this_id.substr(1,this_id.length);
|
344
|
+
|
345
|
+
$(window).scrollTop(this_pos);
|
346
|
+
|
347
|
+
}else{
|
348
|
+
|
349
|
+
location.hash="";
|
350
|
+
|
351
|
+
};
|
352
|
+
|
353
|
+
$('#menu a').removeClass("active");
|
354
|
+
|
355
|
+
$('[href="' + this_id + '"]').addClass("active");
|
356
|
+
|
357
|
+
}
|
358
|
+
|
359
|
+
pre_pos = this_pos;
|
360
|
+
|
361
|
+
});
|
362
|
+
|
363
|
+
function get_id(pos) {
|
364
|
+
|
365
|
+
var arr = list.filter(function(x) {
|
366
|
+
|
367
|
+
return x.min <= pos && x.max >= pos;
|
368
|
+
|
369
|
+
});
|
370
|
+
|
371
|
+
if (typeof arr[0] == "undefined") return null;
|
372
|
+
|
373
|
+
return arr[0].id;
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
});
|
378
|
+
|
379
|
+
</script>
|
380
|
+
|
381
|
+
</head>
|
382
|
+
|
383
|
+
<body>
|
384
|
+
|
385
|
+
<nav id="menu-wrap">
|
386
|
+
|
387
|
+
<ul id="menu">
|
388
|
+
|
389
|
+
<li><a href="#content_first">Contents1</a></li>
|
390
|
+
|
391
|
+
<li><a href="#content_second">Contents2</a></li>
|
392
|
+
|
393
|
+
<li><a href="#content_third">Contents3</a></li>
|
394
|
+
|
395
|
+
<li><a href="#content_fourth">Contents4</a></li>
|
396
|
+
|
397
|
+
<li><a href="#content_etc">Contents5</a></li>
|
398
|
+
|
399
|
+
</ul>
|
400
|
+
|
401
|
+
</nav>
|
402
|
+
|
403
|
+
<div class="contents" id="content_first">1</div>
|
404
|
+
|
405
|
+
<div class="contents" id="content_second">2</div>
|
406
|
+
|
407
|
+
<div class="contents" id="content_third">3</div>
|
408
|
+
|
409
|
+
<div class="contents" id="content_fourth">4</div>
|
410
|
+
|
411
|
+
<div class="contents" id="content_etc">5</div>
|
412
|
+
|
413
|
+
</body>
|
414
|
+
|
415
|
+
</html>
|
416
|
+
|
417
|
+
```
|
3
修正
test
CHANGED
@@ -60,23 +60,35 @@
|
|
60
60
|
|
61
61
|
var pre_pos=0;
|
62
62
|
|
63
|
-
$(do
|
63
|
+
var this_pos = $(window).scrollTop();
|
64
64
|
|
65
|
-
var t
|
65
|
+
var pre_id = get_id(pre_pos);
|
66
66
|
|
67
|
-
var
|
67
|
+
var this_id = get_id(this_pos);
|
68
68
|
|
69
|
-
|
69
|
+
if (pre_id !== this_id) {
|
70
70
|
|
71
|
+
/* 以下hashを変更するよう修正*/
|
72
|
+
|
71
|
-
if(
|
73
|
+
if(this_id!==null){
|
74
|
+
|
75
|
+
location.hash=this_id.substr(1,this_id.length);
|
76
|
+
|
77
|
+
$(window).scrollTop(this_pos);
|
78
|
+
|
79
|
+
}else{
|
80
|
+
|
81
|
+
location.hash="";
|
82
|
+
|
83
|
+
};
|
72
84
|
|
73
85
|
$('#menu a').removeClass("active");
|
74
86
|
|
75
|
-
$('[href="'+this_id+'"]').addClass("active");
|
87
|
+
$('[href="' + this_id + '"]').addClass("active");
|
76
88
|
|
77
89
|
}
|
78
90
|
|
79
|
-
pre_pos=this_pos;
|
91
|
+
pre_pos = this_pos;
|
80
92
|
|
81
93
|
});
|
82
94
|
|
2
追記
test
CHANGED
@@ -18,7 +18,17 @@
|
|
18
18
|
|
19
19
|
load時のチェックを1度だけして、あとはクリックしたところがactiveになればいいのでは?
|
20
20
|
|
21
|
+
```CSS
|
21
22
|
|
23
|
+
#menu-wrap{position:fixed;}
|
24
|
+
|
25
|
+
.contents{height:500px;}
|
26
|
+
|
27
|
+
.active{background-color:red;}
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
```
|
22
32
|
|
23
33
|
```javascript
|
24
34
|
|
@@ -37,3 +47,137 @@
|
|
37
47
|
});
|
38
48
|
|
39
49
|
```
|
50
|
+
|
51
|
+
# スクロールチェック
|
52
|
+
|
53
|
+
この際なのでスクロールをチェックしてみますか?
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
```javascript
|
58
|
+
|
59
|
+
$(function(){
|
60
|
+
|
61
|
+
var pre_pos=0;
|
62
|
+
|
63
|
+
$(document).on('scroll',function(){
|
64
|
+
|
65
|
+
var this_pos=$(window).scrollTop();
|
66
|
+
|
67
|
+
var pre_id=get_id(pre_pos);
|
68
|
+
|
69
|
+
var this_id=get_id(this_pos);
|
70
|
+
|
71
|
+
if(pre_id!==this_id){
|
72
|
+
|
73
|
+
$('#menu a').removeClass("active");
|
74
|
+
|
75
|
+
$('[href="'+this_id+'"]').addClass("active");
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
pre_pos=this_pos;
|
80
|
+
|
81
|
+
});
|
82
|
+
|
83
|
+
function get_id(pos){
|
84
|
+
|
85
|
+
var list=[
|
86
|
+
|
87
|
+
{
|
88
|
+
|
89
|
+
"min":$('#content_first').offset().top,
|
90
|
+
|
91
|
+
"max":$('#content_first').next().offset().top-1,
|
92
|
+
|
93
|
+
"id":'#content_first',
|
94
|
+
|
95
|
+
},
|
96
|
+
|
97
|
+
{
|
98
|
+
|
99
|
+
"min":$('#content_second').offset().top,
|
100
|
+
|
101
|
+
"max":$('#content_second').next().offset().top-1,
|
102
|
+
|
103
|
+
"id":'#content_second',
|
104
|
+
|
105
|
+
},
|
106
|
+
|
107
|
+
{
|
108
|
+
|
109
|
+
"min":$('#content_third').offset().top,
|
110
|
+
|
111
|
+
"max":$('#content_third').next().offset().top-1,
|
112
|
+
|
113
|
+
"id":'#content_third',
|
114
|
+
|
115
|
+
},
|
116
|
+
|
117
|
+
{
|
118
|
+
|
119
|
+
"min":$('#content_fourth').offset().top,
|
120
|
+
|
121
|
+
"max":$('#content_fourth').next().offset().top-1,
|
122
|
+
|
123
|
+
"id":'#content_fourth',
|
124
|
+
|
125
|
+
},
|
126
|
+
|
127
|
+
{
|
128
|
+
|
129
|
+
"min":$('#content_etc').offset().top,
|
130
|
+
|
131
|
+
"max":$('#content_etc').offset().top+9999,
|
132
|
+
|
133
|
+
"id":'#content_etc',
|
134
|
+
|
135
|
+
},
|
136
|
+
|
137
|
+
];
|
138
|
+
|
139
|
+
var arr=list.filter(function(x){return x.min<=pos && x.max>=pos;});
|
140
|
+
|
141
|
+
if(typeof arr[0]=="undefined") return null;
|
142
|
+
|
143
|
+
return arr[0].id;
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
});
|
148
|
+
|
149
|
+
```
|
150
|
+
|
151
|
+
```HTML
|
152
|
+
|
153
|
+
<nav id="menu-wrap">
|
154
|
+
|
155
|
+
<ul id="menu">
|
156
|
+
|
157
|
+
<li><a href="#content_first">Contents1</a></li>
|
158
|
+
|
159
|
+
<li><a href="#content_second">Contents2</a></li>
|
160
|
+
|
161
|
+
<li><a href="#content_third">Contents3</a></li>
|
162
|
+
|
163
|
+
<li><a href="#content_fourth">Contents4</a></li>
|
164
|
+
|
165
|
+
<li><a href="#content_etc">Contents5</a></li>
|
166
|
+
|
167
|
+
</ul>
|
168
|
+
|
169
|
+
</nav>
|
170
|
+
|
171
|
+
<div class="contents" id="content_first">1</div>
|
172
|
+
|
173
|
+
<div class="contents" id="content_second">2</div>
|
174
|
+
|
175
|
+
<div class="contents" id="content_third">3</div>
|
176
|
+
|
177
|
+
<div class="contents" id="content_fourth">4</div>
|
178
|
+
|
179
|
+
<div class="contents" id="content_etc">5</div>
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
```
|
1
追記
test
CHANGED
@@ -13,3 +13,27 @@
|
|
13
13
|
load時だけでよいのですか?
|
14
14
|
|
15
15
|
クリック時に変えるのであればわざわざhrefをしらべる必要もないので・・・
|
16
|
+
|
17
|
+
# 追記
|
18
|
+
|
19
|
+
load時のチェックを1度だけして、あとはクリックしたところがactiveになればいいのでは?
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
```javascript
|
24
|
+
|
25
|
+
$(function(){
|
26
|
+
|
27
|
+
$('#menu a[href="'+location.hash+'"]').addClass('active');
|
28
|
+
|
29
|
+
$('#menu a').on('click',function(){
|
30
|
+
|
31
|
+
$('#menu a').removeClass('active');
|
32
|
+
|
33
|
+
$(this).addClass('active');
|
34
|
+
|
35
|
+
});
|
36
|
+
|
37
|
+
});
|
38
|
+
|
39
|
+
```
|