Tips for hiding a class while scrolling up with jQuery

I have been experimenting with making classes appear and disappear based on scrolling behavior in a browser. I successfully made a class appear when reaching a certain height using the following code:

jQuery(".tetapElement").css("opacity", 1);
 
 jQuery(window).scroll(function() {
var windowHeight = jQuery(window).height();
if (windowHeight < 470) {
    jQuery(".tetapElement").css("opacity",1);
} else if (windowHeight > 1820) {
jQuery(".tetapElement").css("opacity",1);
}
else {
    jQuery(".tetapElement").css("opacity",0);
}
});
.tetapElement {
  position:fixed;
top: -30%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!--make another checkout button -->

<div class="tetapElement">
<div class="order_title">Order details:</div>
<div class="order_bar_details">
<div class="pack_name"><?php echo $post->post_title ;?></div>
<div class="pack_name_value">Package name:</div>


<div class="pack_details"></div>

<div class="addon_title">Add-On Menu</div>
<div class="addon_details"></div>
</div>
<div class="order_price">Total price (<?php echo $currency; ?>): <span class="total_price">0</span></div>

<div class="chekout_link">

<textarea id="order_details" style="display:none;" name="order_details" ></textarea>
<?php wp_nonce_field('process_checkout_action','process_checkout_field'); ?>
<input type="submit" class="btn btn-success checkout_btn mk-flat-button shop-black-btn" value="Checkout">

</div>

</div>

You can view this functionality in action here. When clicking on the link and reducing the browser height, a different class will pop up. Now, I am trying to figure out how to make this new class disappear again as the user scrolls back up, in order to maintain a smooth transition between classes.

In summary, I want one class to disappear as the user scrolls down, replaced by another class. And then, I want this second class to disappear as the user scrolls back up, reverting to the original class. Any suggestions on how to achieve this using jQuery?

Answer №1

For a similar query, refer to this thread.

let previousScrollTop = 0;
$(window).scroll(function(event){
   let currentScrollTop = $(this).scrollTop();
   if (currentScrollTop < previousScrollTop){
       // code for scrolling up
       $(".fixedElement").removeClass("classToRemove");
   }
   previousScrollTop = currentScrollTop;
});

Answer №2

In my view, using a media query with max-height is the more effective approach for making changes to CSS properties.

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

Forward visitors to the Next.JS website

Currently, I have a basic web page built with next.js that simply displays the "Hello World" element. However, I am looking to create a redirect from this page to another URL (specifically YouTube). This is essentially meant to serve as a redirection page ...

The issue of `for` loop and `forEach` loop not functioning in EJS

I'm currently developing a To-Do App and facing difficulty with the POST request functionality. I've attempted using both a for loop and a forEach loop but none seem to be working. <% todos.forEach(item => { %> <li><%= item ...

Having issues with EasySearch functionality; it was functioning correctly previously

Using Meteor has completely transformed my web development experience. Javascript has become incredibly fun for me. I recently integrated the EasySearch solution on my site, but unfortunately it seems to have stopped working. It was functioning perfectly ...

What is the process for importing module functions in Svelte when utilizing npm?

I'm having trouble understanding how to properly use imports in Svelte with npm. I have a Svelte component that I can't seem to get working. Uncommenting "//import { Configuration, OpenAIApi } from "openai";" leads to the error message: [!] (plug ...

Switching the favicon to utilize the favicon.ico file

After initially adding a favicon to my HTML page, I attempted to change it to a different one. However, the first favicon seems to be stuck and won't move, even after removing the link tag entirely. I created a new favicon (favicon.ico) and tried impl ...

Enhancing Angular with Plotly: Implementing click events on bar chart legends

I'm currently working on implementing color pickers for my plotly-plot charts within an Angular template. I am looking to add a function that triggers when the chart legend is clicked. How can I achieve this and get a click event for the chart legends ...

Fetching data from Page 1 and passing it to Page 2 using Javascript

I'm currently experiencing an issue with my AJAX function where it needs to transfer a value from page 1 to page 2 for storage. Let's start with the AJAX function on page one: top.location.href = 'http://www.something.com/redirect.php?emai ...

Issues with cross-domain AJAX JSONP requests not functioning in Internet Explorer 8

After enabling CORS support using jquery-1.9.1, I made an ajax request with jsonp callback support from the server side. $.ajax({ type: 'GET', url: url, async: false, contentType: "application/json", jsonpCallback: 'jso ...

Strange behavior occurs when a DOCTYPE is specified in XSLT and javascript interactions

Within our XLST, we utilize "vanilla" JavaScript to manipulate certain div elements within our reports. One particular div sits in the center of the screen and overlaps all other content. The code snippet below is used to position this div when the page lo ...

Spring Ajax response returns empty objects in the list

I'm currently developing a locker management system which includes an update history feature for the lockers. I am interested in implementing Ajax to retrieve a list of this update history, limited to X entries. However, when I return either List<L ...

Altering the color of text in hyperlinks within a list

Recently, I've started exploring CSS styling and I'm curious if there is a more effective way to change the color of link text when they are within a list. Currently, all my links have the same color and I'd like to enhance this aspect. < ...

Timing of Vue mounting and initialization phases

I am currently working on a component where I pass the reference to an internal element to the parent using defineExpose. In this example, I simply log the element, but in my actual project, I intend to manipulate it. SFC Playground // Comp.vue <scrip ...

Node.js server crashes upon automatic startup but remains stable when started manually

Currently, I am using Node.js and Socket.io to power an online chat feature. To manage the server, I have created a configuration file located at: /etc/init/example.conf The contents of this file are as follows: start on startup exec forever start /var/ ...

My initial reaction to the code

I recently completed my first React code, which is the classic "Hello World" example. Unfortunately, I'm encountering some issues with it and despite trying various solutions, nothing seems to work. Below is the code snippet: <html> <h ...

Tips for automatically closing a bootstrap modal popup when there is no data available in the dialog

Using a bootstrap model here, each card in the model has a close button. Clicking on the close button will hide or remove the card from the model popup. If there is only one card left in the popup and I want to close that last card, I also want the model p ...

Emphasizing specific terms by changing them (but only within p tags)

After spending about 8 hours searching for a solution yesterday on various platforms, I almost gave up. But then I realized I should turn to this community for some much-needed help. So here is my first question - a big thank you to everyone who has helped ...

The error message "TypeError: Trying to access 'insertBefore' property of null object" is indicating that the

Below is an example of the array I am using: var users = [ { 'id': 1, 'name': 'user name', 'age': 25, ... }, { 'id': 2, 'name': 'user name', 'age': 25, ... } ... ...

Jquery form extension

I've implemented the Jquery form plugin for submitting login form data and it's working well. However, I am facing an issue with redirecting a successfully logged-in user to the client panel from the login page in my PHP script. Whenever a user s ...

What is the best way to access nested JSON array data that includes varying key names and content?

In my current project, I am trying to extract data from a JSON array. Here is my approach: Below is the jQuery/JavaScript code I used to generate and retrieve data, particularly focusing on the nodes object which determines different layouts: var getPost ...

What methods can I use to prevent a JavaScript array from getting filled during page loading?

Looking for assistance to populate an array using JQuery with option values from multiple select elements. I need the array to reset and fill with new options each time a select element is used. Strangely, despite my efforts, the array appears pre-filled w ...