Hướng dẫn viết bài viết mới có mục lục

Ngày 14/04/2020 07:11 AM (GMT+7) 95 lượt xem

scrollNav is a light jQuery plugin that grabs your page's existing content, divides it up into logical sections and builds a customizable scrolling sidebar navigation. Scroll this page and watch the nav follow along with you.


scrollNav is a light jQuery plugin that grabs your page's existing content, divides it up into logical sections and builds a customizable scrolling sidebar navigation. Scroll this page and watch the nav follow along with you.


Simple to setup

A single file import and function in your footer and you're on your way.

Fully Customizable

All the styling is up to you and the few bits controled by the plugin are editable

Tiny Size

Damn right it's fast! At 1.5kb min'd and GZip'd, scrollNav is pretty unintrusive.



Start by importing the script to your page, the best location is in the footer, but no matter what, make sure it follows your jQuery file.

<script src="jquery.scrollNav.min.js"></script>

Build your page

Include a class or id hook on the element you want to apply the plugin to and include an <h2> for section headings you want to inlcude in the navigation.

<div class="main"> <article class="post__article"> <header class"post__header"> <h1 class="post__heading">This is the main heading for the article</h1> <p class="post__sub-heading">This is a sub-heading for the article</p> </header> <p>Yada yada yada...</p> <h2>This is a section heading</h2> <p>More yada yada...</p> <h2>Another section heading</h2> <p>More more yada...</p> </article> </div>


Now initialize the plugin with your hook for the article


The plugin scans the article, grabs all the <h2>s, adds them to the navigation list and inserts the list before the article. It’s that easy…well almost.


To keep the plugin simple there are no styles added to the navigation, that's all up to you. The nav structure looks like this and includes class names in the BEM Methodology style (for a good overview read MindBEMding - getting your head 'round BEM syntax):

<nav class="scroll-nav"> <div class="scroll-nav__wrapper"> <span class="scroll-nav__heading"> <ol class="scroll-nav__list"> <li class="scroll-nav__item"> <a class="scroll-nav__link">

An active class is attached to the nav item matching the section that is the highest within the view bounds. An in-view class is attached to all nav items whose section is within the view bounds. If you have short sections at the end of your page and dislike that the last nav itmes are never activated, you can use the in-view hook to style all sections with in the view.

There are loading hooks added to the body element (similar to how Typekit handles font loading) to allow for css transitions or any other changes in css you'd need. When the plug-in starts sn-loading is added to the body class and is replaced by sn-active when the plugin is successful or sn-failed if it fails.


There are a few customizable options in scrollNav using key : value pairs. Here are the defaults.

$('.post-article').scrollNav({ sections: 'h2', subSections: false, sectionElem: 'section', showHeadline: true, headlineText: 'Scroll To', showTopLink: true, topLinkText: 'Top', fixedMargin: 40, scrollOffset: 40, animated: true, speed: 500, insertTarget: this.selector, insertLocation: 'insertBefore', arrowKeys: false, onInit: null, onRender: null, onDestroy: null });


As mentioned, the script automatically searches for <h2>s within the target article. If your page structure differs, feel free to target another element, like an <h3> or <h4> or even a class, like .scroll-headline.


Set to false by default, the plugin supports nesting sub-sections within each section in the final nav. Available selectors are the same as Sections.

Section Wrapper Element

If your article already contains section tags, you'll want to change this to 'div'. Sub-sections aren't affected by this option.

Show Headline

Set this to false to remove the Headline Text entirely.

Headline Text

scrollNav’s default headline text is ‘Scroll To’, but feel free to change it to whatever works for you, like ‘Article Sections’ or ‘Page Navigation’

Show Top Link

Set this to false to remove the Top nav item entirely.

Top Link Text

scrollNav’s default return to the top link text is ‘Top’, but feel free to change it to whatever works for you.

Fixed Margin

This is the top dimension you set for the .scroll-nav.fixed class, which is applied as the user scrolls down the page and is removed as they scroll above the article. You definitely want to set this if you don’t use the default 40px, otherwise the nav will jump around as the user scrolls past the top of the article.

Scroll Offset

This option affects two things. First is the "active state" boundries within the viewport. The bounderies are within the area from the top and bottom of the viewport offset by this amount. Second is the destination when scrolling the page. This will place the heading of the section right at the top edge of the "active state" boundry.

Animated Scrolling

The plugin animates the page scroll when clicking on a nav link by default. Set this to false if you do not wish to animate the scroll.

Scrolling Speed

Change this to either increase or decrease the animated page scroll speed.

Insertion Target

If you need to insert the nav relative to an element other than the one scrollNav is initialized on, you can change it here.

Insertion Location

You can pass any of the following jQuery insertion methods to change where scrollNav is inserted in relation to the targeted container. insertBefore, prependTo, appendTo, or insertAfter

Arrow Key Navigation

Set to false by default, change this to `true` to allow up/down arrow keys to jump through each section.

Callback functions

There are three new callback functions that you can utilize to accomodate anything you need to run after specific scrollNav events. They are onInit, onRender, and onDestroy. Add them to your options just like any other and they should look like this:

$('.post__article').scrollNav({ onInit: function() { callback actions in here } });


The plugin will refuse to build and log an error message if it doesn’t find your desired container or any sections within the container. If the nav doesn’t show up on load, check your browser’s console.


There are a few known issues, including poor location updating when scrolling on touch devices. If you find any others please submit them to the issue tracker.


scrollNav is Copyright © 2012-2013 James Wilson, released under the MIT license. This means you can re-create, edit or share the plugin as long as you maintain the same open licensing.


Written by James Wilson (@jimmynotjim)

With lots of help from:

Eric Clemmons (@ericclemmons)

Jeff Byrnes (@jeffbyrnes)

Jeff Coburn (@coburnicus)

Jen Germann (@germanny)

Wizcover (@wizcover)

Meghdad Hadidi (@MeghdadHadidi)


The scrollNav logo is set in Alfa Slab One by JM Solé. Body text and headlines are set in Open Slab by Steve Matteson. Both are delivered using Google Web Fonts. Icons are Entypo by Daniel Buce and were packaged using Fontello.

This demo is hosted and powered by GitHub through GitHub Pages, built using Sublime Text 2, Codekit utilizing standard html5, jQuery and SCSS with the ResponsableCSS framework.

Bạn thấy bài viết có hữu ích không

Click vào ngôi sao để đánh giá

Đánh giá 5 / 5. Số đánh giá 4

Tin bài cùng chủ đề