The scrollTop function consistently yields a value of 0 in this situation

I am trying to achieve a scrolling effect where the cover title moves up as I scroll, but unfortunately I am having trouble getting the scrollTop function to work using vanilla JavaScript.

Here is the code snippet:

window.onload = function(){
  var coverTitle = document.querySelectorAll('.cover-title');
  window.addEventListener("scroll", function(){
    console.log(coverTitle.item(0).scrollTop);
  });
};
.cover{
  width: 100%;
  height: 500px;
  background-image: url('img/cover.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  position: relative;
}

.cover h1{
  color: white;
  position: absolute;
  font-family: serif;
  top: 70%;
  left: 10%;
}
<div class="cover">
  <h1 class="cover-title animate fadeIn">Some text</h1>
</div>

Answer №1

As per the documentation

The Element.scrollTop attribute either retrieves or sets the number of pixels by which an element's content is scrolled vertically. It specifically measures the distance between an element's top and its topmost visible content. If an element's content does not require a vertical scrollbar, then scrollTop will default to 0.

Documentation: https://developer.mozilla.org/en/docs/Web/API/Element/scrollTop

The main point highlighted is that.

If the element cannot be scrolled (e.g. has no overflow property or is designated as "non-scrollable"), then scrollTop will be set to 0.

Since your element lacks an overflow property, its initial scrollTop value is naturally set to 0.

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 retrieve JSON values based on a key using JavaScript, jQuery, or AngularJS?

Here is some JSON data that I need help with: var jsonData = { "title": "Testing", "settings": { "mySettings": false }, "jsonList": ["TestingList"], "testJsonVals": { "Test1": { "name": "name1", ...

Problem with flags series in Highcharts/Highstock

Can anyone help me figure out why not all the flags in the withdrawals series are displaying? For reference, you can view the following JS fiddle: https://jsfiddle.net/lucianpurcarea/5zxa0jsm/13/ The snippet of code below is responsible for creating the d ...

Use the json method and JavaScript to bind the data

I have been attempting to fetch data for a dropdownlist using JSON. While the program is functioning correctly, the dropdownlist is displaying a blank list. Upon inspection, I can see that the data is present but not being displayed. Controller: publi ...

mongoose populate method does not return the expected results

My Project Objective I am currently in the process of creating a travel booking platform for a transportation company. One of the key features I am working on is displaying the name of the individual who made the booking on a specific page within the webs ...

Unable to display any posts in my React application, no content is being produced

I am currently in the process of developing a blogging site using React by following a tutorial. However, I have encountered an issue where the posts are not appearing on the screen. Here is the link to my GitHub repository: https://github.com/AkshatGarg2 ...

Popups generated on the fly without any triggers from links

I'm populating a listview with items from localStorage. When a user clicks on a list item, a corresponding popup should appear. I already have a working solution where the popups are displayed upon request. However, this time I am dynamically adding t ...

The form is not being submitted when I click on the submit button because I have two buttons that trigger AJAX requests

I am facing an issue with a form where there is a submit button inside it, as well as another button within the form that has attributes type=button and onclick=somefunction(). The button with the onclick function works perfectly fine, but the other button ...

Learn how to efficiently pass multiple props using a loop in Vue

I am dealing with an object that has multiple properties. Typically, I know which props I want to pass to my component and do it like this: <component :prop1="object.prop1" :prop2="object.prop2" :prop3="object.prop3" /> However, I want to pass the ...

Acquiring the handler within the on() function

$( document ).ready(function() { $('#handler').on('click', '.selector', function(){ alert( $(this).text()); }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script&g ...

Combine several forms into a single submission

On the webpage, I am faced with the challenge of having two separate forms that need to function as one cohesive unit for the user. Despite my efforts to reuse code and integrate elements seamlessly, I am unable to consolidate the two forms into a single e ...

Updating Tailwind CSS to accommodate older web browsers by converting modern rgba() notation to be browser-compatible

I am facing a challenge with Tailwind CSS v3+ as it builds colors into the rgb space/color notation that is not compatible with an older browser (Safari 11) that my web app now needs to support. For example, rgb(163 160 158 / var(--tw-bg-opacity) The iss ...

Using Symfony2 to make an Ajax request in order to show a message based on a particular condition

As a novice in the realm of JavaScript and Ajax, I am struggling to find a way to display a message based on certain conditions. Let's consider a scenario where a user can adjust the quantity of a product they wish to purchase. While implementing thi ...

Is it possible to transmit parameters when calling an ajax function on an HTML page?

I am trying to implement multiple buttons that trigger the same ajax function. Each button should be able to pass its own unique ID so that the ajax function can apply its action on a specific element related to that button. Is it possible to include a p ...

Easily ajaxify your website without the need for hash or hashbang URLs

Currently enrolled in a web design course, I am eager to explore the world of ajax and how it can enhance our projects. Unfortunately, our instructor focuses solely on design and HTML, leaving us to figure out the more technical aspects on our own. If I m ...

Receiving an error when trying to import the 'marked' module into an Angular project

Working on a project with Angular 15, I recently added marked to transform MarkDown text to HTML using an Angular pipe. However, no matter how I import it, I can't seem to get it working and keep encountering errors. I followed these steps: npm i mar ...

The "Mongoose 'model is not a constructor' error" is causing issues

It appears that I am encountering a rather common error. Despite reviewing previous questions, none have been able to resolve my issue. The typical mistakes involve using module.export instead of module.exports, and initializing a new schema instance inste ...

Create personalized auto-suggestions based on user preferences

I'm struggling with the code below: jQuery("#hotelName").autocomplete({ serviceUrl:'xxxxxxxxxxxxxxxxxxxxxxxx', minChars:1, delimiter: /(,|;)\s*/, // regex or character maxHeight:200, ...

When implementing protractor spyOn() with jQuery's ajax() function, an error is triggered stating 'ajax() method is non-existent'

I am currently testing the functionality of using AJAX to submit a form. Below is the Protractor code for the test: describe('login.php', function() { it("should use ajax on submit", function() { browser.get('/login.php'); spyOn($ ...

Pass data back and forth between app.js (node) and javascript (main.js)

I am facing a challenge in sending and retrieving data (username) between app.js and main.js. In my setup, I have a node app.js that calls index.html which then triggers the main.js function called "clicked". Below is the code snippets for each file: app. ...

Is there a way to verify if a folder exists at the root of the project?

I'm currently working on a NPM script where I have a folder called "scripts" that contains all of my scripts. My goal is to check if there is a folder named "docs" at the root of the project, and if it exists, delete it. If not, then perform another t ...