Last active
August 29, 2015 14:01
-
-
Save maiko-ampersand/03407c28936183145e6b to your computer and use it in GitHub Desktop.
見出し一覧をつくるためのスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | |
| <script type="text/javascript"> | |
| $.noConflict(); | |
| jQuery(document).ready(function(){ | |
| (function($){ | |
| // $('<ul></ul>') と同義だけども、左記の書き方をするとWPが勝手に閉じタグを入れるのでNG | |
| var $_ul = $(document.createElement("ul")); | |
| // 記事IDで選択スコープを絞ってね | |
| $("#post-2420 h2, #post-2420 h3").each(function(){ | |
| var $_h = $(this); | |
| // ↑と同じ | |
| var $_li = $(document.createElement("li")); | |
| var _text = $_h[0].tagName === "H2" ? "<b>"+$_h.text()+"</b>" : " "+$_h.text(); | |
| var $_a = ($("<a href='#'>"+_text+"</a>").on("click",function(e){ | |
| e.preventDefault(); | |
| $("html,body").animate({ | |
| scrollTop: $_h.offset().top - 50 // ヘッダ分を調整(WP非ログイン時のぶんです) | |
| }, | |
| "fast" | |
| ); | |
| })); | |
| $_ul.append($_li.append($_a)); | |
| // 終了条件 | |
| if($_h.text() === "E ~ F") { | |
| return false; | |
| } | |
| }); | |
| // アペンド対象、記事IDをサフィックスにするとかで重複防止してね | |
| // IDは振らないこと | |
| $(".nav_2420").append($_ul); | |
| })(jQuery); | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment