When overlay is utilized with jQuery, the fade in effect causes a noticeable jumping

I have integrated a "scroll down" button on my website. I want this scroll down button to gradually decrease in opacity as the user scrolls further down the page. However, I am encountering two issues with this implementation. Firstly, when I use a standard fade in and out effect, the opacity of the element jumps as it transitions between different z-index values, resulting in an unpleasant visual effect. Here is the JavaScript code snippet I used:

$(window).scroll(function() {

  if ($(this).scrollTop()>0)
  {
    $('.godown').fadeOut();
  }
  else
  {
    $('.godown').fadeIn();
  }
});

You can view the code here.

Next, I attempted to create a smoother transition by adjusting the opacity based on the scroll position from the top of the page. However, I haven't been successful in making this code work effectively:

var target = $('.godown'),
  targetHeight = target.outerHeight();

$(document).scroll(function(e) {
  var scrollPercent = (targetHeight - window.scrollY) / targetHeight;

  if (scrollPercent >= 0) {
    target.css('opacity', 1 - scrollPercent);
  }
});

I would greatly appreciate any assistance in combining these two methods seamlessly, while also addressing the issue of jumping opacity experienced in the provided CodePen example.

Answer №1

Here are the steps to achieve the desired effect:

  • Retrieve the height of the page
  • Determine our initial position (which starts at maximum and decreases as we scroll)
  • Calculate a factor by dividing the position by the document's height on scroll
  • Optionally, round this number for precision
  • Set the opacity using this calculated number (the value will always be between 0 and 1)
  • You can adjust this number further to suit your preferences

Example code snippet for implementation:

$(window).scroll(function() {

  var docHeight = document.documentElement.scrollHeight;
  var x = docHeight;
  var pos = document.body.getBoundingClientRect().bottom;
  var opacity = docHeight - pos;

  x = pos / docHeight;
  document.getElementById("anchorP").innerHTML = x.toFixed(2);
  $('a').css('opacity', x);

});
@import url(https://fonts.googleapis.com/css?family=Josefin+Sans:300,400);
.godown a {
  position: fixed;
  bottom: 1vh;
  left: 50%;
  display: inline-block;
  color: #fff;
  font: normal 400 20px/1 'Josefin Sans', sans-serif;
  letter-spacing: .1em;
  text-decoration: none;
}

.godown a:hover {
  opacity: .5;
}

#scroller a {
  padding-top: 60px;
}

#scroller a span {
  position: absolute;
  top: 0;
  left: 50%;
  width: 24px;
  height: 24px;
  margin-left: -12px;
  border-left: 1px solid #fff;
  border-bottom: 1px solid #fff;
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
  box-sizing: border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<body style="background-color: red;">
  <section id="scroller" class="godown">
    <div>
      <a href="#anchor"><span></span>Scroll <p id ='anchorP'><p></a>
    </div>
  </section>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
</body>

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

Dynamically insert a new row into an HTML table using AJAX and refresh the table with .load method

I am facing an issue with my HTML table that loads data dynamically through a PHP snippet containing SQL queries. There is a Select option and a button on the table to add a new row, which triggers an AJAX procedure to send the data to PHP for insertion in ...

.functioning live, malfunctioning on

When I use the .live method, the debugger goes inside the block and opens up the dialog. However, when I use the .on method, it doesn't. Why is that? $('#btnCreateNewSet').live("click", function (e) { debugger; ...

What criteria need to be met for height: 100% to be displayed correctly?

Although I haven't had the need to use this often, there are times when it proves to be useful... when it decides to cooperate. It's interesting how sometimes the height: 100%; code works perfectly, while other times it seems to fail. It leaves ...

Issue with Responsive Behavior of Multi-Row Cards in Bootstrap 4

My goal is to create responsive rows of cards with some margin, but I am running into issues. When I use margin:5px; on my cards, the row overflows and they are not centered correctly. There seems to be extra space on the right side of the cards. How can I ...

Automatically populate the city field with Ajax

I'm trying to create an HTML form where the city field is automatically updated based on the zipcode entered. Here's my form: <form method="post" action=""> <input type="text" name="zipcode" id="zipcode"> <input type="text ...

Add HTML content individually to each item in the array

I am currently developing a plugin and I need to load a preset in order to populate a form with the relevant data. In an attempt to write concise code, I created a variable called "template" that looks like this: var Fields = '<div c ...

Unveiling the method to retrieve XML tag based on the attribute of its parent element

This snippet is a segment of XML code: <?xml version="1.0"?> <menu> <pizzas> <pizza number="0"> <title>Tomato &amp; Cheese</title> <small>550</small> <medium></medium> <large> ...

When the page is scrolled, the div is fixed in place and a class is dynamically added. Some issues may

Greetings everyone, I have a webpage with two floating divs - one is about 360px wide and the other has an auto width. As the page scrolls, the left div is assigned a class that fixes it to the screen, allowing the other div to scroll. This functionality w ...

My code seems to be malfunctioning - why can't I keep the aspect ratio?

UPDATE: Can someone please assist me with maintaining the aspect ratio of a drawn image? I am struggling to achieve this and would appreciate any help. I have been attempting to upload an image, draw it using the imageDraw() function, and fit it within a ...

Javascript involved in the process of CSS Background-Images loading after HTML Images

I quickly put together a microsite that is located at . If your internet connection isn't fast or nothing is cached, you may notice that the background-images used for CSS image-text replacement are the last items to load (especially the h1#head with ...

Enhancing list item appearance by replacing bullets with Font Awesome icons

I need to create a list similar to this example https://i.sstatic.net/w84xS.png Although I successfully replaced the bullet with a fontawesome icon, the result is not quite right as shown here https://i.sstatic.net/yqnbJ.png You may notice a difference ...

The footer menu fails to glide upwards

I designed a top menu for my website that includes 2 key features: Sticky navigation bar when scrolling Slide toggle navbar You can view the demo here: JSBIN However, I am facing 2 issues: When the page is at the top and the menu is in its default pos ...

executing a JavaScript function in a separate .js document

When working with the success callback function in my AJAX post, I encountered an issue trying to call a function from another JavaScript file. Within page1.html: <head> <link href="style.css" rel="stylesheet" type="text/css" /> <s ...

Add a new to-do list item to the current list using jQuery

Currently, I am in the process of developing a todo list application using jQuery. However, I have encountered some issues and bugs that are causing challenges. Initially, my objective was to set the first element through HTML code, which is functioning as ...

The webpage is inaccessible only on mobile Safari, with an error message indicating a lack of internet

Initially, I do not possess an iPad, however, some clients have reported an unusual issue with one of my websites on the iPad. They are unable to access any page on the website, instead encountering a blank page with the error message: "Safari cannot open ...

Encountering issues with accessing the clientWidth and clientHeight references of the DOM in Vue

Issue with 'clientWidth' and 'clientHeight' properties on Vue and Element types. <div class="invoice-step-detail" id="invoice" ref="invoice"> @Component({ name: 'CreateInvoice', co ...

Utilizing JavaScript and the Document Object Model (DOM) to display images within a

My main goal is to have images load dynamically as the background of the browser frame, without any white spaces or repeats. I've successfully achieved this statically, but now I want to implement it so that different images are generated randomly. W ...

"SASS: Troubleshooting the Issue with nth-child Selector Not Functioning Proper

I am currently attempting to apply specific styles to all items in a list except for the first child. While I believe I have correctly set up the SASS, the style is not being applied as expected. HTML <div class="list"> <div *ngFor="let item o ...

Employing a for loop in jQuery with a brief pause between each iteration greatly enhances the functionality

Hey there, I'm looking for some guidance on implementing a pause in a for loop. I've been exploring the setTimeout() function but need help understanding how to use it effectively in my code. If anyone can assist me with integrating it or has su ...

Sending AJAX request within a Twitter Bootstrap modal in Symfony2

After exhausting countless Google and StackOverflow search results, I have come to the conclusion that seeking help is my best option. I am currently developing a Symfony2 application. In every view of my app, I have integrated a Twitter Bootstrap modal e ...