Having trouble with jQuery's recursive animation functionality

I have developed a function to scroll multiple images horizontally in the header of my website. Inside my header, I have implemented code that looks like this:

<div class='images'> <!-- this div is 150% wide with overflow hidden -->
     <img src=... /><img src=... /><img src=... /><img src=... />
</div>

This functionality is intended to operate as follows:

  1. Take the first image, utilize animation to scroll it out of view to the left (by adjusting its margin-left property).
  2. Move the first image to the end (effectively shifting it and adding a new image at the beginning).
  3. Repeat the entire process.

The function I coded for this purpose is as follows:

function scroll()
{
    var o = jQuery('.images').children().first();
    var w = o.css('width');
    var s = o.attr('src');
    jQuery('.images:first-child').animate({'margin-left': "-"+w}, {'duration': 1000, 'complete': function(){
        jQuery('.images').children().first().remove();
        jQuery('.images').append("<img src='"+s+"' />");
        scroll();}});
}

Currently, the function operates in the following manner: The first image animates correctly, but subsequent images do not scroll (the animation does not work). However, after 1000 ms, the first image moves to the end. So while the images are changing, the animations are not functioning as intended.

I suspect that the scroll() function is being called too soon, but I am unsure how to sequence it properly. Any suggestions on how to chain it appropriately?

Answer №1

It seems like the solution you are looking for is this:

JSFiddle

The only thing you forgot was to specify the images themselves.

 jQuery('.images img:first-child')

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 steps are necessary to activate javascript in HTML for WebView?

I recently discovered that when my HTML/JavaScript site is visited via an Android webview, JavaScript is disabled by default. This causes a pricing list on my page to not display properly because it requires a JavaScript class to be added for it to open. I ...

Issues with audio and video playback intermittently occurring on Sencha Touch 2

As a beginner with Sencha Touch 2, I am currently working on uploading images onto an iPad using Xcode. My app has audio playing on the home screen, and I want the video to start playing and the audio to stop when a specific tab is selected. However, the ...

"Utilizing the 8-column layout feature in Twitter Bootstrap

Is it possible to set up 8 equal columns using the latest version of Twitter bootstrap? I know how to create 4 equal columns but I am struggling with figuring out how to achieve 8: <div class="col-sm-12"> <div class="row"> ...

Struggling with getting the Encore Webpack Bootstrap4.5 navbar to function properly

I am currently diving into the world of Symfony, and I have chosen to leverage bootstrap for styling my page. Interestingly, when I include the link, <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_ema ...

Tips for perfectly aligning heading both horizontally and vertically

https://i.sstatic.net/8ttSh.png https://i.sstatic.net/vSsH5.png My website has a header with text that I want to center both horizontally and vertically. I am currently using the 'text-center' class in Bootstrap 4, which centers the text but onl ...

Ajax call for Laravel resource does not support the use of PUT method

In the routes.php file, I have defined the following resource: Route::resource('items', 'ItemsController', ['before' => 'admin_access']); I am attempting to access the ItemsContoller@update method via AJAX but k ...

Lag in jQuery Animate Property

Hello fellow developers! I am currently working on a promotional website where both the homepage and subpages are contained within the same index file. The transition between the content occurs when users click on the menu items. There are a total of 4 su ...

How can I assign an ID to retrieve the property of <?php $row->name;?> using AJAX?

As a newcomer to Code Igniter, I am working on implementing an edit button in front of each row. My goal is to have a pop-up or modal box appear without refreshing the page after clicking the edit button, displaying all the fields within that box. < ...

What is the best way to dynamically insert an image into an <li> element?

I am looking to enhance my menu by adding an image below each li item as a separator. How can I achieve this using CSS? I want the images to automatically appear whenever a new li tag is added to the ul. Here is the HTML code: <ul class="menu"> ...

Leveraging the power of Ajax to dynamically submit a single form nested within a forEach iteration

I'm currently working on a database query that retrieves all user posts. This query is then used in a forEach loop to generate a post for each result: for($indexPost=0; $indexPost < $postCount; $indexPost++) { //Code for handling the post echo&l ...

As you hover over the div, the content inside subtly shifts upwards and downwards

I am facing an issue with my movie list. When hovering over a movie, a border appears around it as expected. However, I also notice that at the same time, it seems like extra padding is being added to the 'top' and 'bottom', causing the ...

Using Jquery Simplyscroll to incorporate images on both the left and right side within a container or clip

I am currently utilizing Jquery Simplyscroll (http://logicbox.net/jquery/simplyscroll/) and I am seeking a way to incorporate a faded png image on both the left and right sides to prevent my images from appearing "cut off" while sliding through. Below is ...

Trigger the meteor template to render manually upon any changes in the database

Currently, I am using a meteorjs template to display some records. What I am trying to achieve is that whenever there is a change in the database, I would like to trigger the template render method manually. Is it possible to reload just a single template ...

Finding elements through their text representation

I am struggling to select an element using the text "SELECT PRODUCT" in the following HTML: <div style="font-family: franklin-gothic-urw; font-weight: 400; font-style: normal; font-size: 19px; opacity: 1; color: rgb(255, 255, 255);">SELECT&nbsp; ...

The design breaks occur exclusively in the Android default browser

I am experiencing issues with the design of a nested div element when viewed in the default Android browser. Unfortunately, I don't have access to tools that would allow me to identify the cause of this problem. However, the design functions correctly ...

Basic jQuery fetch request

Having some trouble implementing an onclick button to send a get request to a php file. The jQuery is loading correctly, so that's not the issue. Check out the code below: The function that needs to be called <script type='text/javascript&a ...

Retrieve data attributes to obtain details about the slider before and after

My task is to create a slider with information about the previous and next slides. For example, in the slider, there are left < and right > arrows. Behind these arrows, there are circles. When you hover over any arrow to change the next or previous ...

When making an Ajax request to a controller, rather than simply returning JSON data, it redirects you to a

Utilizing jQuery and MVC 5, I have implemented a modal form in my project. Upon clicking 'ok' on the modal box, the javascript function should trigger an ajax call to the "Companies/Createa" controller. The code successfully executes and update ...

Struggling to make jQuery focus on duplicated input fields

I am working on a page that requires creating dynamic form fields based on user input. I am using Ajax to speed up form entry and prevent typos by connecting to my database. The issue arises when dealing with cloned form fields. They do not trigger the po ...

Encountering an Uncaught TypeError: Attempting to call a controller function using AJAX in Magento results in undefined not being a function

I've developed a custom module in the admin panel called createadmincontroller. It's configured in the config.xml file. I'm trying to call the controller index function from a .phtml file using Ajax, but I keep getting an error that says "Un ...