executing various functions while scrolling through the window

I have a layout with main divs containing sub divs, each with specific CSS classes that include images. My single-page website has a lot of images, so I want to implement a window scroll effect. After researching on Google, I couldn't find a suitable solution. While the page is scrolling, I need to dynamically remove certain classes using jQuery's removeClass() method. I already have a function set up for the window scroll event, and now I want to delete classes as the page scrolls down.

I'm wondering how to use multiple functions on the window scroll event, bind two or three functions to it, and execute them sequentially.

$(window).scroll(function () {        
        if ($("#first").offset().top + $("#first").height() < $(document).scrollTop()) {
            var obj = $('#first').children('div');
            if (obj.hasClass("d")) {
                obj.removeClass();
            }
            obj = obj.children('div');
            if (obj.hasClass("c")) {
                obj.removeClass();
            }
            obj = obj.children('div');
            if (obj.hasClass("e")) {
                obj.removeClass();
            }
        } else {
            var obj = $('#first').children('div');
            if (obj.hasClass("d") == false) {
                obj.addClass("d");
            }
            obj = obj.children('div');
            if (obj.hasClass("c") == false) {
                obj.addClass("c");
            }
            obj = obj.children('div');
            if (obj.hasClass("e") == false) {
                obj.addClass("e");
            }
        }
<div id="firstMain">
<div class"a"> </div>
<div class"b"> </div>
<div class"c"> </div>
</div>
<div id="Second"></div>
<div id="Z"></div>

Answer №1

To have multiple handlers executed in a specific order, simply add them to the event listener one by one.

$(window).scroll(firstScrollHandler);
$(window).scroll(secondScrollHandler);

function firstScrollHandler(event) {
  // Add your logic here
  // This will be executed first
}

function secondScrollHandler(event) {
  // Add your logic here    
  // This will be executed second
}

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

Positioning Problems with Popups

I have been facing an issue with the positioning of a popup in my trivia game. Despite trying multiple solutions, I have not been able to achieve consistency. Here is a snippet of the HTML code: <div class="logo"> <img src="css/rio-40.png"/& ...

Ways to determine the name of the calling function in an AJAX or XMLHttpRequest request?

I am currently exploring ways to programmatically identify the function name responsible for triggering an Ajax call in JavaScript or jQuery within an existing codebase. As I delve into instrumenting a large existing codebase, I am seeking to pinpoint the ...

Preserve Tinymce's Raw Html in Laravel 4

Is there a way to retrieve the HTML source code saved in TinyMCE using AJAX in Laravel 4 without Laravel stripping all tags when getting input from the controller? For example, when sending post data through AJAX: var form = {id: 1, html: &apo ...

Choosing individual div elements to assign attributes using event listeners

I've developed a Tic-Tac-Toe game but now I'm trying to figure out how to add colors to the winning squares or draw a line. The CSS for creating the square or line is simple, but I'm unsure about selecting only the winning squares when my wi ...

Update the image source every 1 second with Jquery and Javascript

I've been experimenting with creating a script that dynamically changes the source of an image every two seconds based on a list. Essentially, my current approach involves using a for loop to iterate over the provided list: $(document).ready(functio ...

Minimizing the use of numerous nested callbacks during the animation of multiple objects

I am looking to implement an animation that changes the opacity of 3 images one after the other in a continuous loop. The code I currently have seems to work fine, but I'm wondering if there are any optimizations that can be made. Any suggestions or i ...

Remove the bottom border from the active tab by utilizing the <div> and <a> elements

I am facing an issue with a tabbed menu on my webpage. While the menu is functioning correctly, I am struggling to remove the bottom border from the active tab. You can view all of my test code here. While I have come across solutions using <ul> an ...

Guide on how to have two controllers execute identical tasks in Angular while modifying the appearance of the website

Trying to recreate Google's homepage functionality using Angular has been challenging for me. Despite watching Egghead videos and studying the API extensively, I couldn't find a specific example for this behavior. Here's what I aim to achiev ...

Discover the secret to automatically calling a function every minute when the page is loaded!

Our team is currently developing a PhoneGap application with 4 pages. We are in need of posting the current latitude and longitude every minute to our server. Fortunately, we are familiar with how to post values to the server as well as how to retrieve the ...

The Web Browser is organizing CSS elements in an alphabetized sequence

map.css({ 'zoom': zoom, 'left': map.width()/(2*zoom) - (point[0]/100)*map.width(), 'top': map.height()/(2*zoom) - (point[1]/100)*map.height() Upon observation, it appears that Chrome adjusts the map zoom first be ...

The issue with CSS z-index not functioning properly when using absolute positioning

Here is the issue I'm facing: I have multiple div containers in my code. .outer { position: relative; z-index: 1; } .inner { position: absolute; z-index: 999; } <div class="outer"> <div class="inner"> <div class="option ...

Optimal method for storing text descriptions across two platforms (react native and react)

Hello, I hope you are doing well. I have a question regarding two platforms that I am using - React Native and React (NextJS). Both platforms have forms to save data, one of which is a "text description." Here's the issue: When I input a long passag ...

Getting images using index in Selenium

When it comes to extracting images from HTML, we usually rely on XPath, CSS selectors, or unique IDs. However, there are cases where images lack any distinguishable attributes. The structure of the HTML is as follows: <div id="altImages" class="a-fixed ...

Comparing `height: calc(100vh);` to `height: 100vh;` in CSS can reveal a few

Currently tackling a project where the previous developer utilized: .main-sidebar { height: calc(100vh); } Unfortunately, I can no longer reach out to them, and I am curious about the variance (if any) between the two approaches. (Am I in the approp ...

Encountering issues while attempting to add vertical scroll to a div

Trying to add horizontal scrolling to a div that contains two separate divs, each with a width of approximately 4080px. One of the divs holds a table with headers, while the other div has <td> elements in <tr>. I need to enable vertical scrolli ...

Restricting a Collection of Dropdown Menus to Permit only a single Specific Value

I want to use jQuery to create a group of select boxes that function in such a way that only one can be selected as "yes". If the user selects "yes" in one box, all other boxes should automatically change to "no". Initially, I thought I had code that work ...

Unusual illumination on Mobile Safari

Help needed with CSS highlight issue Here is the CSS code causing the problem: html { -webkit-tap-highlight-color: rgba(0,0,0,0.7); -webkit-touch-callout: none; } This is my HTML snippet: <a href="javascript: location ...

Unable to obtain the identification number of the specified variable

I am working on a jquery event that is enclosed within $(document).ready(function() { //.....// });. The code for the event is as follows: $('.puzzle_cells').keyup(function(event) { }); My next step is to retrieve the ID of the <input> e ...

Is there a way to alter the color of options within a select tag when hovering the mouse over them

After searching through multiple websites, I couldn't find a solution on how to change the option background color on hover from blue to green. The screenshot provided below illustrates what I am requesting for in a clearer manner. https://i.sstatic. ...

Tips on refreshing a div using jQuery while maintaining the functionality of addEventListener

Hi, I am facing an issue with updating the "div" element. The refresh works fine, but after refreshing when I try to click on the updated "div", the addEventListener in my JavaScript code does not seem to work anymore. Can someone please explain why this i ...