teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

4

調整版

2017/11/09 05:04

投稿

yambejp
yambejp

スコア117892

answer CHANGED
@@ -95,4 +95,115 @@
95
95
  <div class="contents" id="content_fourth">4</div>
96
96
  <div class="contents" id="content_etc">5</div>
97
97
 
98
+ ```
99
+
100
+ # 調整版
101
+ 以下ベタに貼ります
102
+ 挙動確認下さい
103
+
104
+ ```HTML
105
+ <html>
106
+ <head>
107
+ <style>
108
+ #menu-wrap {
109
+ position: fixed;
110
+ }
111
+ #menu{
112
+ text-align: left;
113
+ display: flex;
114
+ margin: 0px;
115
+ padding-left: 20px;
116
+ }
117
+ #menu li {
118
+ display:inline;
119
+ }
120
+ #menu li:last-child a{
121
+ border-right:1px solid #000000;
122
+ }
123
+ #menu a:hover{
124
+ background-Color:yellow;
125
+ }
126
+ #menu a{
127
+ width: 80px;
128
+ border:1px solid #000000;
129
+ border-right:0px;
130
+ padding-left: 5px;
131
+ padding-right: 5px;
132
+ display: inline-block;
133
+ text-decoration:none;
134
+ }
135
+ .contents {
136
+ height: 500px;
137
+ }
138
+ .active {
139
+ background-color: red;
140
+ }
141
+ </style>
142
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
143
+ <script>
144
+ $(function() {
145
+ var list=[];
146
+ ['#content_first',
147
+ '#content_second',
148
+ '#content_third',
149
+ '#content_fourth',
150
+ '#content_etc'
151
+ ].map(function(x){
152
+ list.push({
153
+ "min": $(x).offset().top,
154
+ "max": $(x).next().length>0?$(x).next().offset().top-1:($(x).offset().top+$(x).height()),
155
+ "id": x,
156
+ })});
157
+ var pre_pos = 0;
158
+ $(function() {
159
+ $('#menu a[href="' + location.hash + '"]').addClass('active');
160
+ $('#menu a').on('click', function() {
161
+ $('#menu a').removeClass('active');
162
+ $(this).addClass('active');
163
+ });
164
+ });
165
+
166
+ $(document).on('scroll', function() {
167
+ var this_pos = $(window).scrollTop();
168
+ var pre_id = get_id(pre_pos);
169
+ var this_id = get_id(this_pos);
170
+ if (pre_id !== this_id) {
171
+ if(this_id!==null){
172
+ location.hash=this_id.substr(1,this_id.length);
173
+ $(window).scrollTop(this_pos);
174
+ }else{
175
+ location.hash="";
176
+ };
177
+ $('#menu a').removeClass("active");
178
+ $('[href="' + this_id + '"]').addClass("active");
179
+ }
180
+ pre_pos = this_pos;
181
+ });
182
+ function get_id(pos) {
183
+ var arr = list.filter(function(x) {
184
+ return x.min <= pos && x.max >= pos;
185
+ });
186
+ if (typeof arr[0] == "undefined") return null;
187
+ return arr[0].id;
188
+ }
189
+ });
190
+ </script>
191
+ </head>
192
+ <body>
193
+ <nav id="menu-wrap">
194
+ <ul id="menu">
195
+ <li><a href="#content_first">Contents1</a></li>
196
+ <li><a href="#content_second">Contents2</a></li>
197
+ <li><a href="#content_third">Contents3</a></li>
198
+ <li><a href="#content_fourth">Contents4</a></li>
199
+ <li><a href="#content_etc">Contents5</a></li>
200
+ </ul>
201
+ </nav>
202
+ <div class="contents" id="content_first">1</div>
203
+ <div class="contents" id="content_second">2</div>
204
+ <div class="contents" id="content_third">3</div>
205
+ <div class="contents" id="content_fourth">4</div>
206
+ <div class="contents" id="content_etc">5</div>
207
+ </body>
208
+ </html>
98
209
  ```

3

修正

2017/11/09 05:03

投稿

yambejp
yambejp

スコア117892

answer CHANGED
@@ -29,15 +29,21 @@
29
29
  ```javascript
30
30
  $(function(){
31
31
  var pre_pos=0;
32
- $(document).on('scroll',function(){
33
- var this_pos=$(window).scrollTop();
32
+ var this_pos = $(window).scrollTop();
34
- var pre_id=get_id(pre_pos);
33
+ var pre_id = get_id(pre_pos);
35
- var this_id=get_id(this_pos);
34
+ var this_id = get_id(this_pos);
36
- if(pre_id!==this_id){
35
+ if (pre_id !== this_id) {
36
+ /* 以下hashを変更するよう修正*/
37
+ if(this_id!==null){
38
+ location.hash=this_id.substr(1,this_id.length);
39
+ $(window).scrollTop(this_pos);
40
+ }else{
41
+ location.hash="";
42
+ };
37
43
  $('#menu a').removeClass("active");
38
- $('[href="'+this_id+'"]').addClass("active");
44
+ $('[href="' + this_id + '"]').addClass("active");
39
45
  }
40
- pre_pos=this_pos;
46
+ pre_pos = this_pos;
41
47
  });
42
48
  function get_id(pos){
43
49
  var list=[

2

追記

2017/11/09 03:54

投稿

yambejp
yambejp

スコア117892

answer CHANGED
@@ -8,7 +8,12 @@
8
8
  クリック時に変えるのであればわざわざhrefをしらべる必要もないので・・・
9
9
  # 追記
10
10
  load時のチェックを1度だけして、あとはクリックしたところがactiveになればいいのでは?
11
+ ```CSS
12
+ #menu-wrap{position:fixed;}
13
+ .contents{height:500px;}
14
+ .active{background-color:red;}
11
15
 
16
+ ```
12
17
  ```javascript
13
18
  $(function(){
14
19
  $('#menu a[href="'+location.hash+'"]').addClass('active');
@@ -17,4 +22,71 @@
17
22
  $(this).addClass('active');
18
23
  });
19
24
  });
25
+ ```
26
+ # スクロールチェック
27
+ この際なのでスクロールをチェックしてみますか?
28
+
29
+ ```javascript
30
+ $(function(){
31
+ var pre_pos=0;
32
+ $(document).on('scroll',function(){
33
+ var this_pos=$(window).scrollTop();
34
+ var pre_id=get_id(pre_pos);
35
+ var this_id=get_id(this_pos);
36
+ if(pre_id!==this_id){
37
+ $('#menu a').removeClass("active");
38
+ $('[href="'+this_id+'"]').addClass("active");
39
+ }
40
+ pre_pos=this_pos;
41
+ });
42
+ function get_id(pos){
43
+ var list=[
44
+ {
45
+ "min":$('#content_first').offset().top,
46
+ "max":$('#content_first').next().offset().top-1,
47
+ "id":'#content_first',
48
+ },
49
+ {
50
+ "min":$('#content_second').offset().top,
51
+ "max":$('#content_second').next().offset().top-1,
52
+ "id":'#content_second',
53
+ },
54
+ {
55
+ "min":$('#content_third').offset().top,
56
+ "max":$('#content_third').next().offset().top-1,
57
+ "id":'#content_third',
58
+ },
59
+ {
60
+ "min":$('#content_fourth').offset().top,
61
+ "max":$('#content_fourth').next().offset().top-1,
62
+ "id":'#content_fourth',
63
+ },
64
+ {
65
+ "min":$('#content_etc').offset().top,
66
+ "max":$('#content_etc').offset().top+9999,
67
+ "id":'#content_etc',
68
+ },
69
+ ];
70
+ var arr=list.filter(function(x){return x.min<=pos && x.max>=pos;});
71
+ if(typeof arr[0]=="undefined") return null;
72
+ return arr[0].id;
73
+ }
74
+ });
75
+ ```
76
+ ```HTML
77
+ <nav id="menu-wrap">
78
+ <ul id="menu">
79
+ <li><a href="#content_first">Contents1</a></li>
80
+ <li><a href="#content_second">Contents2</a></li>
81
+ <li><a href="#content_third">Contents3</a></li>
82
+ <li><a href="#content_fourth">Contents4</a></li>
83
+ <li><a href="#content_etc">Contents5</a></li>
84
+ </ul>
85
+ </nav>
86
+ <div class="contents" id="content_first">1</div>
87
+ <div class="contents" id="content_second">2</div>
88
+ <div class="contents" id="content_third">3</div>
89
+ <div class="contents" id="content_fourth">4</div>
90
+ <div class="contents" id="content_etc">5</div>
91
+
20
92
  ```

1

追記

2017/11/09 01:40

投稿

yambejp
yambejp

スコア117892

answer CHANGED
@@ -5,4 +5,16 @@
5
5
  ですね。
6
6
  なにをトリガーに色を変えるのでしょうか?
7
7
  load時だけでよいのですか?
8
- クリック時に変えるのであればわざわざhrefをしらべる必要もないので・・・
8
+ クリック時に変えるのであればわざわざhrefをしらべる必要もないので・・・
9
+ # 追記
10
+ load時のチェックを1度だけして、あとはクリックしたところがactiveになればいいのでは?
11
+
12
+ ```javascript
13
+ $(function(){
14
+ $('#menu a[href="'+location.hash+'"]').addClass('active');
15
+ $('#menu a').on('click',function(){
16
+ $('#menu a').removeClass('active');
17
+ $(this).addClass('active');
18
+ });
19
+ });
20
+ ```