When the height of the html and body is set to 100%, ScrollY/ScrollTop is unable to function properly

Utilizing height: 100% and overflow: auto on html/body has helped me fix a scrolling issue in Chrome. However, I've noticed that ScrollY and ScrollTop always return 0 now. Is there a way to determine the scroll position in this scenario?

Here is the HTML structure:

<html>
    <body>
        <div class="wrapper">
            <div class="top-nav">
            <div class="scroll-wrapper">
        </div>
    <body>
</html>

And here is the CSS used:

html {
    height: 100%;
    overflow: auto;

    body {
        height: 100%;
        overflow: auto;

        .wrapper {
            .top-nav {
                z-index: 999;
                position: fixed;
                top: 0;
            }
            .scroll-wrapper {
                padding-top: 45px;
            }
        }
    }
}

Additionally, the JavaScript code includes:

    // Apply styles to top-nav when scrolling down

    $('.wrapper').scroll(function () {
        if (window.scrollY > 0)                        // always returns 0
        if ($('.wrapper')[0].scrollTop > 0)   
        if ($('.scroll-wrapper')[0].scrollTop > 0)
    }

Answer №1

UPDATE

The root cause of your problem seems to be attributed to the presence of both html and html body with the CSS rule overflow: scroll.

Please try removing one of these CSS rules to see if it resolves your initial issue.

html {
    height: 100%;
    /* overflow: auto; */

    body {
        height: 100%;
        overflow: auto;

       /* ... */

    }
}

Additionally, ensure that your JavaScript code checks for document scrolling rather than just the wrapper element.

$(document).scroll(function () {
    console.log(document.body.scrollTop)
    if (document.body.scrollTop > 0) {
        // Perform Action
    }
})

I hope this helps in resolving your problem.

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

Managing empty props in React applications

I am facing an issue where I need to display an image fetched from an API on app start, but when I try to render it in React, the application crashes with the error message: TypeError: Cannot read property 'icon' of undefined. Although the icon ...

Looping animations using AngularJS

I have implemented a custom directive to trigger an animation on an element when a specific field is empty on the page. However, I am facing an issue where the animation only works once when the user clicks the button with the directive. Subsequent clicks ...

AngularJS $routeProvider is experiencing difficulties with its routing functionality

I am a beginner with Angular and I'm trying to understand how multiple routes can lead to the same view/templateUrl and controller. This is what I have written: angular .module('mwsApp', [ 'ngAnimate', 'ngCookies&ap ...

Troubleshooting: AJAX takes precedence over PHP for log in issues

I've been working on creating a login form using html > ajax > php, but I've run into an issue with the php code not working properly. I'm not sure where the problem lies, but I suspect that the ajax is unable to execute my php file. A ...

Is your JQuery Gallery experiencing issues with the next button function?

I'm working on developing a simple gallery using JQuery. The main concept is to have all image files named x.png (where x is a number), and the program will then add a number to the current one, creating x+1.png and so forth. Here's the code I ...

What is the best way to transfer a property-handling function to a container?

One of the main classes in my codebase is the ParentComponent export class ParentComponent extends React.Component<IParentComponentProps, any> { constructor(props: IParentComponent Props) { super(props); this.state = { shouldResetFoc ...

Checking for duplicate clicks on an element using jQuery

Is there a way to determine if the user is interacting with the same div element? I attempted the following code snippet without achieving the desired outcome: _oldthis = null; var _this = $(this); if(_oldthis == _this) { alert(&apo ...

Retrieve DirectionsResult data from a Google Maps URL

Currently, I am developing an updated tool that can transform Google Maps directions into GPX files. The initial version of this tool is performing quite well: it utilizes the Google Maps Javascript API (v3) to showcase a map on the website, allowing users ...

Is there a way I can obtain the code for a message box?

When I refer to a message box, I am talking about a container that gives users the ability to input their text and access different features like BOLD, ITALIC, color, justify, etc., in order to customize their message's appearance! (Think of it as the ...

Material-UI: The Mystery of Missing CSS Properties

Struggling to apply a CSS class to an element from Material-UI: bw:hover { -webkit-filter: grayscale(100%); /* Safari 6.0 - 9.0 */ filter: grayscale(100%); } Tried using makeStyles: import { makeStyles } from '@material-ui/core/styles' ...

Is React State with JSON Object Losing Updates?

Encountering a strange issue here. At the start of my program, I load various values into a JSON object stored as a User State Object in a Postgres table. Although the object is quite large, I'll provide an example below. const [person, setPerson] = u ...

Text seems to be more robust when placed on a dark backdrop

I am currently facing an issue with fonts, particularly Google's Roboto font. In my tests, I have noticed that the font appears bolder on dark backgrounds (using Chrome 62.0.3202.62 on OSX Sierra 10.12.6), while it looks fine on Google's dark fon ...

When attempting to use dynamic imports with `react-icons`, NextJS will import all necessary components and dependencies

My current task involves incorporating an Icon from the react-icons package into my project. However, when I attempt to do so using an import statement, the resulting bundle size looks like this: Route (pages) Size First Lo ...

Can heuristic methods be utilized to identify encoding in XML/HTML documents?

Imagine having an XML file stored on a remote computer with an unknown encoding. The goal is to read and manipulate this file, but the challenge lies in identifying the encoding used within the document. Considering the <?xml version="1.0" encoding="xx ...

Encountering issues with link button redirection

I have a question regarding a Link element and CustomButton element with an onClick handler. < Link to = "/dashboard" style = { linkStyles } > < CustomButton text = "Login" onClick = { handleSubmit } /> < /Link> The code ...

The customization of a class within an array loop is not functioning properly

I'm facing an issue where I've added a class (question) to each element in an array, and the loop is running smoothly. However, I'm having trouble getting jQuery to recognize the class - it's almost as if it doesn't exist... I sus ...

Guide to positioning an image absolutely within a div

I am struggling to position a small close image within a div without success. The goal is for the image to always appear on the left side of the div, regardless of the content. The content consists of a text label with a maximum length of 8 characters. Iss ...

Having difficulty invoking a PHP file with the $.post() function in Ajax

I've been attempting to call a PHP file in the same directory as my HTML using the $.post() method of Ajax. Unfortunately, I'm encountering a "file not found" (404) error. Here is the code snippet that I'm currently using: $(document).read ...

Harnessing the power of jQuery ajax in conjunction with the versatile knockout.js mapping

I'm currently attempting to utilize jquery's $.ajax function along with the knockout.js mapping plugin. The test api being used can be found here: However, I seem to be encountering an issue where the data retrieved is not in the expected format ...

Trouble with submitting an AJAX form in ASP.NET MVC

Recently, I encountered an issue with a form that is supposed to upload a file with specific details like name and category. The form uses the "post" method on the page using the ajax.beginform call. However, whenever I click submit, it simply redirects me ...