Move divs that are currently not visible on the screen to a new position using CSS animations

Upon entering the site, I would like certain divs to animate from an offscreen position. I came across this code snippet:

$( document ).ready(function() {
    $('.box-wrapper').each(function(index, element) {
        setTimeout(function(){
            element.classList.remove('loading');
        }, index * 500);
    });    
});
body {
    overflow-x: hidden;
}
.box-wrapper {
    -webkit-transition-duration: 600ms;
    transition-duration: 600ms;
}
.box-wrapper.loading:nth-child(odd) {
    transform: translate(100%);
    -webkit-transform: translate(100%);    
}
.box-wrapper.loading:nth-child(even) {
    transform: translate(-100%);        
    -webkit-transform: translate(-100%);
}
.box-wrapper.loading:nth-child(?) {
    transform: translate(-100%);        
    -webkit-transform: translate(-100%);
}
   .box-wrapper:nth-child(odd) #box1 {
}
.box-wrapper:nth-child(even) #box2 {
}
.box-wrapper:nth-child(?) #box3 {
}
#box1 {
    position: relative;
    display: block;
    margin: auto;
    width: 100px;
    height: 100px;
    background-color: aqua;
}
#box2 {
    position: relative;
    display: block;
    left:200px;
    width: 200px;
    height: 150px;
    background-color: aqua;
}
#box3 {
    position: relative;
    display: block;
    left:400px;
    width: 600px;
    height: 150px;
    background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div class="box-wrapper loading"><div id="box1"></div></div>
<div class="box-wrapper loading"><div id="box2"></div></div>
<div class="box-wrapper loading"><div id="box3"></div></div>

I have successfully implemented the animation, but I am struggling to apply different animations to each box. Whenever I try to replace the "?" with "box3" or something similar, it does not work. The animation needs to be "even" or "odd," but I desire a unique animation for each box.

Any suggestions or assistance on this matter would be greatly appreciated. Thank you!

Answer №1

Try using this code snippet to achieve the desired effect.

$(document).ready(function () {
    var animationStyles = ["bounce", "fade"];

    $('.element-wrapper').each(function (index, element) {
        setTimeout(function () {
            var randomStyle = animationStyles[Math.floor(Math.random() * animationStyles.length)];
            element.classList.remove(randomStyle);
        }, index * 500);
    });
});

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

Tips for storing the state using Localstorage?

Greetings to the person reading this message! I am relatively new to programming and I have a query... I created a Navigation bar: body > div > nav > div > ul > li*10 I have implemented the styling in CSS. //Navigation Bar const list = ...

"Using only JavaScript to target and manipulate child elements within the

I'm currently transitioning from using jQuery to pure JavaScript, and I've just started but am encountering difficulties. Specifically, I am unsure how to select child elements in the DOM. <div class="row-button"> <input type="submi ...

"Exploring the insertion of a line break within the modal body of an Angular application

Is it possible to create a modal for error messages with multilined body messages without altering the modal template? Any assistance would be greatly appreciated. <div class="modal-body" > <p *ngIf="!_isTranslatable">{{_modalBody}}</p&g ...

Exploring the functionality of Material-UI's TextField component through role-based testing with React

I currently have some components that contain mui TextFields, and there are two specific scenarios for these components: One TextField is meant for LicenseCode and does not require a label. Additionally, there are several TextFields generated using t ...

Change the value of input on onClick event in React

Although this may seem simple, it is a little confusing to me. I'm having trouble understanding why the input value in the following case is not being reset. handleReset() { this.setState({ data: "" }); } <button onClick={this.handleReset}>R ...

React.js: Passing an array as a property to an element results in the transformation of the array into an object

I'm trying to understand why passing an array as a prop to another element results in it getting transformed into an object with the array as its value. I need help understanding if this is a common JavaScript 'quirk' or specific to React an ...

Create a search feature based on names utilizing Node Express in conjunction with SQL database

After deciding to create an API with a search feature using SQL queries in node express, this is how I structured my code: app.get('/search/:query', (req, res) => { pool.getConnection((err, connection) => { if(err) throw err ...

Tips for updating state and rendering in a function component in React Native:

I am attempting to update the state before rendering the component in a function component. While I have found suggestions to use the useEffect hook for this purpose, I am finding the information on the React official site somewhat perplexing. The docume ...

Switching the ASP.NET XHTML Display Mode

I have been given HTML code from a designer to create a master page. The doctype in the HTML is set to HTML 4.0 Strict. The meta tags in the HTML do not contain closing end tags (they end with > instead of />) and the HTML is compliant when checked ...

What is the best approach for organizing JavaScript/CoffeeScript in a Rails 5 project for optimal efficiency?

I am currently working on a web application with Rails 5.0.2 and I have a set of JS files for the project: https://i.stack.imgur.com/WYB23.png Each of my own JS files follows a similar pattern, like this: $(function () { var init = function () { ...

Tips for changing the state of a toggle button with JavaScript

I need help creating a toggle button. To see the code I'm working on, click here. In my JavaScript code, I am reading the value of a 'checkbox'. If the value is true, I add another div with a close button. When the close button is clicked, ...

PHP JSON array conversion

[ { "id":"26", "latitude":"10.308749502342007", "longitude":"123.88429984649656" }, { "id":"28", "latitude":"10.313816172726275", "longitude":"123.89030799468992" } ] I have retrieved this JS ...

Angular 2.0 in conjunction with D3.js does not style SVG elements using CSS

Currently, I am utilizing Angular 2.0 TypeScript along with the d3.js library to create graphics. My main focus is on applying CSS to the axis (especially shape-rendering: crispEdges;). Do you have any suggestions? Check out the code below: var vis = d3 ...

Unable to locate the name 'Cheerio' in the @types/enzyme/index.d.t file

When I try to run my Node application, I encounter the following error: C:/Me/MyApp/node_modules/@types/enzyme/index.d.ts (351,15): Cannot find name 'Cheerio'. I found a suggestion in a forum that recommends using cheerio instead of Cheerio. H ...

After the page reload, the text box remains filled and does not reset to null

Even after refreshing the page, the text box does not reset to null. It retains the value that was entered previously. This issue occurs while implementing pagination. <form method='post' class="search" action="<?= base_url() ?>my-subsc ...

Tips for emptying form fields when sliding up in jQuery

I have created a user-friendly form for booking hotel accommodations. Users can easily select the number of adults and children from dropdown menus. Additionally, the form dynamically generates dropdowns for specifying the ages of children once selected. ...

Specify padding or margin for all sides in a single line of XML code

Is there a way to set the padding or margin for all four sides in xml with just one line, similar to how we can do it in css? For instance, in css: padding: 1px 2px 3px 4px; Is there an equivalent method in xml for accomplishing this? ...

Comparing jquery ajax jsonp with angularjs $http json: A breakdown of two powerful ways

I have a scenario where I need to switch from using jquery to angularjs for a particular feature. Specifically, I am replacing jquery's ajax method with angularjs' $http method. This piece of code is responsible for enabling users to sign up for ...

What is the best way to display two radio buttons side by side in HTML?

Looking at my HTML form in this JSFiddle link, you'll see that when the PROCESS button is clicked, a form with two radio buttons appears. Currently, they are displayed vertically, with the female radio button appearing below the male radio button. I& ...

Module Ionic not found

When I attempt to run the command "ionic info", an error is displayed: [ERROR] Error loading @ionic/react package.json: Error: Cannot find module '@ionic/react/package' Below is the output of my ionic info: C:\Users\MyPC>ionic i ...