Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use bootstrap scrollspy with collapsible menu?

I have a MCQ Question website. In a exam there may be 100-200 questions from different subject. So I want to make the navigation easier. As the navigation is quite large so I want to open questions under open subject i.e.: when Subject 1 is active, only question link under Subject 1 is visible like Bootstrap website's right navigation. My working sample is given below.

I used Bootstrap scrollspy according to their site instruction and it worked fine. But when I try to make it collapsible (like Bootstrap website's right navigation) it does not work.

I'm using Bootstrap - 3.3.2

<body data-spy="scroll" data-target="#subjectNavbar" data-offset="0">
    <header id="header" class="container-fluid navbar-fixed-top">
        ...
    </header>
    <div class="container">
        <div class="row">
            <aside id="left-column" class="col-sm-3">
                <nav id="subjectNavbar" class="sub-nav">
                    <ul class="nav subject-ul">
                        <li>
                            <a href="#subject-1">Subject 1</a>
                            <ul class="list-inline question-ul">
                                <li id="question-no-1"><a href="#question-1">1</a></li> 
                                <li id="question-no-2"><a href="#question-2">2</a></li> 
                                <li id="question-no-3"><a href="#question-3">3</a></li> 
                                ...
                                <li id="question-no-20"><a href="#question-20">20</a></li> 
                            </ul>
                        </li>
                        <li>
                            <a href="#subject-2">Subject 2</a>
                            <ul class="list-inline question-ul">
                                <li id="question-no-21"><a href="#question-21">21</a></li> 
                                <li id="question-no-22"><a href="#question-22">22</a></li> 
                                <li id="question-no-23"><a href="#question-23">23</a></li> 
                                ...
                                <li id="question-no-40"><a href="#question-40">40</a></li> 
                            </ul>
                        </li>
                        ...
                        <li>
                            <a href="#subject-5">Subject 5</a>
                            <ul class="list-inline question-ul">
                                <li id="question-no-81"><a href="#question-81">81</a></li> 
                                <li id="question-no-82"><a href="#question-82">82</a></li> 
                                <li id="question-no-83"><a href="#question-83">83</a></li> 
                                ... 
                                <li id="question-no-100"><a href="#question-100">100</a></li>
                            </ul>
                        </li>
                    </ul>
                </nav>                
            </aside>
            <section class="col-sm-6">
                <div class="question-paper">
                    <div class="subject-group" id="subject-1">
                        <h2>Subject 1</h2>
                        <article id="question-1">
                            <header><em>1.</em> Question 1</header>
                            <ol>
                                <li>Option A</li>
                                <li>Option B</li>
                                <li>Option C</li>
                                <li>Option D</li>
                            </ol>
                        </article>
                        ...
                        <article id="question-20">
                            <header><em>20.</em> Question 20</header>
                            <ol>
                                <li>Option A</li>
                                <li>Option B</li>
                                <li>Option C</li>
                                <li>Option D</li>
                            </ol>
                        </article>
                    </div> 
                    ...
                    <div class="subject-group" id="subject-5">
                        <h2>Subject 5</h2>
                        <article id="question-81">
                            <header><em>81.</em> Question 81</header>
                            <ol>
                                <li>Option A</li>
                                <li>Option B</li>
                                <li>Option C</li>
                                <li>Option D</li>
                            </ol>
                        </article>
                        ...
                        <article id="question-100">
                            <header><em>100.</em> Question 100</header>
                            <ol>
                                <li>Option A</li>
                                <li>Option B</li>
                                <li>Option C</li>
                                <li>Option D</li>
                            </ol>
                        </article>
                    </div> 
                </div>
            </section>
            <aside id="right-column" class="col-sm-3">
                ...
            </aside>
        </div><!--/row-->
    </div><!--/.container-->
    <footer class="container">
        ...
    </footer>
</body>
like image 925
Al Amin Chayan Avatar asked Sep 29 '22 20:09

Al Amin Chayan


1 Answers

After a deep research over bootstrap websites native css and javascript file i found that they use css to make the navigation collapsible. they make the ul under an active visible and other child ul display none.

.subject-ul .question-ul{
    display:none;
}

.subject-ul li.active>.question-ul{
    display:visible;
}
like image 122
Al Amin Chayan Avatar answered Oct 03 '22 06:10

Al Amin Chayan