Is there a way to adjust the transparency of text as I scroll, but specifically when it is within the window's view?

My attempt at changing the opacity of text blocks based on the scroll position is not functioning properly. The goal is to fade in each text block as it comes into view and fade out when it goes out of view while scrolling.

You can see an example of what I mean on this website here - look for the features section.

screenshot showing the referenced section: https://i.sstatic.net/eBP9i.png

$(window).scroll(function() {
    var scrollTop = $(this).scrollTop();
        $('.text-block').each(function () {
          $(this).css({
              opacity: function() {
                  var elementHeight = $(this).height(),
                  opacity = ((elementHeight - scrollTop) / elementHeight);
                  return opacity;
            }
      });
    });
});     
h1 {
  height: 100vh;
  background: red;
  text-align: center;
}
.text-block {
  width: 50%;
  margin-bottom: 150px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>SCROLL DOWN</h1>
<div class="text-block">
  Content of the first text block.
</div>
<div class="text-block">
  Content of the second text block.
</div>
<div class="text-block">
  Content of the third text block.
</div>
<div class="text-block">
  Content of the fourth text block.
</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

What are the potential disadvantages of relocating the login logic from the 'signIn()' function in NextAuth.js?

My experience with NextAuth.js for the first time has led me to realize that signing in using the Credentials provider can be a bit tricky when it comes to error handling. It seems like the default implementation should resemble something along these lines ...

The AJAX request is not functioning properly, even on a basic form like this one

Here is the current code I am testing. Due to some issues with AJAX calls, I am unable to make it work. Can someone please help me identify what might be wrong? test.php <html> <body> <form id="myform"> <input type="text" id="fname" ...

Accessing multi-dimensional array properties in PHP with JavaScript integration

In my PHP code, I have an array structured like this: <?php $data = array(); $data[0] = array('year' => 2001, 'month' => array( 'January' => array('val1' => 1000, 'v ...

The non-responsive feature of PrettyPrint with Bootstrap makes it stand out from

Currently, I am utilizing Google's prettyprint alongside bootstrap to exhibit SQL statements with syntax highlighting. However, there seems to be a disconnect in terms of responsiveness, as it does not adhere to the column width of 12 in bootstrap. In ...

What is the best way to retrieve a Rails variable that is restricted to a specific partial?

As a newcomer to Ruby on Rails, I find myself struggling to grasp the larger concept. Any assistance you can offer would be greatly appreciated. Within my application.html.haml file, I utilize =yield to pull content from ranked.html.haml. Currently, this ...

Developing a tool for switching between languages in an internationalization application

I have been exploring the implementation of Lingui(i18n) in apps. All set up, but I'm interested in adding a language switcher to enable users to change between language catalogs on my app. Here's my index.js file: import React, { useEffect } fr ...

Unable to transfer data to the dialog box; "Failed to execute 'addHandlerAync' on 'Office.context.ui'"

I am attempting to transmit data to a dialog box within an Outlook web add-in. var url = window.location.origin+'/dialog.html' var dialog Office.context.ui.displayDialogAsync(url2, function (asyncResult) { dialog = asyncResult.v ...

Creating a directive in AngularJS to automatically populate select options with custom values

As someone who is relatively new to creating directives, I am looking to develop a directive that will assist me in my application. What I aim to achieve is a select element directive that comes pre-populated with options and is self-contained. All I need ...

Toggle slides smoothly using React

I'm struggling to implement slideToggle functionality in React similar to how I did it with jQuery. The goal is to display a list of recipe headings and hide the content underneath to prevent the page from becoming too long. When a user clicks on a he ...

Using a custom ParcelJS plugin from the local directory

I am interested in developing a plugin to enable raw loading of a specific type of file with Parcel. According to the documentation, Parcel detects plugins published on npm with certain prefixes and automatically loads them along with their dependencies li ...

Displaying a jquery delay prior to revealing the content

I need help figuring out how to temporarily hide something using jQuery. Unfortunately, the code I've written doesn't seem to be functioning correctly. Could it be that the delay time is too short? $('#mainForm').hide().delay(8000).sh ...

Tips for utilizing the stretched-link bootstrap class while maintaining the :hover effect directly on the card

I recently customized a bootstrap product card to allow users to access the product details either by clicking on the card itself or by clicking on the product name. To achieve this, I utilized Bootstrap's stretched-link class. However, an issue arose ...

Discover a non-null string within a deeply nested data structure

Within the 'task' object, the value of the 'door' property can vary each time it is accessed: var task = {}; task["123"] = [{"door":""}, {"door":""}, {"door":""}, {"door":""}]; task["456"] = [{"door":""}, {"door":"close"}, {"door":"" ...

What mechanisms does Vue employ to halt the browser from sending a server request when the URL in the address bar is modified?

When a link <a href='www.xxx.com'> is clicked in a traditional HTML page, the page is redirected to the new page. In a Vue application, we utilize the Router. See the code snippet below. Vue.use(Router); export default new Router({ mode ...

CSS class is functioning properly in conjunction with the textarea element, but it is not properly

I have a CSS class called ".form_bg_color" with the following definition: .form_bg_color{ background: red; } When I apply this class to my text field in the view like so: <%= f.text_field :source, class: "form_bg_color" %> The color of the input ...

Incorporate a dojo tooltip dialog into every cell of the table

Trying to implement a dojo tooltip dialog for each table cell so that hovering over each cell reveals its content. The tooltip dialog is necessary as there are clickable elements within it. I am aware of how this can be achieved using the tooltip control, ...

Strategies for eliminating _next folder from file path within the build directory of Next Js

Is there a way to eliminate "_next" from the path in the build folder for Next Js version 13.2.4? For reference, here is an example: We would greatly appreciate any assistance on this matter. ...

phpif (the current date is after a certain specific date, do

Can someone please help me solve this problem? I want to prevent echoing a variable if the date has already expired. Currently, my PHP code displays: Match 1 - April 1, 2015 Match 2 - April 8, 2015 What I need is for Match 1 to not be echoed if the cur ...

Struggling to convert a vb.net string into a json string through Serialization

I'm struggling with the following code, can someone please point out where I'm making a mistake? VB For Each dr As DataRow In dvItems.Table.Rows strItems &= "'" & dr("ItemTitle") & "'," Next strItems = strItems.Trim("," ...

Tips for retrieving a value from an async function called within the .map function in React?

After doing some research, I discovered that async functions return a promise whose result value can be accessed using .then() after the function. This is the reason why it's not rendering properly. My question is: how can I render the actual value fr ...