Tocify是一個jQuery插件,能夠動態的生成文章目錄,Tocify可以隨意的設置Twitter Bootstrap 或者 jQueryUI Themeroller支持的可選動畫和jQuery的顯示/隱藏效果,Tocify還支持平滑滾動,向前和向後按鈕支持,可以監聽瀏覽器的滾動顯示當前的目錄結構 ...
Tocify是一個jQuery插件,能夠動態的生成文章目錄,Tocify可以隨意的設置Twitter Bootstrap 或者 jQueryUI Themeroller支持的可選動畫和jQuery的顯示/隱藏效果,Tocify還支持平滑滾動,向前和向後按鈕支持,可以監聽瀏覽器的滾動顯示當前的目錄結構。
插件特點
-
主題支持 Twitter Bootstrap 或者 jQueryUI Themeroller 風格。
-
支持動態滾動和jQuery的點擊顯示/隱藏效果。
-
支持前進和後退按鈕點擊效果
-
支持平滑滾動動畫效果
-
支持監聽網頁滾動事件,動態顯示當前的目錄結構。
-
支持頁面擴展選項,確保一個頁面有足夠大的滾動表的所有內容項目
-
通過 jsHint 完美沒有錯誤提示
線上實例
使用方法
- <div id="toc"></div>
- $(function() {
- //Calls the tocify method on your HTML div.
- $("#toc").tocify();
- });
參數詳解
選項 | 說明 | 預設值 |
context | 任意可用的jQuery選擇器 | "body" |
selectors | 文章節點,可以關聯生成目錄 | "h1,h2,h3" |
showAndHide | 是否展示二級目錄結構 | true |
showEffect | 目錄展示效果:"none", "fadeIn", "show", or "slideDown" | "slideDown" |
showEffectSpeed | 目錄展示速度:"slow", "medium", "fast", 或數字(毫秒) | "medium" |
hideEffect | 目錄隱藏效果:"none", "fadeOut", "hide", "slideUp" | "none" |
hideEffectSpeed | 目錄隱藏速度:"slow", "medium", "fast", 或數字(毫秒) | "medium" |
smoothScroll | 當點擊目錄節點菜單時,是否平滑滾動到文章對應的節點內容 | true |
smoothScrollSpeed | 平滑滾動速率,可以是數字(毫秒) or String: "slow", "medium", or "fast" | "medium" |
scrollTo | 當頁面滾動時,頁面頂端與目錄之間的間隔 | 0 |
showAndHideOnScroll | 當滾動頁面時,是否顯示和隱藏目錄子菜單 | true |
theme | 內容展示風格,可以是"bootstrap", "jqueryui", or "none" | "bootstrap" |
Name | Type | Default | Options |
---|---|---|---|
context | String | “body” | Any valid jQuery selector |
selectors | String | “h1,h2,h3” | Each comma separated selector must be a header element. |
showAndHide | Boolean | true | true or false |
showEffect | String | “slideDown” | “none”, “fadeIn”, “show”, or “slideDown”, or any of the other jQuery show effects |
showEffectSpeed | String or Number | “medium” | “slow”, “medium”, “fast”, or any numeric number (milliseconds) |
hideEffect | String | “none” | “none”, “fadeOut”, “hide”, “slideUp”, or any of the jQuery hide effects |
hideEffectSpeed | String or Number | “medium” | “slow”, “medium”, “fast”, or any numeric number (milliseconds) |
smoothScroll | Boolean | true | true or false |
smoothScrollSpeed | Number or String | “medium” | Accepts Number (milliseconds) or String: “slow”, “medium”, or “fast” |
scrollTo | Number or Function | 0 | Accepts any number (pixels) or Function |
showAndHideOnScroll | Boolean | true | true or false |
highlightOnScroll | Boolean | true | true or false |
highlightOffset | Number | 40 | Accepts any number (pixels) |
theme | String | “bootstrap” | “bootstrap”, “jqueryui”, or “none” |
extendPage | Boolean | true | true or false |
extendPageOffset | Number | 100 | Any number (pixels) |
history | Boolean | true | true or false |
hashGenerator | String or Function | “compact” | “compact”, “pretty”, function(text, element){}. Compact – #CompressesEverythingTogether. Pretty – #looks-like-a-nice-url-and-is-easily-readable. function(text, element){} – Your own hash generation function that accepts the text as an argument, and returns the hash value. |
highlightDefault | Boolean | true | true or false |
ignoreSelector | String | null | Any valid jQuery selector |
scrollHistory | Boolean | false | true or false |
設置參數
所有的選項可以設置插件時,稱為。某些選項也可以設置插件後使用setoption()或setoptions()方法稱為。
這裡是設置一個選項的插件時,首先調用的一個例子:
- //Executes your code when the DOM is ready. Acts the same as $(document).ready().
- $(function() {
- // Calls the selectBoxIt method on your HTML select box and updates the showEffect option
- var toc = $("#toc").tocify({ showEffect: "fadeIn" });
- });
這裡是設置一個選項的插件後首先調用使用SetOption方法的一個例子:
- //Executes your code when the DOM is ready. Acts the same as $(document).ready().
- $(function() {
- // Calls the selectBoxIt method on your HTML select box
- var toc = $("#toc").tocify().data("toc-tocify");
- // Sets the smoothScroll option to false
- toc.setOption("showEffect", "fadeIn");
- });
這裡是設置多個選項的插件後首先調用使用setOptions方法的一個例子:
- //Executes your code when the DOM is ready. Acts the same as $(document).ready().
- $(function() {
- // Calls the selectBoxIt method on your HTML select box and updates the showEffect option
- var toc = $("#toc").tocify().data("toc-tocify");
- // Sets the showEffect, scrollTo, and smoothScroll options
- toc.setOptions({ showEffect: "fadeIn", scrollTo: 50, smoothScroll: false });
- });
獲取參數
一個單一的選項可以通過使用option()方法檢索。目前所有的選項可以通過引用選項屬性檢索。
這裡是一個選項後檢索插件使用option()方法稱為例:
- //Executes your code when the DOM is ready. Acts the same as $(document).ready().
- $(function() {
- //Calls the tocify method on your HTML div
- var toc = $("#toc").tocify().data("toc-tocify");
- // Writes the showEffect option to the console
- console.log(toc.option("showEffect"));
- });
這裡是檢索所有當前選項的插件後利用期權特性稱為例:
- //Executes your code when the DOM is ready. Acts the same as $(document).ready().
- $(function() {
- //Calls the selectBoxIt method on your HTML select box
- var toc = $("#toc").tocify().data("toc-tocify");
- // Writes all of the current plugin options to the console
- console.log(toc.options);
- });