JavaScript scroll event not firing

I have searched multiple questions on SO to avoid duplication, but none of the solutions worked for me.

My goal is to toggle the visibility of a button based on scroll position. I tried creating a scroll event listener to trigger a function that checks the scroll position and performs necessary actions accordingly. However, the listener did not fire as expected.

Despite referring to various documentation sources and ensuring my code is correct, I still cannot figure out what's missing here. Any insights?

Here is the relevant document: scroll event

const showHideArrow = () => {
    //console.log("func triggered");
    const arrow = document.getElementById("scroll-arrow");
    if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
        arrow.style.display = "none";
      } else {
        arrow.style.display = "block";
      }
}

window.addEventListener("scroll", showHideArrow);
section{
  max-height: 100vh;
  height: 100vh;
  width: 90vw;
  margin: 0 auto;
  
  display: flex;
  flex-direction: column;
}

.scroll-arrow {
  margin: auto auto 5rem auto;
  text-align: center;
  padding-bottom: 5px;
}
<head>
    <title>Test</title>
</head>

<body>
    <main>
        <section class="intro">
             <a id="scroll-arrow" class="scroll-arrow" target="_self" href="#about">
                <svg width="34" height="34" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path
                        d="M11.0001 3.67157L13.0001 3.67157L13.0001 16.4999L16.2426 13.2574L17.6568 14.6716L12 20.3284L6.34314 14.6716L7.75735 13.2574L11.0001 16.5001L11.0001 3.67157Z"
                        fill="currentColor" />
                </svg>
            </a>
        </section>
        <section id="about" class="about">
            <h2>test</h2>
        </section>
    </main>
</body>
</html>

Answer №1

The issue appears to be linked to the following code snippet:

html, body {
  overflow-x: hidden;
}

It's unclear why this specific CSS rule is interfering with the event listener, but for now it seems to have resolved the problem. I'll need to explore alternative methods for hiding scroll bars.

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

"Triggering ngClick causes ngSrc to update, however, the keydown event handler does not have

Currently, I am developing a dynamic live map application that includes various navigation buttons like pan and zoom. To enable keyboard control, I have implemented a keydown event handler that triggers the same function as the button clicks. Interestingl ...

"Enhance Your Coding Experience with Visual Studio Code - TypeScript Definitions Catered

I am currently working on developing a basic web application using Node.js and Express. I have successfully installed both definitions using tsd following the steps outlined in this guide. When I run tsd query mongodb --action install, I do not encounter ...

The error message "Type 'string | number' is not assignable to type 'number'" indicates a type mismatch in the code, where a value can be either

I encountered an error code while working with AngularJS to create a countdown timer. Can someone please assist me? //Rounding the remainders obtained above to the nearest whole number intervalinsecond = (intervalinsecond < 10) ? "0" + intervalinseco ...

Objects remaining static

I'm currently working on a VueJS component that has the ability to export data into .xlsx format. To achieve this functionality, I am utilizing the json2xls library, which requires an array of objects with identical keys (representing column names) to ...

Navigating horizontally to find a particular element

I developed a unique Angular component resembling a tree structure. The design includes multiple branches and nodes in alternating colors, with the selected node marked by a blue dot. https://i.stack.imgur.com/fChWu.png Key features to note: The tree&ap ...

A guide to obtaining a single access token/refresh token for unlimited use with oauth2 on the Spotify API

I'm working on a project that will exclusively rely on one Spotify account, but I'm having trouble grasping the concept of the refresh token in oauth2. Ideally, I want to generate an access token and refresh token through the Spotify API console ...

Error: Could not locate local grunt on Windows 7 Professional

When attempting to initiate grunt, an error message is displayed: c:\repositories\kunde_1\themes-projekt_1\projekt_1-responsive\source>grunt grunt-cli: The grunt command line interface. (v0.1.13) Fatal error: Unable to find lo ...

Load information from a JavaScript object when the user clicks dynamically

My challenge involves utilizing a JavaScript object that contains video information such as title and source URL. Within my HTML, I have image placeholders and the goal is to trigger a modal pop-up (using Twitter Bootstrap modal) of the specific video when ...

What is the best way to save website information in SQL Server?

My website is www dot abc dot com and it contains some valuable content that I want to store in SQL Server. Can this be done using DOM? If not, please recommend alternative methods. I've searched this forum for similar solutions but haven't found ...

Making If Statements Easier

Currently, I'm tackling an assignment that involves utilizing if statements and switch statements. Here is a snippet of code that I am working on: if (validateField(document.forms[0].name) == false) { isValid = false; } if (validateField(docume ...

Issue (@websanova/vue-auth): http plugin has not been properly configured in drivers/http/axios.js

I've been working on integrating vue-auth into my laravel-vue application, but I'm encountering some console errors: Error (@websanova/vue-auth): drivers/http/axios.js: http plugin has not been set. Uncaught TypeError: this.plugins.http is u ...

I'm curious if someone can provide an explanation for `<->` in TypeScript

Just getting started with TypeScript. Can someone explain the meaning of this symbol <->? And, is ProductList actually a function in the code below? export const ProductList: React.FC<-> = ({ displayLoader, hasNextPage, notFound, on ...

Leverage the Ajax response within a Jade template

I'm currently utilizing ExpressJS with the Jade template engine. My goal is to achieve the following: Within a Jade file, I need to extract a variable that will be used in a JavaScript file. This JavaScript file will then make an Ajax request to the ...

Tips for implementing the handleClick method within a class component, as opposed to using the export default function syntax in

Hey there! I'm a beginner in React and I'm trying to incorporate Material UI into my project. I came across the documentation for the menu section like this, but I'm facing some challenges with writing my code. Specifically, I'm struggl ...

What is the best way to access the iframe element?

This is main.html <body> <iframe id="frame" src="frame.html"></iframe> <script type="text/javascript"> document.getElementById('frame').contentWindow.document.getElementById('test').innerHtml = &apos ...

Mobile Image Gallery by Adobe Edge

My current project involves using Adobe Edge Animate for the majority of my website, but I am looking to create a mobile version as well. In order to achieve this, I need to transition from onClick events to onTouch events. However, I am struggling to find ...

The resizing issue persists with Angularjs charts

I have recently developed a small web application using AngularJS and I have implemented two charts from the AngularJS library - a bar chart and a pie chart. Although both charts are rendering correctly, they are not resizing properly as the display size c ...

Safely render user-generated HTML content within a React application to prevent cross-site scripting vulnerabilities

A unique situation has arisen in our React app where a user inputs a string that is displayed to other users. The challenge lies in searching for specific characters within the string and replacing them with HTML elements. For example, transforming the wor ...

The scrolling speed of my news div is currently slow, and I am looking to increase its

This is the news div with bottom to top scrolling, but it is slow to start scrolling. I want to increase the speed. The current behavior is the div appears from the y-axis of the system, but I want it to start exactly where I define. The scrolling div is ...

The checkbox must be checked for the conditional form to appear, experiencing a Safari bug in version 12.2. No issues found in Chrome and Firefox browsers

My form includes a billing address section with a checkbox that automatically sets the shipping address to match the billing address. If the user unchecks the checkbox, another form will appear below for entering a different shipping address. The Checkbox ...