Pause animated effects during scrolling

My goal is to add animations that trigger as the user scrolls, but I want them to be staggered so they occur one after the other in quick succession.

I attempted using a function that adds a class to trigger CSS animations and then used setTimeout to delay the next animation slightly. However, instead of each animation being delayed, they all fade in simultaneously...
JSFiddle

UPDATE: It seems like I need to handle this in JavaScript since I have multiple types of animations on one page and need to introduce a few milliseconds delay between adding the classes.

/**
 * Check if Animation is currently in view
 */
function anim_in_view() {
  var window_height = $(window).height();
  var window_top_position = $(window).scrollTop();
  var window_bottom_position = (window_top_position + window_height);
  var $animations = $('body .animate');

  if ($animations.length) {
    $.each($animations, function() {
      var $elm = $(this);
      var element_height = $elm.outerHeight();
      var element_top_position = $elm.offset().top + 50;
      var element_bottom_position = (element_top_position + element_height);

      setTimeout(function() {
        if ((element_bottom_position >= window_top_position) &&
          (element_top_position <= window_bottom_position)) {
          // $elm.delay( 2000 ).addClass( 'visible' );
          $elm.addClass('visible');
        }
      }, 1000);
    });
  }
}
$(window).on('load scroll resize', anim_in_view);
.flex {
  margin-top: 1000px;
  margin-bottom: 500px;
  display: -webkit-flex;
  display: flex;
}
.flex > div {
  width: 33.33%;
  height: 200px;
}
.red {
  background: #f00;
}
.green {
  background: #0f0;
}
.blue {
  background: #00f;
}
.animate-opacity {
  opacity: 0;
  -webkit-transition: opacity 1s ease-in-out;
  transition: opacity 2s ease-in-out;
}
.animate-opacity.visible {
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="fadeIns" class="flex">
  <div class="animate animate-opacity red"></div>
  <div class="animate animate-opacity green"></div>
  <div class="animate animate-opacity blue"></div>
</div>

Answer №1

To achieve the desired effect, you can utilize the transition-delay property:

.animate-opacity:nth-child(2) {
  transition-delay: 1s;
}
.animate-opacity:nth-child(3) {
  transition-delay: 2s;
}

// Function to determine if animation is within view
function checkAnimationInView() {
  var windowHeight = $(window).height();
  var windowTopPosition = $(window).scrollTop();
  var windowBottomPosition = (windowTopPosition + windowHeight);
  var $animations = $('body .animate');

  if ($animations.length) {
    $.each($animations, function() {
      var $element = $(this);
      var elementHeight = $element.outerHeight();
      var elementTopPosition = $element.offset().top + 50;
      var elementBottomPosition = (elementTopPosition + elementHeight);

      if ((elementBottomPosition >= windowTopPosition) &&
        (elementTopPosition <= windowBottomPosition)) {
        // $element.delay(2000).addClass('visible');
        $element.addClass('visible');
      }
    });
  }
}

$(window).on('load scroll resize', checkAnimationInView);
.flex {
  margin-top: 1000px;
  margin-bottom: 500px;
  display: -webkit-flex;
  display: flex;
}
.flex > div {
  width: 33.33%;
  height: 200px;
}
.red {
  background: #f00;
}
.green {
  background: #0f0;
}
.blue {
  background: #00f;
}
.animate-opacity {
  opacity: 0;
  -webkit-transition: opacity 1s ease-in-out;
  transition: opacity 2s ease-in-out;
}
.animate-opacity:nth-child(2) {
  transition-delay: 1s;
}
.animate-opacity:nth-child(3) {
  transition-delay: 2s;
}
.animate-opacity.visible {
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="fadeIns" class="flex">
  <div class="animate animate-opacity red"></div>
  <div class="animate animate-opacity green"></div>
  <div class="animate animate-opacity blue"></div>
</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 is the process behind the creation of the class or id fields in HTML for Gmail and Quora? How are these

When using Gmail: <tr class="zA yO" id=":1t4"> While on Quora: <span id="ld_zpQ1Cb_27965"> What is the purpose behind this and what specific tool do they employ to achieve it? ...

Guide on dividing and presenting ajax information into two separate div containers

I am attempting to showcase two sets of data on separate divs using ajax. Below is the code I am utilizing for this task. Here is the ajax implementation $(function () { $(".myBtn").click(function () { var id = $(this).data("id"); ...

Obtain the content enclosed within parentheses using JavaScript

const str = "(c) (d)"; I need to separate the given string into an array The result should be [0] => 'c' [1] => 'd' ...

Using JSON parsing to dynamically create classes with preloaded background images

Today, I successfully deployed my browser game using MVC4 to my website for the first time. I am currently navigating through the differences between running the site off of localhost and running it from the actual website. My process involves loading all ...

Utilize a fluid AJAX endpoint for the Vue Select2 encapsulated component

I have customized the Wrapper Component Example based on VueJS documentation by adding the AJAX data source feature. You can view my modified code here. My goal is to dynamically set the ajax url property of my select2 component, like this: <select2 :o ...

Delving into the World of CSS

I have been working on changing the background image using this JavaScript code, but I am not sure what to reference in the CSS file. Despite reading through everything, my screen still remains blank. $(function() { var body = $('body'); var bac ...

Eliminate the commas in the JSON string except for the commas that separate arrays

I am facing an issue with stringified JSON data that includes commas in the description fields which causes the AJAX post to fail when there are apostrophes or commas present. How can I modify the output of var test = JSON.stringify(data)? The current ...

Browse the string on the web browser

Is there a way for me to continuously read and capture a string as it is being written in the browser, retrieving its value every 5 seconds? I need to be able to monitor the changing value while it is being input. In PHP, I have the following code snippet ...

Updating the iFrame source using jQuery depending on the selection from a dropdown menu

I want to create a dynamic photosphere display within a div, where the source is determined by a selection from a drop-down menu. The select menu will provide options for different rooms that the user can view, and the div will contain an iframe to showca ...

The shown.bs.tab event is causing an issue where the previous active tab cannot be retrieved

Reference from https://getbootstrap.com/docs/4.0/components/navs/#events states that I am utilizing e.relatedTarget in the shown.bs.tab event to retrieve the previous active tab for applying some CSS styles. However, currently, e.relatedTarget is returning ...

Using Jquery AJAX to request data in either JSON or TEXT format

Seeking guidance on extracting the TEXT from the getsku javascript function after submission, but unsure of the exact method. 1) How can I fetch the POST data? 2) How can I retrieve and post multiple variables back, especially if they are of type text? 3 ...

Properly spaced website images

I'm trying to display my images side by side with a slight margin in between them. However, when I apply a `margin-right: 10px;` to each div, the last image doesn't line up properly with my title bar. Is there a way to create spacing between the ...

Using Javascript to Treat an HTML Document as a String

Check out the complete project on GitHub: https://github.com/sosophia10/TimeCapsule (refer to js/experience.js) I'm utilizing JQuery to develop "applications" for my website. I'm encountering a problem where I can't locate the correct synta ...

Symfony error message encountered when making an AJAX request with a "url not found" issue

Within the code below, I utilized jQuery to make a call to a URL and transmit values in JSON format. $(document).ready(function(){ $('input#receive_count').click(function(event){ var time = $('select#urgent_mail_start ...

What is the process of integrating jQuery into a Chrome application?

I am trying to incorporate jQuery methods into my Chrome app, but I'm uncertain about the process of including the API. As someone who is new to front-end development, I am essentially looking for something similar to .h #include, which I typically us ...

javascript increment variable malfunctioning

Below is the script I am working with: $(function() { var fileCount = {{$image_counter}}; $('#remove-file').click(function() { fileCount--; }); if (fileCount >= '5'){ $("#d ...

Identifying between a webview and browser on an Android device

I need to differentiate between a webview and a browser in Android to display a banner only for users accessing the app via a browser, not through a webview. I've tried using PHP code but it doesn't work as expected. Is there another method to a ...

Calculate the total number of randomly generated integers using jQuery

Below are examples of the inputs I have generated: <input type="text" id="rfq_ironilbundle_quotes_offer_price_0" name="rfq_ironilbundle_quotes[offer_price][0]" required="required" class="form-control offered-price"> <input type="text" id="rfq_iro ...

Troubleshooting: AJAX takes precedence over PHP for log in issues

I've been working on creating a login form using html > ajax > php, but I've run into an issue with the php code not working properly. I'm not sure where the problem lies, but I suspect that the ajax is unable to execute my php file. A ...

Can the layout created with JqueryMobile be used on both web and mobile platforms?

I develop websites using php for the backend and HTML/CSS for the frontend. Currently, the need to make my website mobile-friendly has arisen. I am contemplating utilizing JqueryMobile but I'm uncertain if the same layout will work for both desktop an ...