Scrolling may cause Opacity to return a negative value

I'm facing an issue with my code that involves fading the opacity of a div named .overlay when the user is 100vh from the bottom of the screen. The problem arises when the screen width changes and causes elements to grow taller, resulting in the opacity function generating a negative number.

The logic within the if statement seems correct; it's the opacity function that's causing trouble. Any suggestions on how to fix this?

$(window).scroll(function() {
  // Begin adjusting footer opacity at 100vh  
  if ($(window).scrollTop() + $(window).height() >= $(document).height() - $(window).height()) {
    var scrollTop = $(this).scrollTop();
    $('.overlay').css({
      opacity: function() {
        var elementHeight = $(this).height();
        return 1 + (elementHeight - scrollTop) / elementHeight;
      }
    });
  } else {
    $('.overlay').css({
      opacity: function() {
        return 1;
      }
    });
  }
});
header {
  background: blue;
}

main {
  background: white;
}

footer {
  background: blue;
  position: relative;
}

header,
main,
footer {
  min-height: 100vh;
}

.overlay {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  background-color: #000;
}

.add-height{
  min-height: 1200px; 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header></header>
<main>
  <div class="add-height">Hello</div>
</main>
<footer id="footer">
  <div class="overlay"></div>
</footer>

Answer №1

$(window).scroll(function() { 
  if($(window).scrollTop() + $(window).height() >= $(document).height() - $(window).height()) {
    var transparency = ($(document).height() - $(window).scrollTop()) / $(window).height() - 1; 
    $('.overlay').css({
      opacity: function() {
        return transparency;
      }
    });
  }
});

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

Setting up Webpack 5 to utilize global jQuery

I implemented jQuery on my website by linking to a CDN through a script tag, loading it into the global scope. The site functions properly with various small JavaScript snippets utilizing jQuery. I prefer to maintain this method of loading jQuery. In addi ...

picking out a particular set of data from a JSON document

I have a map of Europe along with a JSON file that displays the unemployment rate for each country in the year 2011. The JSON file also includes x and y elements, allowing me to place a blue circle on top of each country on the map. My goal is to be able ...

Troubleshooting Compatibility Issues: JavaScript Function Works in Chrome but not in Internet

After collaborating with fellow coders to develop info boxes using HTML, CSS, and JavaScript, I have come across an issue with the hover functionality not working properly in Internet Explorer. Interestingly, everything functions flawlessly on Google Chrom ...

Unexpectedly large icon height in font icon

I have encountered a issue with the icon font that I am using in my project, which was generated by fontastic.me. It seems that all icons are positioned slightly higher than expected. See the code snippet below: .icon-camera { font-size: 40px; ...

What is the process for sending dynamic emails in Business Catalyst?

I'm currently using Adobe Business Catalyst for my project. I've set up a web form with several fields, including three dynamic fields from "Web Apps." One of the fields is called "Select Location." Each location has an associated email address ...

Extract the title of a research paper from the personnel website

I am looking to extract the title and authors of journal articles from the official web pages of all staff members. For example: https://eps.leeds.ac.uk/civil-engineering/staff/581/samuel-adu-amankwah The specific section I need to access is this: https: ...

Why isn't jQuery toggle working to toggle, and how can I fix my background

Hello everyone, I am new and just created this page. Unfortunately, I'm facing two issues - I don't want my background to repeat and my jQuery toggle isn't working :( I have included a snippet of my code below. Can someone please guide me o ...

What is the best way to include a clickable button in an HTML email instead of a plain link?

My application has a feature where it sends emails through Java code. I want to include a clickable link within a button that says "Activate your account". Here is my current code snippet: Message message = new MimeMessage(sessionMail); message.setFrom(ne ...

Is it possible to minify HTML in PHP without parsing JavaScript and CSS code?

After finding a solution in this discussion, I successfully managed to 'minify' HTML content. function process_content($buffer) { $search = array( '/\>[^\S ]+/s', // eliminate spaces after tags, except for ...

Transitioning the slider after displaying the full image

Currently, I am utilizing the Supersize slider for my project. However, I have encountered an issue where if an image is large and takes time to load, the slider transitions to a new image before the previous one has fully loaded. I am looking to impleme ...

utilizing different types of text style

Whenever I write a paragraph, I want the text to be styled like this: Click here for alt text http://img6.imageshack.us/img6/9894/fullscreencapture282010.jpg Do I need to use CSS to achieve this kind of formatting? I'd like it to look similar. ...

Tips on showcasing array elements within a table's tbody section and including several values within the same array

I am struggling to figure out how to display array values in my table rows that I receive from input values. I have created an array, but I can't seem to find a way to display them in the table. Furthermore, I want to be able to add more values to th ...

Eliminate a class by simply clicking on any section of the screen

I've created a dynamic "side-navigation" feature that can be hidden and shown (sliding from the right) by clicking on a specific icon. The functionality is implemented using an onclick function. Now, I'm looking to add a feature that allows the m ...

Adjust the spacing of a div based on the fluctuating height of another dynamically changing div

Is it possible to dynamically adjust the margin of a div using jQuery or JS? Currently, I have set a margin on a div by calculating the height of a container that includes images. var articleImageHeight = $('.slides_control').height(); $(' ...

Setting up internationalization (i18n) in JavaScript

I am in the process of creating a dynamic webpage using i18n's JavaScript library. I have been following their example code from their homepage located at: However, I am encountering difficulties in loading the specified JSON data, despite adhering t ...

Unveiled absolute positioned element disrupts page layout when revealed

I'm facing an issue where an absolutely positioned element is causing content to move when it's displayed. You can see the problem in action by clicking on Profile in this fiddle. Can anyone help me understand what is causing this behavior and h ...

"Utilize Boostrap's responsive columns feature to optimize targeting within your

Is it possible to create a table with rows that adjust their size based on the screen size? For example, 3 elements for large screens, 4 for medium screens, and 2 for small screens. Below is the code I used to achieve this: <div class=row> <di ...

The final column appearing as green within the Bootstrap CSS

Seeking to highlight the final column of a table in green. I am working with a table that utilizes Bootstrap styles (it's ASP.Net) and I am looking to make the last column green. While the full table is longer, I am specifically referring to 'Gr ...

Mix up your elements by having some inline and others block, all based on their neighboring elements

In my project, I am working on showcasing a list of 'input' and 'textarea' elements within a flex box arrangement. Each element needs to have a width set to 45% of the flex box. One challenging issue that arises is the following: I am s ...

ajax fails to send the variables, instead sending undefined values

In my HTML code, I have inputs and checkboxes. The JavaScript code collects the data from the inputs, and through AJAX, it should send the information to PHP where it is received and stored in the session. However, when sending the array to the PHP file v ...