What's new

Welcome to the forum 👋, Visitor

To access the forum content and all our services, you must register or log in to the forum. Becoming a member of the forum is completely free.

How to make sidebar (with toggle option) fixed (without using actual "fixed" property) with javascript?

Markob15

New member
Joined
Sep 18, 2021
Messages
1
Reaction score
0
HTML Coins
0
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.

  1. 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.
Here is the following js code:


$(function() {

var $sidebar = $("#menu"),
$window = $(window),
offset = $sidebar.offset(),
topPadding = 60;

$window.scroll(function() {
if ($window.scrollTop() > offset.top) {
$sidebar.stop().animate({
marginTop: $window.scrollTop() - offset.top + topPadding
});
} else {
$sidebar.stop().animate({
marginTop: 0
});
}
});

});




Here is the html:

<!-- Sidebar -->
<div class="bg-light border-right" id="sidebar-wrapper" style="background-image: url('geogebra6.png'); scroll-margin: 120px;"> <!--Dodao side baru scroll marginu -->
<div class="sidebar-heading">Sadržaj</div>
<div class="list-group list-group-flush" style="background-image: url('geogebra1.png');" id="menu">
<a href="#" class="list-group-item list-group-item-action bg-light" onclick="callmymethod2(event)"><b>1. Uvod</b></a>
<a href="#" class="list-group-item list-group-item-action bg-light" onclick="callmymethod1(event)"><b>2. Realni nizovi </b></a>
<!-- class="dropdown-toggle" style="color: black; "-->
<a href="#homeSubmenu" data-toggle="collapse" aria-expanded="false" class="list-group-item list-group-item-action bg-light" onclick="callmymethod3(event)"><b>3. Funkcionalni nizovi</b></a>
<ul class="collapse list-unstyled" id="homeSubmenu" >
<li>
<a href="#" class="list-group-item list-group-item-action bg-light" onclick="callmymethod4(event)"><b>3.1. Obična konvergencija</b></a>
</li>
<li>
<a href="#" class="list-group-item list-group-item-action bg-light" onclick="callmymethod5(event)"><b>3.2. Ravnomerna konvergencija</b></a>
</li>

</ul>

</div>
</div>

Of course, using position:fixed does not get the job done because it breaks the flow and mu side bar has the toggle option.

  1. 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.
Here is the code in question:


<!-- Menu Toggle Script -->

// $("#wrapper").toggleClass("toggled");
$("#menu-toggle").click(function(e) {
e.preventDefault();
$("#wrapper").toggleClass("toggled");
});




Here is the actual link of the site:

http://alas.matf.bg.ac.rs/~ml05184/

Here is the original post I've posted on stackoverflow:
 

Theme customization system

You can customize some areas of the forum theme from this menu.

  • Wide/Narrow view

    You can control a structure that you can use to use your theme wide or narrow.

    Grid view forum list

    You can control the layout of the forum list in a grid or ordinary listing style structure.

    Picture grid mode

    You can control the structure where you can open/close images in the grid forum list.

    Close sidebar

    You can get rid of the crowded view in the forum by closing the sidebar.

    Fixed sidebar

    You can make it more useful and easier to access by pinning the sidebar.

    Close radius

    You can use the radius at the corners of the blocks according to your taste by closing/opening it.

  • Choose the color combination that reflects your taste
    Background images
    Color gradient backgrounds
Back