jimmynotjim / scrollnav

A dependency free JavaScript plugin for auto generating single page navigation
http://scrollnav.com
MIT License
461 stars 127 forks source link

Subsections in nested elements are not recognized #102

Open justlevine opened 5 years ago

justlevine commented 5 years ago

For some reason, only subsections in the same parent element are recognised by scrollnav. This is obviously not ideal behavior, as there are various reasons one might be nesting their subsections in a different element (e.g. lists).

Config:

scrollnav.init( content, {
    sections: 'h2',
    subSections: 'h3',
    insertLocation: 'prepend',
} );

Html

<section>
    <h2>Section Title</h2> <!--Will appear-->
    <ul>
        <li>
            <h3>Step 1</h3> <!-- Will NOT appear -->
            <p>Content goes here</p>
        </li>
    <ul>
</section>

Suggestions on how to fix?

jimmynotjim commented 5 years ago

I use a nextUntil function to search for the subsections that only looks at elems on the same level. I did this to try and speed up the dom query but I'll have to rethink it. Thanks for opening the bug.

makrohard commented 3 years ago

I did come across the same issue. I "solved" this behaviour, repeating the headings on the expected level and hide the duplicates using visibility:hidden (display:none shows heading in nav, but does not scroll to correct position). Works for me.