Exploring various animation techniques through JavaScript

I have implemented a cool animation effect using JavaScript scroll from the following code: https://github.com/daneden/animate.css

In my JavaScript code, I am adding a class in the last line like this:

$(this).addClass('fadeInLeft');

While this is great, sometimes I wish to change the fadeInLeft effect to something else (like slideInleft or flipInLeft). How can I add different effects in that line for different divs?

This is my current JavaScript code:

$(document).ready(function() {
  // Check if element is scrolled into view
  function isScrolledIntoView(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
  }
  // If element is scrolled into view, fade it in
  $(window).scroll(function() {
    $('.scroll-animations .animated').each(function() {
      if (isScrolledIntoView(this) === true) {
        $(this).addClass('fadeInLeft');
      }
    });
  });
});

Answer №1

To specify the type of animation, you can add a custom attribute to your element.

$(window).scroll(function() {
  $('.scroll-animations .animated').each(function() {
    if (isScrolledIntoView(this) === true) {
      $(this).addClass($(this).data("animationType"));
    }
  });
});
<div class="scroll-animations">
  <div class="animated" data-animation-type="fadeInLeft"></div>
  <div class="animated" data-animation-type="flipInLeft"></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 best method for retaining the complete YQL outcome within a JavaScript object?

I am trying to store the output of my YQL Query in a JavaScript object Here is my query: SELECT * FROM html WHERE url="http://myurl.com" and xpath="/html/body/center/table[1]/tr" Can someone guide me on how to proceed? I have gone through the YQL docu ...

Guide the user to redirect and guarantee a complete page load or reload every time

Suppose I navigate to the page http://mywebsite.com/contact and execute the following JavaScript code: window.location.assign('http://mywebsite.com/contact#foo'); The page remains static without refreshing, which is intentional. However, I aim ...

Populate a div using an HTML file with the help of AJAX

In my main HTML file, there is a div element: <div id="content"></div> Additionally, I have two other HTML files: login.html and signup.html. Furthermore, there is a navigation bar located at the top of the page. I am curious to know if it i ...

The dynamically generated button is visible only once

I'm currently working on creating a status update box similar to Facebook using Javascript/jQuery. However, I've run into an issue where the button I've appended inside the div element only appears once after clicking the post button. Below ...

assigned to a variable and accessed in a different route

Why does the "res.username" variable return as undefined in the second route even though my user needs to login before accessing any route? router.post('/login', passport.authenticate('local'), function(req, res) { res.username = r ...

Using Node.js to Insert Data into MySQL

I recently started experimenting with node.js and decided to use node-mysql for managing connections. Even though I am following the basic instructions from the github page, I am unable to establish a simple connection successfully. var mysql = require(& ...

Solutions for organizing backend data arrays by date and month

I'm currently in the process of developing a MERN-based animal shelter web app, and I'm facing challenges with grouping data by date month. Here's the schema I am using for the rescued date: const animalSchema = new mongoose.Schema({ date ...

Sending loop data as a parameter from JavaScript to AJAX through a Bootstrap modal

Below is my code that retrieves records from a loop using JavaScript. It seems to be working well, however, there is an issue with the looped buttons that are supposed to alert the users_id in a Bootstrap Modal popup. The problem I am facing is that when ...

"Trigger a click event on a DOM element whenever the component's props

I created a component with a click event in the componentDidMount lifecycle method. Each time the component is rendered, I want the div to be clickable. The issue I am facing is that the componentDidMount method runs only once, and the click event does n ...

What methods are available to display Excel files in a web browser without any cost? (Specifically looking for solutions involving JavaScript and Python within a Django

When you click on the file, it automatically downloads. However, I would prefer it to open in a new tab, similar to how SharePoint works. Unfortunately, I cannot use SharePoint due to security concerns, as it would require access to all company files. htt ...

The issue with ajax in CodeIgniter is that it keeps displaying a false message, even though the value is present

Trying to validate the existence of a value in the CodeIgniter website's database using AJAX. Below is the code snippet: <input id="username" name="pincode" type="text" class="form-control" placeholder="Enter Pincode"> <input id="prodid" n ...

What's the Catch with Vue's Named Slots?

When working with Named Slots in Vue (using the older, more verbose API for component slots), if there is a reusable component with a template structured like this: <template> <div v-for="field in formFields"> <slot name="`${field.key}_P ...

JSproblem with rendering in React

As a novice coder immersing myself in ReactJS with webpack, I've embarked on a self-paced course on Udemy. However, I'm facing a roadblock as my code doesn't seem to work despite mirroring the tutorial videos closely. The crux of the matter ...

What is the proper way to generate relative paths in JavaScript within an MVC3 framework?

Struggling with aligning paths in JavaScript without hardcoding. Currently using an asp.net MVC3 web application. If the path looks like this: var url = 'http://serverNameHardcode/websiteNameHardcode/service/service?param1=' + param; Everythin ...

Using both withNextIntl and withPlaiceholder simultaneously in a NextJS project causes compatibility issues

I recently upgraded to NextJS 14 and encountered an issue when deploying my project on Vercel. The next.config.mjs file in which I wrapped my nextConfig in two plugins seemed to prevent the build from completing successfully. As a workaround, I decided t ...

The operation to assign a value to property 'two' cannot be completed as it is currently undefined

I'm facing an issue with the code below and cannot figure out why I am encountering the error message. I have ensured that each object contains a value, so why is there a reference to 'undefined'? Cannot set property 'two' of unde ...

Is there a way to retrieve all "a" tags with an "href" attribute that contains the term "youtube"?

My goal is to capture all a tags that have the href attribute containing the word youtube. This task requires the use of jquery. ...

PHP Function Not Defined Upon Click

While using a while loop to generate a data table, I am encountering an issue with passing information from an onclick event to a function. The function is being called properly but the info from the onclick event is not being received. HTML: echo "<t ...

I need assistance improving my poor JavaScript patterns and practices. Where should I turn for help?

After focusing primarily on back-end tasks for the past few years, it's become apparent to me that JavaScript (and CoffeeScript) projects have evolved significantly in my absence. Working mainly in a rails environment, my previous JavaScript/jQuery i ...

evaluation of three variables using short-circuit logic

I was quite surprised by the outcome of the code I wrote. It seemed like it wouldn't work because it was checking if something is not running and the ID matches a specific one, then executing the code regardless of the break size limit: if(!isRunning ...