Is there a way to animate two divs with identical classes to glide across the screen together?

I am looking to create a Javascript animation where certain divs move horizontally across the screen, similar to examples on this website and here. The HTML code I have is:

<div id = "creators" class = "big-part">
    <h3>Creators</h3>
    <div class = "creator_name">
        <h4>Name</h4>
        <p>Text</p>
    </div>
    <div class = "creator_name">
        <h4>Name</h4>
        <p>Text</p>
    </div>
    <div class = "creator_name">
        <h4>Name</h4>
        <p>Text</p> 
    </div>
</div>

My CSS for this section includes styling for the div elements with class "big-part" and "creator_name". Here's how it looks:

.big-part {
    color: rgb(230, 230, 230);
    background-color: #3c3c91;
    border-radius: 21px;
    padding: 0.5% 1.5%; 
    margin-top: 2%;
}
.creator_name {
    background-color: rgb(230, 230, 230);
    height: 300px;
    color: #3c3c91;
    border-radius: 15px;
    padding: 1%;
}
.creator_name {
    width: 25%;
    display: inline-block;
    margin: 2% 2%;
}

For the Javascript part, I have tried to make these divs move using the following script:

var creatorEl= document.getElementsByClassName("creator_name");

var startTime = new Date().getTime();
var moveTheDiv = function() {
    var currTime = new Date().getTime();
    var secondsElapsed = ((currTime - startTime)/1000);
    var newLeft = parseFloat(secondsElapsed) * 30 + "px";
    creatorEl.style.left = newLeft;
    window.requestAnimationFrame(moveTheDiv);
};
moveTheDiv();

I would like this animation to occur only when the user scrolls down and the div is visible on the screen. Any guidance on achieving this requirement would be appreciated.

EDIT: I have noticed that I can make the movement happen by adding an ID to each div element and moving them individually with the IDs. Is there a way to make them both move simultaneously using just the class attribute?

Answer №1

If you're looking for a powerful animation engine, consider checking out Velocity.js by Julian. You can learn more about it here: (I have no affiliation with the creator).

With Velocity.js, you can create animations similar to jQuery's .animate() method.

For example, here is how you can make a div spin:

$(document).ready(function(){   
   $('#divToAnimate').velocity(
    { rotateX: "+=0", 
      rotateY: "+=360",
      height: '+=50px',
      width: '+=100px'
    }, 
    {
      duration:4000,
      loop:false,
      easing:'linear'
  });
});
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Sample animation with Velocity.js</title>

  <!-- jQuery CDN -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
  </script>

  <!-- Velocity.js CDN -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.3/velocity.min.js"></script>
</head>

<body>
  <div id="divToAnimate">Example Div to Animate</div>
</body>

</html>

Hope this information proves useful and best of luck with your animations!

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

Switching between vertical and horizontal div layouts while reorganizing text fields within the top div

function toggleDivs() { var container = document.querySelector(".container"); var top = document.querySelector(".top"); var bottom = document.querySelector(".bottom"); if (container.style.flexDirection === "column") { container.style.flexDirec ...

Leveraging hapi-auth-basic

I attempted to incorporate a basic authentication strategy following a tutorial I stumbled upon. Here is the setup of my server.js file. 'use strict'; const Hapi=require('hapi'); const sequelize = require('sequelize'); c ...

Designing a custom CSS Stylesheet to enhance the look and feel of various pages on a

I am currently in the process of creating a website with only two main pages - an "Index" page and an "About Me" page. However, I have run into a dilemma when it comes to editing the CSS style on the About Page. Both my About and Index pages have adopted ...

Center align items of the Bootstrap Navbar

I've been trying to design a navbar using Bootstrap, with the 'navbar-brand' on the left, centering the navbar items in the middle, and having two items on the right. Initially, I attempted to create it from scratch but struggled to make it ...

Having trouble releasing the package on npm platform

I'm facing difficulties in publishing a public package to npm. Despite creating an organization, I'm unable to figure out how to add a new package. Any assistance on this matter would be greatly appreciated. https://i.sstatic.net/RMKU9.png ...

Having issues with Exit Property functionality in Framer Motion when using react js

Help Needed: Framer Motion Exit Property Not Working I've been trying to get the exit motion to work on Framer Motion with React JS for over a week now, but everything else seems to be functioning correctly. I have installed the latest version of Fra ...

Choose all checkboxes that have a certain identifier

Currently, I am developing a menu permission project using vue.js. Within this project, I have various submenus that are children of different menus. My objective is to automatically select all submenus under a selected menu when the user clicks on "select ...

Please indicate the generator title (for example, nx generate @nrwl/workspace:library) specifically for Angular

Currently, I am expanding my knowledge in web development. While working with Angular, I encountered an issue when trying to create a new component. The error message reads "Specify the generator name (e.g., nx generate @nrwl/workspace:library)" after exec ...

Issue with updating robot's direction using string in Javascript not resolving

Javascript Developing a simple game where a robot moves on a 5x5 board based on user input commands 'L' (move left), 'R' (move right), and 'F' (move forward) from a starting position while facing North (N). Clicking the Move ...

What is the best way to call a JavaScript function with multiple arguments from a Silverlight project?

I encountered an issue when trying to invoke a JavaScript function with multiple arguments from an HTML page. Here is what I am attempting to do: wbNavigator.Navigate(new Uri("http://localhost:56433/route.html", UriKind.Absolute)); object results = wbNavi ...

Is there a way to transform a local array into remote JSON data?

I am attempting to retrieve an array from a remote server that is connected to a dynamic database. From what I have gathered on Ionic forums, it seems that I need to utilize the $http function from AngularJS. However, since I am new to AngularJS, the curr ...

Submitting an intricate item to a WCF REST API using JQuery

Currently, I am attempting to send a complex object to my WCF REST service. Utilizing this method seems to be the most straightforward way to upload a Stream type object along with other parameters to an endpoint simultaneously. Service: [OperationContra ...

What does it mean when my JQuery selector returns a n.fn.init[0]? Can someone explain this to me?

I am facing an issue with a group of dynamically generated checkboxes, each containing a data-id attribute that corresponds to a specific integer id in the database. When I populate my HTML form with an object for editing, there is a list of integers indic ...

Using PhantomJS webdriver in Python Selenium to disable CSS styling

There seems to be some uncertainty surrounding the ability to disable CSS while utilizing the PhantomJS webdriver for Selenium. It has been established that this is achievable with FireFox by adjusting the FireFox profile, but I am interested in doing so w ...

Encountering a problem with the JSON POST request in my Django application

Trying to access a JSON object sent from JS-Jquery using $.post to a Django script has been quite challenging. I've experimented with different approaches found on Stackoverflow, but haven't been able to get it working: On the .js side: $("#su ...

Modify the menu list by adding or removing classes

My current setup includes a menu layout that looks like this: <ul id="mainmenu" class="nav menu sf-menu responsive-menu superfish"> <li class="active"> <a href="index.php">Home</a> </li> <li class=""> ...

Issue with sending an ajax post request using Jquery

The jquery ajax request below is not working as expected: $(document).ready(function(){ var friendrequest = $(".friendrequest").val(); $(".afriendreq").click(function(){ $(".afriendreq").hide(); ...

Verify the validation of the text box

Checking a textbox control for validation is necessary. Validation Criteria: Value should be between 0 and 1000, with up to 2 decimal places (e.g. 1.00, 85.23, 1000.00). Once 2 decimal points are used, users should not be able to enter additional ze ...

What's the best way to ensure two <div> tags are perfectly vertically aligned across all screen resolutions?

Currently, I am in the process of revamping the login form for the upcoming Blackboard update. The new theme being introduced is not compatible with our current page layout, so adjustments need to be made. While I am not an expert in design or HTML/CSS, I ...

What is the best way to smoothly insert an item into a nested object within the state of a React component

Trying to create a complex array using react hook: const [strategy, setStrategy] = useState([leg]); Here's how the `leg` object is defined: const leg = { entry: { conditions: [], actions: [""], }, exit: { conditions: [], ...