Issue with scrollTop functionality not functioning properly in sidebar navigation / division

I am currently working on a web page that features a fixed scrollable sidebar menu styled with Bootstrap on the left side, while the main content is displayed on the right side.

<div class="wrapper">
   <nav id="sidebar">
      <ul id="mainMenu">
         <li id="menuItem1">item 1</li>
         <li id="menuItem2">item 2</li>
         <li id="menuItem3">item 3</li>
         .
         .
         .
      </ul>
   </nav>
   <div id="content">
      Main contents goes here on the right side...
   </div>
</div>

Below is the CSS for the sidebar menu:

#sidebar {
  width: 250px;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  transition: all 0.3s;
}
#content {
  width: calc(100% - 250px);
  min-height: 100vh;
  position: absolute;
  top: 0;
  right: 0;
}

I have attempted to use JavaScript to automatically scroll the sidebar menu to a specific item so that it appears in view. Here are the codes I have tried:

document.getElementById('sidebar').scrollTop = 400;
document.getElementById('menuItem20').scrollIntoView();
location.href = "#menuItem20";

Unfortunately, none of the above methods seem to be working as intended. While I can successfully implement scrolling for the main content section using JavaScript, I am facing difficulties achieving the same for the sidebar menu. It's worth noting that the sidebar menu does scroll properly when using the mouse wheel. Any assistance or guidance on this matter would be greatly appreciated.

Answer №1

I appreciate your efforts to assist me. After some investigation, I was able to identify the issue at hand. The sidebar menu template I am using, sourced from the internet, includes a plugin named "mCustomScrollbar." This particular plugin is causing interference with the standard javascript scrolltop functionality. Luckily, the plugin offers its own scroll function that I have successfully implemented:

var yOffset = $("menuItem20").position().top;

$("#sidebar").mCustomScrollbar({
   theme:"minimal"
});

$("#sidebar").mCustomScrollbar("scrollTo", yOffset);

The intention behind incorporating this snippet of code is to automatically scroll the active menu item into view whenever the webpage reloads. It is worth mentioning that one can utilize window.innerHeight to calculate the optimal yOffset for centering the active menu item; however, this topic falls outside the context of my current question.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to display a chosen item in a text input field?

https://i.stack.imgur.com/Qe5Ds.png Looking to create a similar design, but lacking the necessary expertise. What steps should I take or what is the specific term for this style? Seeking assistance in implementing this using jQuery, PHP, or JavaScript. A ...

Issue with panel not responding to clicks after switching pages

Currently, I am developing a small web application using jQuery Mobile and PHP. One issue that I have encountered is related to an onclick event within a menu in a panel. The problem arises when trying to trigger the event after changing pages; it does not ...

Configuration of injected services in IONIC 2

I am curious about how the services from injected work in IONIC 2. Specifically, my question is regarding the number of instances that exist when one service is used in two or more controllers. Previously, I asked a colleague who mentioned that IONIC 2 op ...

Maintain the vertexShader's movement even when the mesh is rotating

Currently, I am adjusting the vertices of my cube along the y-axis. Although this method works well when simply moving the cube, issues arise when I begin rotating it. The movements continue to follow the global y-axis even during rotation. Is there a way ...

Retrieving data from router in Angular

I have been working on a web application using Angular, and I have implemented a fixed header in the index.html file. The header appears on every page, with content injected through a ui-view. However, I now want to display this header only after the user ...

"Troubleshooting issue with setAttribute not functioning properly when used with onClick in

A new variable named "navBtn" is created by utilizing document.createElement("span") to generate a span element. However, for unknown reasons, applying setAttribute to this span is not functioning as expected. The navBtn variable resides within its own fun ...

Is there a way to create a marker that changes color when hovering over the text directly below it?

Hi everyone, I hope you're all doing well. Currently, if you hover over the marker, it changes color. However, I want this effect to apply when hovering over the text below as well. Is there a way to create a connection between these two div elements? ...

Implementing a Vue feature where an object is utilized as the v-model within a select

I am currently working with a select tag that displays an array of objects as its options. Each option only shows the name property of the object. The v-model on the select tag is initially set to one of the object's name properties. My aim is to use ...

Ways to detect the scroll event on a scrollable container?

https://i.sstatic.net/Vx3M7.jpg I am looking to implement a scroll event listener on a specific element, where an arrow icon will appear when the user scrolls up in the message box. Similar to the functionality seen in the Youtube live chat image above. ...

Implementing a Vue v-for loop based on a condition with the help of

As I work on creating a form, I've come across a challenging issue for which I have yet to find a solution. The problem involves a Vuex data on Vehicles Make and Model of the vehicle. Essentially, when a make is selected, I need the form to dynamicall ...

Having trouble configuring Travis; crashes just before installation begins

Today I attempted to set up Travis on my GitHub project but encountered a few roadblocks (refer to the screenshot). I experimented with different configurations in .travis.yml, such as the one below: language: node_js node_js: - "8.11.2" sudo: false b ...

Using the SmartSheets API to Iterate Over a List to Determine the Variable of a Function

I have a group of numerical values listed below that are stored in a variable. [700000000084, 100000000051652, 8800000000000072, 280000000000004, 680000000000008, 880000000000006] My goal is to iterate through this list using the SmartSheets API to gathe ...

Retrieve the content of a web page using PHP and AJAX every 3 seconds until a change is detected

I have a situation where my web server (www.domain.com) is constantly pinging a subnet web server (test.domain.com) to retrieve information, but I want it to stop once the status changes. The goal is to check for updates every 3 seconds and display any new ...

Developing NodeJS applications locally using Docker

How can I effectively develop an app locally within a container with access to my laptop's file system for the node_modules? Currently, my Dockerfile includes the following settings: COPY package.json /app/ RUN npm install COPY . /app/ And my docke ...

Transforming an array of HashMaps into a JSON object within a servlet and showcasing it on a JSP page

Looking to extract all table rows and store them in an array of hashmaps, then convert them into a JSON object to be sent to a JSP page using Ajax and jQuery. Struggling with displaying the content on the JSP page. I've written the code below but need ...

Receiving a 401 error when making an Axios post request

Having trouble with a 401 error when making a POST request to an API? Don't worry, I've got some suggestions that might help. I'm able to successfully make GET requests to the same API with a 200 status, so it could be a syntax issue in the ...

Unable to communicate between react components

I am currently working on a project using React JS (jsfiddle). The issue I am facing is that the textbox does not update with the true/false value of the checkbox when it is checked. Can anyone provide insight into why this might be happening? var Custo ...

What is the best way for AngularJS ng-repeat to access the key of an item?

For more information, check out the documentation: https://code.angularjs.org/1.2.26/docs/api/ng/directive/ngRepeat The ngRepeat directive creates a template for each item in a collection. Each template has its own scope with the current item assigned t ...

How can I store the city name instead of the city id in the database when submitting data from a dependent dropdown select in Ajax PHP?

For the past few days, I have been unable to solve a problem. The issue is as follows: I have a web form with dependent dropdown select boxes where users can choose their city, zip code, and street. However, when the form is submitted, only the city_id ( ...

What is causing the chat-widget to display a null value for the style read property?

Could someone assist me with hiding the Widget-chat? I keep getting an error that the property of style is null. Any help would be greatly appreciated. Thank you in advance. document.getElementById("chat-widget").style.display='none'; ...