Chrome does not allow accessing or setting the `scrollTop()` property for elements with `position: absolute`

When using the JS Fiddle provided, it was discovered that clicking the link in the sidebar scrolls the page to 400px down in Firefox, but this functionality does not work in Chrome.

Check out the JS Fiddle here

If I am unable to modify the HTML or CSS, is there a JavaScript solution to resolve this issue?

body {
  position: absolute;
  left: 0;
  right: 20%;
  top: 0;
  bottom: 0;
  overflow: hidden;
  overflow-y: scroll;
}
html {
  overflow: hidden;
  height: 100%;
}

In Chrome, the following code snippet always returns 0 regardless of scrolling distance:

console.log($('body').scrollTop());

Additionally, the following code has no effect in Chrome:

$('body').scrollTop(400);

These methods work as expected in Firefox.

Answer №1

The reason for this issue is the presence of overflow: hidden; in your CSS applied to the html selector. Once you eliminate that, everything should function as intended. Check out the revised demo here: https://example.com/demo

Answer №2

To resolve the issue, try removing the 'position: absolute' from the body attributes before attempting $("html,body").animate({ scrollTop: 400 }, "slow");

I apologize for overlooking the title... my previous solution is not compatible with elements using absolute positioning.

Answer №3

Take a look at this interactive example on JSFiddle: https://jsfiddle.net/a4qx507f/12/

To manage overflowed content, enclose it within a 'content' div.

<div id="content">
  <button class="btn">Get scroll Top</button>
  <p>
    Lorem ipsum dolor sit amet, 
   .....
  </p>
</div>

Add the following CSS to the #content selector:

#content{
  overflow-y: auto;
  max-height: 100%;
}

Additionally, remove overflow-y: scroll from the body.

body {
  position: absolute;
  left: 0;
  right: 20%;
  top: 0;
  bottom: 0;
  overflow: hidden;
  background: #c0ffee;
  padding: 0;
  margin: 0;
}

SNIPPET

$('.btn').on('click', function() {
  console.log($('body').scrollTop());
  console.log($('#top-pos').position().top);
})

$('#scroll').on('click', function(e) {
  e.preventDefault();

  $('#content').scrollTop(400);


})
body {
  position: absolute;
  left: 0;
  right: 20%;
  top: 0;
  bottom: 0;
  overflow: hidden;
  background: #c0ffee;
  padding: 0;
  margin: 0;
}

html {
  overflow: hidden;
  height: 100%;
}

.sidebar {
  width: 20%;
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  background: #333;
  color: #fff;
  padding: 1em;
  box-sizing: border-box;
}

.sidebar a {
  color: #fff;
}

#top-pos {
  top: 0;
  position: absolute;
  height: 0;
}

#content{
  overflow-y: auto;
  max-height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sidebar">
  <h4>Sidebar</h4>
... [Truncated for brevity] ...         turpis egestas.
  </p>
  ...
  <button class="btn">Get scroll Top</button>
</div>

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

When the parent element has a fixed position, the child element will no longer maintain its floating property

I'm currently working on a fixed navigation panel that sticks in place while scrolling down. Here is the code I have so far: <header> <a class="logo" href="/">Logo_name</a> <nav> <a href="#">Menu_1</a& ...

Navigating the FormSpree redirect: Tips and tricks

I recently set up my website on Github Pages and wanted to integrate a free contact form from FormSpree. However, I encountered an issue where after submitting the form, it redirected to a different website, which was not ideal. After researching online, I ...

choose the initial element within a class

Seeking a way to target the first element within each group of the same class? Consider the following scenario: <div class="group"> <div class="element"></div> <div class="element"></div> <div class="element"&g ...

How can I customize the appearance of a disabled radio button within spans?

While attempting to make my radio button square using CSS by incorporating spans and before and after elements, I encountered a new client request. The client now wants the radio buttons to be disabled with a unique style when in this state. Unfortunately, ...

What steps can I take to streamline and simplify this tab controller code?

I'm looking to simplify this jQuery code because I feel like there's repetition. As someone new to JavaScript and jQuery, I've created two tabs with their respective containers containing miscellaneous information. My goal is to have each co ...

Invoke a JavaScript file within the MongoDB shell

Running a JS file directly from the Mongo shell using the query mongo localhost:27017/west createSumCollection.js has resulted in an error: "uncaught exception: SyntaxError: unexpected token: identifier : @(shell):1:6" The content of CreateSu ...

Creating a Dynamic Input Validation Range with JQuery

Greetings and thank you for taking the time to review this! :-) The form validation is functioning correctly with required fields, but I am facing a challenge with setting up numeric range validation dynamically for an autocomplete feature. The JQuery val ...

Modify the Ng-Pattern with Jquery

Is there a way for someone to assist me in updating the Ng-Pattern with Jquery? I have looked at the questions below, but they haven't been helpful (Most of them focus on updating to dynamic values using Angular JS). Question 1 Question 2 Question ...

Execute PHP and HTML code stored in a string

I have a string stored in a variable within my PHP code that looks like this: <?php $string = " <?php $var = 'John'; ?> <p>My name is <?php echo $var; ?></p> "; ?> When using the $string variable elsewhere, I ...

I'm struggling to successfully insert and retrieve data from a MySQL database using Express.js. I need to figure out how to make these operations work seamlessly across the board

HTML CODE <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>CHAT APPLICATION</title> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+ ...

Create an AngularJS task manager that saves your to-do list even when the page is refreshed

Currently, I am developing a straightforward to-do list application using AngularJS within an ASP.NET MVC template. Surprisingly, I have successfully integrated Angular and Bootstrap to achieve the desired functionality. The app allows users to add and re ...

Error: Please provide the required client_id when setting up Google Sign-In with Next-Auth

I have been trying to implement the Sign in with Google option in my Next.js application using next-auth. Below is a snippet of my [...nextauth].js file located in the api/auth folder: import NextAuth from "next-auth"; import Google ...

Guide to Establishing a Connection to WCF Service using Ionic Project and AngularJS

Greetings, I am currently experiencing an issue tasked with connecting my Ionic project to a WCF service located on another PC (running a C# Application) within the local network. I have verified that the network connection between the PCs is functioning p ...

Can integer values be stored in localStorage similar to JavaScript objects and retrieved without requiring typecasting?

After setting an integer value to a localStorage item: localStorage.setItem('a', 1) and checking its data type: typeof(localStorage.a) "string" it shows as a string. I then typecast it to an int for my purposes: parseInt(localStorage.a) My ...

Test a JavaScript function within an Angular service using the Jasmine framework

I need to write a unit test for a JavaScript function within an Angular service using Jasmine. Here is the Angular service: angular.module("app.services").service("validationService", ["$q", function ($q) { this.validate = function (filter): ng. ...

Text overlaps when li element is nested inside another element

Seeking assistance in creating a CSS/HTML menu bar that triggers list elements on hover. Facing difficulty arranging the list in two columns. Code Sample:http://jsfiddle.net/Km922/1/ <!DOCTYPE html> <html lang="en"> <head> <meta ...

Exploring the implementation of query parameters in Nest.js

I am currently a freshman in the world of Nest.js. Below is an excerpt from my code: @Get('findByFilter/:params') async findByFilter(@Query() query): Promise<Article[]> { } I have utilized Postman to test this specific router. ht ...

What is the best way to add a line break to a menu item?

Looking for some assistance with Angular Material here. I am trying to design a menu that includes an item with two lengthy paragraphs, but the text is getting truncated and only showing the first paragraph. If anyone could offer guidance or help, it woul ...

Can a function be returned using AJAX?

Suppose I am in need of initiating an ajax call to my server $.ajax({ type: 'POST', url: 'url/url', success: function(response){} }); and upon receiving a response from the server, I send some JavaScript code res.send(&qu ...

Utilizing Dojo widgets in Xpages

I'm having trouble applying CSS to certain dojo elements within my Xpage. Here is the CSS style sheet I am using: .formFields { color: rgb(5, 56, 107); font-family: Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: nor ...