ページ内移動で現在いる場所のメニューをハイライトさせたいのですが
今のjavascriptがフルパスでの設定方法になっています。
これをIDで指定したいです。
ご教授願いいたします。
html
1<nav id="menu-wrap"> 2 <ul id="menu"> 3 <li><a href="#content_first">Contents1</a></li> 4 <li><a href="#content_second">Contents2</a></li> 5 <li><a href="#content_third">Contents3</a></li> 6 <li><a href="#content_fourth">Contents4</a></li> 7 <li><a href="#content_etc">Contents5</a></li> 8 </ul> 9</nav> 10
javascript
1<script> 2 var url = window.location; 3 $('#menu a[href="'+url+'"]').addClass('active'); 4</script>
css
1.active{color:red;}
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
とりあえず
var url = window.hash
ですね。
なにをトリガーに色を変えるのでしょうか?
load時だけでよいのですか?
クリック時に変えるのであればわざわざhrefをしらべる必要もないので・・・
追記
load時のチェックを1度だけして、あとはクリックしたところがactiveになればいいのでは?
CSS
1#menu-wrap{position:fixed;} 2.contents{height:500px;} 3.active{background-color:red;} 4
javascript
1$(function(){ 2 $('#menu a[href="'+location.hash+'"]').addClass('active'); 3 $('#menu a').on('click',function(){ 4 $('#menu a').removeClass('active'); 5 $(this).addClass('active'); 6 }); 7});
スクロールチェック
この際なのでスクロールをチェックしてみますか?
javascript
1$(function(){ 2 var pre_pos=0; 3 var this_pos = $(window).scrollTop(); 4 var pre_id = get_id(pre_pos); 5 var this_id = get_id(this_pos); 6 if (pre_id !== this_id) { 7 /* 以下hashを変更するよう修正*/ 8 if(this_id!==null){ 9 location.hash=this_id.substr(1,this_id.length); 10 $(window).scrollTop(this_pos); 11 }else{ 12 location.hash=""; 13 }; 14 $('#menu a').removeClass("active"); 15 $('[href="' + this_id + '"]').addClass("active"); 16 } 17 pre_pos = this_pos; 18 }); 19 function get_id(pos){ 20 var list=[ 21 { 22 "min":$('#content_first').offset().top, 23 "max":$('#content_first').next().offset().top-1, 24 "id":'#content_first', 25 }, 26 { 27 "min":$('#content_second').offset().top, 28 "max":$('#content_second').next().offset().top-1, 29 "id":'#content_second', 30 }, 31 { 32 "min":$('#content_third').offset().top, 33 "max":$('#content_third').next().offset().top-1, 34 "id":'#content_third', 35 }, 36 { 37 "min":$('#content_fourth').offset().top, 38 "max":$('#content_fourth').next().offset().top-1, 39 "id":'#content_fourth', 40 }, 41 { 42 "min":$('#content_etc').offset().top, 43 "max":$('#content_etc').offset().top+9999, 44 "id":'#content_etc', 45 }, 46 ]; 47 var arr=list.filter(function(x){return x.min<=pos && x.max>=pos;}); 48 if(typeof arr[0]=="undefined") return null; 49 return arr[0].id; 50 } 51});
HTML
1<nav id="menu-wrap"> 2 <ul id="menu"> 3 <li><a href="#content_first">Contents1</a></li> 4 <li><a href="#content_second">Contents2</a></li> 5 <li><a href="#content_third">Contents3</a></li> 6 <li><a href="#content_fourth">Contents4</a></li> 7 <li><a href="#content_etc">Contents5</a></li> 8 </ul> 9</nav> 10<div class="contents" id="content_first">1</div> 11<div class="contents" id="content_second">2</div> 12<div class="contents" id="content_third">3</div> 13<div class="contents" id="content_fourth">4</div> 14<div class="contents" id="content_etc">5</div> 15
調整版
以下ベタに貼ります
挙動確認下さい
HTML
1<html> 2<head> 3<style> 4#menu-wrap { 5 position: fixed; 6} 7#menu{ 8text-align: left; 9 display: flex; 10margin: 0px; 11 padding-left: 20px; 12} 13#menu li { 14 display:inline; 15} 16#menu li:last-child a{ 17 border-right:1px solid #000000; 18} 19#menu a:hover{ 20 background-Color:yellow; 21} 22#menu a{ 23 width: 80px; 24 border:1px solid #000000; 25 border-right:0px; 26 padding-left: 5px; 27 padding-right: 5px; 28 display: inline-block; 29 text-decoration:none; 30} 31.contents { 32 height: 500px; 33} 34.active { 35 background-color: red; 36} 37</style> 38<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 39<script> 40$(function() { 41 var list=[]; 42 ['#content_first', 43 '#content_second', 44 '#content_third', 45 '#content_fourth', 46 '#content_etc' 47 ].map(function(x){ 48 list.push({ 49 "min": $(x).offset().top, 50 "max": $(x).next().length>0?$(x).next().offset().top-1:($(x).offset().top+$(x).height()), 51 "id": x, 52 })}); 53 var pre_pos = 0; 54 $(function() { 55 $('#menu a[href="' + location.hash + '"]').addClass('active'); 56 $('#menu a').on('click', function() { 57 $('#menu a').removeClass('active'); 58 $(this).addClass('active'); 59 }); 60 }); 61 62 $(document).on('scroll', function() { 63 var this_pos = $(window).scrollTop(); 64 var pre_id = get_id(pre_pos); 65 var this_id = get_id(this_pos); 66 if (pre_id !== this_id) { 67 if(this_id!==null){ 68 location.hash=this_id.substr(1,this_id.length); 69 $(window).scrollTop(this_pos); 70 }else{ 71 location.hash=""; 72 }; 73 $('#menu a').removeClass("active"); 74 $('[href="' + this_id + '"]').addClass("active"); 75 } 76 pre_pos = this_pos; 77 }); 78 function get_id(pos) { 79 var arr = list.filter(function(x) { 80 return x.min <= pos && x.max >= pos; 81 }); 82 if (typeof arr[0] == "undefined") return null; 83 return arr[0].id; 84 } 85}); 86</script> 87</head> 88<body> 89<nav id="menu-wrap"> 90 <ul id="menu"> 91 <li><a href="#content_first">Contents1</a></li> 92 <li><a href="#content_second">Contents2</a></li> 93 <li><a href="#content_third">Contents3</a></li> 94 <li><a href="#content_fourth">Contents4</a></li> 95 <li><a href="#content_etc">Contents5</a></li> 96 </ul> 97</nav> 98<div class="contents" id="content_first">1</div> 99<div class="contents" id="content_second">2</div> 100<div class="contents" id="content_third">3</div> 101<div class="contents" id="content_fourth">4</div> 102<div class="contents" id="content_etc">5</div> 103</body> 104</html>
投稿2017/11/08 01:09
編集2017/11/09 05:04総合スコア114843
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/08 01:58
2017/11/08 02:17
2017/11/08 02:23
2017/11/08 02:48
2017/11/08 19:39
2017/11/08 19:41
2017/11/09 01:41
2017/11/09 01:55
2017/11/09 02:17
2017/11/09 02:28
2017/11/09 03:47
2017/11/09 03:55
2017/11/09 04:29
2017/11/09 05:05
2017/11/09 06:42
2017/11/09 07:10