`What is the best way to adjust scroll position using jQuery?`

After encountering a similar issue with locking the vertical scroll while opening a mobile menu on a website I'm working on, I came across a potential solution HERE. However, despite trying to implement it, I haven't been successful. The main problem I faced was that the page would jump to the top whenever the menu was opened.

Here is the jQuery code snippet:

timber.openDrawerMenu = function () {
    var tempScrollTop = $(window).scrollTop();
    var $mobileMenu = $('.nav-bar'),
    $mobileMenuButton = $('#menu-opener'),
    $body = $('body');
    $mobileMenuButton.addClass('opened');
    $mobileMenu.addClass('opened');
    $body.addClass('opened-drawer');

  // Make drawer a11y accessible
  timber.cache.$navBar.attr('aria-hidden', 'false');

  // Set focus on drawer
  timber.trapFocus({
     $container: timber.cache.$navBar,
     namespace: 'drawer_focus'
  });

  // Escape key closes menu
  timber.cache.$html.on('keyup.drawerMenu', function(evt) {
    if (evt.keyCode == 27) {
      timber.closeDrawerMenu();
    }
  });
  console.log(tempScrollTop);
  $(window).scrollTop(tempScrollTop);
}

Despite adding a console.log() statement to check the scrollTop value, the script doesn't seem to be setting the scroll position as intended. I'm unable to pinpoint the exact reason for this issue. Any assistance in resolving this matter would be highly appreciated.

Answer №1

It appears that the issue is specific to mobile devices.

  1. Here's a suggestion I have:
    • Have you tried setting position: relative on the body tag?
  2. If that doesn't solve it, here's another workaround:
    • Attempt resetting the scroll location right before closing the menu.

I hope those solutions help. If not, feel free to leave a comment :)

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

Is your prop callback failing to return a value?

I am currently utilizing a Material UI Table component in my ReactJS project and I would like to update a state variable whenever a row is selected or deselected. The Table component has an onRowSelection prop that gets triggered each time a row is is sele ...

Is there a way to work around coursera's keyboard input verification using JavaScript?

Is it possible to bypass Coursera's keyboard input verification using JavaScript? For example: What methods or techniques could be used to accomplish this? ...

Text cannot be highlighted within DIV element

Check out this page and this page on my website. Text within the advert DIV tag cannot be selected, and some text in the Incinerator product page is unselectable in the topic-div above the iframe element containing a YouTube video. Here is the relevant HT ...

The use of special characters in jQuery for specific selectors

I'm currently attempting to implement a basic hover effect on a special character as a selector. The special symbol I am utilizing is the double-daggers ( ‡ ‡) character with ASCII code 8225, and I would like it to function as a toolt ...

Executing multiple AJAX calls at the same time in PHP

I have a puzzling question that I can't seem to easily solve the behavior of this situation There are two ajax call functions in my code: execCommand() : used for executing shell commands through PHP shell_exec('ping 127.0.0.1 > ../var/l ...

Ways to eliminate color from a link upon clicking a different one

I had a specific idea in mind - to create a simple menu bar where clicking on a particular link would display related content below. The chosen link should turn blue when selected. I managed to come up with the following code: <a href="#" class="link"& ...

Is there a way to create a color bar that is positioned halfway below the title using only CSS? I am looking to only color the lower half of a div - can this be achieved

Is there a way to create a color bar beneath the title using only HTML and CSS without relying on background images? I want the bar to be flush with the text and have some flexibility in terms of width to prevent wrapping. Alternatively, can you position ...

Is there a way to incorporate both max-width and min-width in a matchMedia query effectively?

I'm currently utilizing the matchMedia API in my JavaScript code to identify viewports, with the goal of reducing DOM manipulations. Instead of using display: none extensively, I am opting for a v-if directive from Vue to determine when elements are ...

I'm wondering why my typography components display correctly on my local host, but not on my aws server. Any insights on

I've encountered an issue with the typography component I'm using for my headings. When I publish the website, the headings do not render properly and the styles are not applied correctly. Strangely, everything looks fine during npm run dev, but ...

After the page has finished loading, I aim to have the modal window pop up after 10 seconds. Thank you to all!

$(document).ready(function() { var id = "#dialog"; //Calculate screen dimensions var maskHeight = $(document).height(); var maskWidth = $(window).width(); //Adjust mask to cover entire screen $('#mask').css({'wid ...

What is the way to assign distinct colors to points in three.js through dat.gui based on their distance from the origin?

Currently, I am developing a 3D Pi Estimator using three.js. You can view the source code at https://jsfiddle.net/fny94e7w/. let num_points = 5000; let in_sphere = 0; let out_sphere = 0; let geometry_point = new THREE.Sp ...

The jQuery Validation plugin ensures that a minimum of one image has been uploaded

My current setup includes a form that permits users to upload images. Each image uploaded by the user is shown in a div named imgHolder, located outside the form. In addition, once a user uploads 5 files, the corresponding file[] input from the form is rem ...

Trouble connecting: MongoDB Node.js client hangs during connection attempt

The node-mongodb-native for node.js seems to be causing a hang when using MongoClient.connect(...), while the mongodb-client (shell command line) works fine in the terminal. Any ideas on what might be causing this issue? var MongoClient = require('mo ...

Extracting data from a Wikipedia table using web scraping in JavaScript

I'm attempting to extract information from a specific website. <script> var convertToInt; var allData = []; $.ajax({ url: "http://en.wikipedia.org/wiki/Demographics_of_Europe", type: 'GET', cache: false, success: func ...

Utilizing CSS to Properly Position FontAwesome Icons

Within my h1, I have incorporated 2 FontAwesome quote icons. To adjust their positioning, I utilized CSS due to the lack of a simpler method. However, I am facing challenges with responsiveness on various screen sizes, leading me to resort to media queries ...

Utilize the HTTP.get function to serve files in the img src attribute

I am facing an issue where my file can only be accessed if I include an x-authentication token in the header of the "GET" http request. Unfortunately, using a cookie is not an option in this case. This means I cannot simply call for the file by its URL in ...

Transferring content files from a nuget directory to a specific destination folder

I am dealing with a nuget package that includes css files as content. My goal is to have these css files copied to a specific directory upon importing the package into a project, without requiring the project's structure to match exactly. Is there a ...

Extract the Top X elements from a multidimensional array

Consider an Array that consists of nested arrays: [ ["2000-01-01", "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1a9a8abe091b6bcb0b8bdffb2bebc">[email protected]</a>", 1, 9, 338], ["2000-01-01", "<a href="/ ...

AngularJS $compile function is failing to render a custom template

Hi there, I am facing an issue while trying to render a custom template using the $compile function. The error message I keep getting is: unrecognized expression: {{senddata}} I have provided my code below for reference: app.controller('MainCtrl&ap ...

Observing the HTMLInputElement Object in Action with JavaScript

After utilizing JavaScript to extract the value from the SPAN element, placing it into a hidden form field, and submitting the data, I am puzzled by the unexpected result I am receiving. Can you help me understand why this is happening? <form onsubmit= ...