title: How to make sidebar fixed (without using actual "fixed" property) with javascript?
body: <p>I’m a beginner and I’m stuck. And I would really use some help. Have two questions regarding js.
  Namely, I have a sidebar on the left that has a toggle option. In order to solve the problem of keeping my sidebar in sight when scrolling, I added javascript code that constantly raises it to the top of the page. But that doesn't seem like the optimal solution.</p>
  <ol>
  <li>Is there a way to use javascript to set the menu to be fixed to the top of the page whenever the toggle is open.</li>
  </ol>
  <p>Here is the following js code:</p>
  <pre><code>          
              $(function() {
  
              var $sidebar   = $(&quot;#menu&quot;),
                $window    = $(window),
                offset     = $sidebar.offset(),
                topPadding = 60;   
  
                $window.scroll(function() {
                  if ($window.scrollTop() &gt; offset.top) {
                  $sidebar.stop().animate({
                  marginTop: $window.scrollTop() - offset.top + topPadding
                  });
                  } else {
                  $sidebar.stop().animate({
                      marginTop: 0
                  });
                  }
                });
  
                });
  
  
              
  </code></pre>
  <p>Here is the html:</p>
  <pre><code>      &lt;!-- Sidebar --&gt;
            &lt;div class=&quot;bg-light border-right&quot; id=&quot;sidebar-wrapper&quot; style=&quot;background-image: url('geogebra6.png'); scroll-margin: 120px;&quot;&gt; &lt;!--Dodao side baru scroll marginu --&gt;
              &lt;div class=&quot;sidebar-heading&quot;&gt;Sadržaj&lt;/div&gt;
              &lt;div class=&quot;list-group list-group-flush&quot; style=&quot;background-image: url('geogebra1.png');&quot; id=&quot;menu&quot;&gt;
                &lt;a href=&quot;#&quot; class=&quot;list-group-item list-group-item-action bg-light&quot; onclick=&quot;callmymethod2(event)&quot;&gt;&lt;b&gt;1. Uvod&lt;/b&gt;&lt;/a&gt;
                &lt;a href=&quot;#&quot; class=&quot;list-group-item list-group-item-action bg-light&quot; onclick=&quot;callmymethod1(event)&quot;&gt;&lt;b&gt;2. Realni nizovi &lt;/b&gt;&lt;/a&gt;
                &lt;!-- class=&quot;dropdown-toggle&quot; style=&quot;color: black; &quot;--&gt;
                &lt;a href=&quot;#homeSubmenu&quot; data-toggle=&quot;collapse&quot; aria-expanded=&quot;false&quot; class=&quot;list-group-item list-group-item-action bg-light&quot; onclick=&quot;callmymethod3(event)&quot;&gt;&lt;b&gt;3. Funkcionalni nizovi&lt;/b&gt;&lt;/a&gt;
                &lt;ul class=&quot;collapse list-unstyled&quot; id=&quot;homeSubmenu&quot; &gt;
                  &lt;li&gt;
                    &lt;a href=&quot;#&quot; class=&quot;list-group-item list-group-item-action bg-light&quot; onclick=&quot;callmymethod4(event)&quot;&gt;&lt;b&gt;3.1. Obična konvergencija&lt;/b&gt;&lt;/a&gt;
                  &lt;/li&gt;
                  &lt;li&gt;
                    &lt;a href=&quot;#&quot; class=&quot;list-group-item list-group-item-action bg-light&quot; onclick=&quot;callmymethod5(event)&quot;&gt;&lt;b&gt;3.2. Ravnomerna konvergencija&lt;/b&gt;&lt;/a&gt;
                  &lt;/li&gt;
  
                &lt;/ul&gt;
  
              &lt;/div&gt;
            &lt;/div&gt;
  </code></pre>
  <p>Of course, using position:fixed does not get the job done because it breaks the flow and mu side bar has the toggle option.</p>
  <ol start="2">
  <li>While we are here, is there any way of editing this line of code to make sidebar closed by default and then to open when clicked on the button? I've tried several different solutions but none of them seems optimal.</li>
  </ol>
  <p>Here is the code in question:</p>
  <pre><code>
  &lt;!-- Menu Toggle Script --&gt;
           
              // $(&quot;#wrapper&quot;).toggleClass(&quot;toggled&quot;);
              $(&quot;#menu-toggle&quot;).click(function(e) {
                e.preventDefault();
                $(&quot;#wrapper&quot;).toggleClass(&quot;toggled&quot;);
              });
              
  
  
  </code></pre>
  <p>Here is the actual link of the site:</p>
  <p><a href="http://alas.matf.bg.ac.rs/%7Eml05184/" rel="nofollow noreferrer">http://alas.matf.bg.ac.rs/~ml05184/</a></p>