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

回答編集履歴

3

調整

2017/11/15 03:35

投稿

yambejp
yambejp

スコア117877

answer CHANGED
@@ -182,6 +182,7 @@
182
182
  $.when($('html,body').animate({scrollTop:$($(this).attr("href")).offset().top }, 500, 'swing')).done(function(){
183
183
  $('#menu a').removeClass('active');
184
184
  me.addClass('active');
185
+ location.href=me.attr('href');
185
186
  });
186
187
  });
187
188
  });

2

追記

2017/11/15 03:35

投稿

yambejp
yambejp

スコア117877

answer CHANGED
@@ -113,4 +113,120 @@
113
113
  <div class="contents" id="content_etc">5</div>
114
114
  </body>
115
115
  </html>
116
+ ```
117
+
118
+ # クリックしたところを必ずアクティブにする
119
+
120
+ ```javascript
121
+ <html>
122
+ <head>
123
+ <style>
124
+ #menu-wrap {
125
+ position: fixed;
126
+ }
127
+ #menu{
128
+ text-align: left;
129
+ display: flex;
130
+ margin: 0px;
131
+ padding-left: 20px;
132
+ }
133
+ #menu li {
134
+ display:inline;
135
+ }
136
+ #menu li:last-child a{
137
+ border-right:1px solid #000000;
138
+ }
139
+ #menu a:hover{
140
+ background-Color:yellow;
141
+ }
142
+ #menu a{
143
+ width: 80px;
144
+ border:1px solid #000000;
145
+ border-right:0px;
146
+ padding-left: 5px;
147
+ padding-right: 5px;
148
+ display: inline-block;
149
+ text-decoration:none;
150
+ }
151
+ .contents {
152
+ height: 500px;
153
+ }
154
+ .active {
155
+ background-color: red;
156
+ }
157
+ #content_first,#content_third,#content_etc{background-Color:aqua;}
158
+ #content_second,#content_fourth{background-Color:lime;}
159
+
160
+ </style>
161
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
162
+ <script>
163
+ $(function() {
164
+ var list=[];
165
+ ['#content_first',
166
+ '#content_second',
167
+ '#content_third',
168
+ '#content_fourth',
169
+ '#content_etc'
170
+ ].map(function(x){
171
+ list.push({
172
+ "min": $(x).offset().top,
173
+ "max": $(x).next().length>0?$(x).next().offset().top-1:($(x).offset().top+$(x).height()),
174
+ "id": x,
175
+ })});
176
+ var pre_pos = 0;
177
+ $(function() {
178
+ $('#menu a[href="' + location.hash + '"]').addClass('active');
179
+ $('#menu a').on('click', function(e) {
180
+ e.preventDefault();
181
+ var me=$(this);
182
+ $.when($('html,body').animate({scrollTop:$($(this).attr("href")).offset().top }, 500, 'swing')).done(function(){
183
+ $('#menu a').removeClass('active');
184
+ me.addClass('active');
185
+ });
186
+ });
187
+ });
188
+
189
+ $(document).on('scroll', function() {
190
+ var this_pos = $(window).scrollTop();
191
+ var pre_id = get_id(pre_pos);
192
+ var this_id = get_id(this_pos);
193
+ if (pre_id !== this_id) {
194
+ if(this_id!==null){
195
+ location.hash=this_id.substr(1,this_id.length);
196
+ $(window).scrollTop(this_pos);
197
+ }else{
198
+ location.hash="";
199
+ };
200
+ $('#menu a').removeClass("active");
201
+ $('[href="' + this_id + '"]').addClass("active");
202
+ }
203
+ pre_pos = this_pos;
204
+ });
205
+ function get_id(pos) {
206
+ var arr = list.filter(function(x) {
207
+ return x.min <= pos && x.max >= pos;
208
+ });
209
+ if (typeof arr[0] == "undefined") return null;
210
+ return arr[0].id;
211
+ }
212
+ });
213
+ </script>
214
+ </head>
215
+ <body>
216
+ <nav id="menu-wrap">
217
+ <ul id="menu">
218
+ <li><a href="#content_first">Contents1</a></li>
219
+ <li><a href="#content_second">Contents2</a></li>
220
+ <li><a href="#content_third">Contents3</a></li>
221
+ <li><a href="#content_fourth">Contents4</a></li>
222
+ <li><a href="#content_etc">Contents5</a></li>
223
+ </ul>
224
+ </nav>
225
+ <div class="contents" id="content_first">1</div>
226
+ <div class="contents" id="content_second">2</div>
227
+ <div class="contents" id="content_third">3</div>
228
+ <div class="contents" id="content_fourth">4</div>
229
+ <div class="contents" id="content_etc">5</div>
230
+ </body>
231
+ </html>
116
232
  ```

1

追記

2017/11/15 03:31

投稿

yambejp
yambejp

スコア117877

answer CHANGED
@@ -1,1 +1,116 @@
1
- 先日回答した[メニューバーで現在のページをハイライトさせたい](https://teratail.com/questions/99590)に近いかもしれません
1
+ 先日回答した[メニューバーで現在のページをハイライトさせたい](https://teratail.com/questions/99590)に近いかもしれません
2
+
3
+ # 改良版
4
+
5
+ スクロール時のアニメーションがほしいとのことなので、一部改良版を上げておきます
6
+ (スクロールがみやすくなるよう背景色をつけてあります)
7
+ ```javascript
8
+ <html>
9
+ <head>
10
+ <style>
11
+ #menu-wrap {
12
+ position: fixed;
13
+ }
14
+ #menu{
15
+ text-align: left;
16
+ display: flex;
17
+ margin: 0px;
18
+ padding-left: 20px;
19
+ }
20
+ #menu li {
21
+ display:inline;
22
+ }
23
+ #menu li:last-child a{
24
+ border-right:1px solid #000000;
25
+ }
26
+ #menu a:hover{
27
+ background-Color:yellow;
28
+ }
29
+ #menu a{
30
+ width: 80px;
31
+ border:1px solid #000000;
32
+ border-right:0px;
33
+ padding-left: 5px;
34
+ padding-right: 5px;
35
+ display: inline-block;
36
+ text-decoration:none;
37
+ }
38
+ .contents {
39
+ height: 500px;
40
+ }
41
+ .active {
42
+ background-color: red;
43
+ }
44
+ #content_first,#content_third,#content_etc{background-Color:aqua;}
45
+ #content_second,#content_fourth{background-Color:lime;}
46
+
47
+ </style>
48
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
49
+ <script>
50
+ $(function() {
51
+ var list=[];
52
+ ['#content_first',
53
+ '#content_second',
54
+ '#content_third',
55
+ '#content_fourth',
56
+ '#content_etc'
57
+ ].map(function(x){
58
+ list.push({
59
+ "min": $(x).offset().top,
60
+ "max": $(x).next().length>0?$(x).next().offset().top-1:($(x).offset().top+$(x).height()),
61
+ "id": x,
62
+ })});
63
+ var pre_pos = 0;
64
+ $(function() {
65
+ $('#menu a[href="' + location.hash + '"]').addClass('active');
66
+ $('#menu a').on('click', function() {
67
+ $('#menu a').removeClass('active');
68
+ $(this).addClass('active');
69
+ $('html,body').animate({scrollTop:$($(this).attr("href")).offset().top }, 500, 'swing');
70
+ });
71
+ });
72
+
73
+ $(document).on('scroll', function() {
74
+ var this_pos = $(window).scrollTop();
75
+ var pre_id = get_id(pre_pos);
76
+ var this_id = get_id(this_pos);
77
+ if (pre_id !== this_id) {
78
+ if(this_id!==null){
79
+ location.hash=this_id.substr(1,this_id.length);
80
+ $(window).scrollTop(this_pos);
81
+ }else{
82
+ location.hash="";
83
+ };
84
+ $('#menu a').removeClass("active");
85
+ $('[href="' + this_id + '"]').addClass("active");
86
+ }
87
+ pre_pos = this_pos;
88
+ });
89
+ function get_id(pos) {
90
+ var arr = list.filter(function(x) {
91
+ return x.min <= pos && x.max >= pos;
92
+ });
93
+ if (typeof arr[0] == "undefined") return null;
94
+ return arr[0].id;
95
+ }
96
+ });
97
+ </script>
98
+ </head>
99
+ <body>
100
+ <nav id="menu-wrap">
101
+ <ul id="menu">
102
+ <li><a href="#content_first">Contents1</a></li>
103
+ <li><a href="#content_second">Contents2</a></li>
104
+ <li><a href="#content_third">Contents3</a></li>
105
+ <li><a href="#content_fourth">Contents4</a></li>
106
+ <li><a href="#content_etc">Contents5</a></li>
107
+ </ul>
108
+ </nav>
109
+ <div class="contents" id="content_first">1</div>
110
+ <div class="contents" id="content_second">2</div>
111
+ <div class="contents" id="content_third">3</div>
112
+ <div class="contents" id="content_fourth">4</div>
113
+ <div class="contents" id="content_etc">5</div>
114
+ </body>
115
+ </html>
116
+ ```