Can the animation be looped using setInterval()?

Once the images finish sliding in the animation, they continue to slide right endlessly. I've attempted using a setTimeout function to move the images back left and restart the animation, but this causes the setInterval animation to stop. Is there a way to shift the #slidewindow left by 400% so that the slide can loop infinitely from the beginning?

Thank you for all your contributions!

HTML:

<div id="slide-box">
        <div id="slidewindow">
            <div id="Img1">
                <img class="imgs" src="http://www.publicdomainpictures.net/pictures/170000/velka/sunrise-in-the-mountains.jpg">
            </div>

            <div id="Img2">
                <img class="imgs" src="https://static.pexels.com/photos/402680/pexels-photo-402680.jpeg">
            </div>

            <div id="Img3">
                <img class="imgs" src="https://upload.wikimedia.org/wikipedia/commons/8/80/Winter_landscape_in_Mauricie%2C_Qu%C3%A9bec.jpg">
            </div>

            <div id="Img4">
                <img class="imgs" src="https://upload.wikimedia.org/wikipedia/commons/d/d0/BlenderCyclesLandscape.jpg">
            </div>

            <div id="Img5">
                <img class="imgs" src="https://static.pexels.com/photos/144244/monument-valley-lightning-storm-rain-144244.jpeg">
            </div>

        </div>
    </div>

CSS:

#slide-box{
    width: 80%;
    border: solid;
    border-color: white;
    border-width: 1rem;
    background-color: yellow;
    display: inline-block;
    overflow: hidden;

}

#slidewindow{
    float: left;
    width: 500%;
    margin: 0;
    position: relative;

}

.imgs{
    width: 20%;
    float: left;
    margin: 0 0 0 0;
}

JQUERY:

setInterval(function(){ 
        $('#slidewindow').animate({
            right:'+=100%',

            }, 1000);
    }, 2000);

Answer №1

By restoring the animated right property after the animation and moving the first image to the end of the list, you can create a seamless slideshow that keeps going:

setInterval(function(){
    $('#slidewindow').animate({
        right:'+=100%',
    }, 1000, function () { // Adding this callback function
        $('#slidewindow').append($('#slidewindow>div:first')).css({ right: 0 });
    });
}, 2000);
#slide-box{
    width: 80%;
    border: solid;
    border-color: white;
    border-width: 1rem;
    background-color: yellow;
    display: inline-block;
    overflow: hidden;

}

#slidewindow{
    float: left;
    width: 500%;
    margin: 0;
    position: relative;

}

.imgs{
    width: 20%;
    float: left;
    margin: 0 0 0 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slide-box">
        <div id="slidewindow">
            <div id="Img1">
                <img class="imgs" src="http://www.publicdomainpictures.net/pictures/170000/velka/sunrise-in-the-mountains.jpg">
            </div>

            <div id="Img2">
                <img class="imgs" src="https://static.pexels.com/photos/402680/pexels-photo-402680.jpeg">
            </div>

            <div id="Img3">
                <img class="imgs" src="https://upload.wikimedia.org/wikipedia/commons/8/80/Winter_landscape_in_Mauricie%2C_Qu%C3%A9bec.jpg">
            </div>

            <div id="Img4">
                <img class="imgs" src="https://upload.wikimedia.org/wikipedia/commons/d/d0/BlenderCyclesLandscape.jpg">
            </div>

            <div id="Img5">
                <img class="imgs" src="https://static.pexels.com/photos/144244/monument-valley-lightning-storm-rain-144244.jpeg">
            </div>

        </div>
    </div>

Answer №2

Here is a more versatile version that you can incorporate into your code:

var currentPosition = 0;
setInterval(function() {
  // Calculate the new position by adding the width of one image to the current position
  currentPosition = parseInt($('#slidewindow').css("right")) + $('.imgs').width();
  
  // Check if the new position has reached the end of the slide window
  if (currentPosition === $('#slidewindow').width()) {
    // If at the end, reset back to the beginning instantly
    //$('#slidewindow').css("right", "0px");
    
    // Alternatively, for a smoother transition back to the start, use the following JS code
    $('#slidewindow').animate({
      right: '0'
    }, 1000);
  } else {
    // Slide to the next position if not at the end yet
    $('#slidewindow').animate({
      right: '+=100%'
    }, 1000);
  }
  
  currentPosition += parseInt($('#slidewindow').css("right"));
}, 2000);
#slide-box {
  width: 80%;
  border: solid;
  border-color: white;
  border-width: 1rem;
  background-color: yellow;
  display: inline-block;
  overflow: hidden;
}

#slidewindow {
  float: left;
  width: 500%;
  margin: 0;
  position: relative;
}

.imgs {
  width: 20%;
  float: left;
  margin: 0 0 0 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slide-box">
  <div id="slidewindow">
    <div id="Img1">
      <img class="imgs" src="http://www.publicdomainpictures.net/pictures/170000/velka/sunrise-in-the-mountains.jpg">
    </div>

    <div id="Img2">
      <img class="imgs" src="https://static.pexels.com/photos/402680/pexels-photo-402680.jpeg">
    </div>

    <div id="Img3">
      <img class="imgs" src="https://upload.wikimedia.org/wikipedia/commons/8/80/Winter_landscape_in_Mauricie%2C_Qu%C3%A9bec.jpg">
    </div>

    <div id="Img4">
      <img class="imgs" src="https://upload.wikimedia.org/wikipedia/commons/d/d0/BlenderCyclesLandscape.jpg">
    </div>

    <div id="Img5">
      <img class="imgs" src="https://static.pexels.com/photos/144244/monument-valley-lightning-storm-rain-144244.jpeg">
    </div>

  </div>
</div>

Answer №3

One potential solution is to implement a global counter...

var counter = 0;
setInterval(function(){
    if(counter == 3)
    {
        $('#slidewindow').animate({
            right:'-=300%',
        }, 1000);
        counter = 0;
    }
    else
    {
        $('#slidewindow').animate({
            right:'+=100%',
        }, 1000);
        counter++;
    }
}, 2000);
#slide-box{
    width: 80%;
    border: solid;
    border-color: white;
    border-width: 1rem;
    background-color: yellow;
    display: inline-block;
    overflow: hidden;

}

#slidewindow{
    float: left;
    width: 500%;
    margin: 0;
    position: relative;

}

.imgs{
    width: 20%;
    float: left;
    margin: 0 0 0 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slide-box">
        <div id="slidewindow">
            <div id="Img1">
                <img class="imgs" src="http://www.publicdomainpictures.net/pictures/170000/velka/sunrise-in-the-mountains.jpg">
            </div>

            <div id="Img2">
                <img class="imgs" src="https://static.pexels.com/photos/402680/pexels-photo-402680.jpeg">
            </div>

            <div id="Img3">
                <img class="imgs" src="https://upload.wikimedia.org/wikipedia/commons/8/80/Winter_landscape_in_Mauricie%2C_Qu%C3%A9bec.jpg">
            </div>

            <div id="Img4">
                <img class="imgs" src="https://upload.wikimedia.org/wikipedia/commons/d/d0/BlenderCyclesLandscape.jpg">
            </div>

            <div id="Img5">
                <img class="imgs" src="https://static.pexels.com/photos/144244/monument-valley-lightning-storm-rain-144244.jpeg">
            </div>

        </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

Express Module Employs Promises for Returns

I have a JavaScript file for elasticsearch (could be any other database as well) that performs a simple query and uses a promise to return the data. I am using this module in my Express server (server.js) with the hope of retrieving the data, as I ultimat ...

React Animation Library With Smooth Initial Render and No Dependency on External Props

I'm currently implementing a Modal component within my app, utilizing Portals. My goal is for the Modal to smoothly fade in when it is rendered and fade out when it is no longer displayed. Upon examining the documentation for react-transition-group, ...

Using JavaScript to Toggle Visibility of Div Elements

Hi there! I'm having some trouble with a code that is supposed to show/hide div content based on the selection from a jump menu. Unfortunately, it's not working as expected. Here is the snippet of my code: JS: function toggleOther(chosen){ if ( ...

Navigating the jQuery DOM: traversing from past to future

Seeking assistance with DOM traversal techniques for a trivia quiz. The structure includes multiple inner divs with a repetitive question/answer format. <div class="trivia_entry"> <div class="pad50"> <span>question</span> ..radio b ...

How can one ensure the preservation of array values in React?

I'm struggling to mount a dynamic 'select' component in React due to an issue I encountered. Currently, I am using a 'for' loop to make API calls, but each iteration causes me to lose the previous values stored in the state. Is t ...

When waiting for proxy leads to access the 'then' property, what should be my next move?

In my code, I am using a special Proxy to imitate a virtual object. The getter of this proxy returns values that are already prepared. After some exploration, I found out that when the proxy is awaited, it triggers the 'then' property of the pro ...

Creating a topographical map from a 3D model

<!--B"H--> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Custom Terrain Heightmap Generator</title> ...

Please tap to dial: Access to navigation is restricted

Trying to add a click-to-call link with the following code: <a href="tel:+4912345678912">Tel: +4912345678912</a> Despite Google developers saying it should work, major mobile browsers are blocking the navigation when clicking on the link. It ...

Generate spacing between rows of content in HTML or CSS without affecting the spacing between lines of text

In my R Markdown document in R, I want to replace the header labeled "Preface" with grey lines for visual indication. Here's the code snippet I've tried: :::{#preface} <p> Text </p> ::: However, there seems to be no margin at the beg ...

What methods can be used to conceal a div when the content is not being shown?

I'm a beginner in HTML and CSS and I have a page with the following code: Content displayed : <div id="row-3" ><!-- Row 3 starts here --> <div class="groupz_column1" style="margin-right:0px;"><a href="http://www.xyz.in/ads/ ...

Exploring the possibilities of combining OpenCV with web technologies like Javascript

I am currently faced with the challenge of integrating OpenCV into a web application using Express.js. While I am familiar with the Python and C++ libraries for OpenCV, I now need to work with it in conjunction with Express.js. My main requirements include ...

How can I link a Vue.js webpage with a WordPress site?

Looking to integrate a Vue.js 2 single page into an existing WordPress website? Perhaps you've already got an old WordPress site and now want to develop a new Vue tool or page that can be added seamlessly to the menu. How can this be achieved within W ...

Fade effects on hover with a changing background color

Here is the CSS code to be used: #onec-fourth:hover { background-color: #F36DE1; color: white; } I am looking to keep the background color and text color effects for 1 second after I move my mouse off the object (#onec-fourth). Currently, when I mov ...

PHP is unable to decode JSON that has been converted from JavaScript

When I send an array as a POST request, I first convert it to JSON using the JSON.stringify() method. However, I encountered an issue when trying to decode it in PHP. // JavaScript var arr1 = ['a', 'b', 'c', 'd', & ...

In my attempt to assess the correlation between value 1 and a value in the preceding object, I am utilizing the *ngFor directive

Attempting to compare 2 entries in an *ngFor loop. The code should compare the value at the current object to a value at the previous object. <ng-container *ngFor="let item of s_1.comments[0]; index as b"> <article class="message i ...

Javascript/Webpack/React: encountering issues with refs in a particular library

I've encountered a peculiar issue that I've narrowed down to the simplest possible scenario. To provide concrete evidence, I have put together a reproducible repository which you can access here: https://github.com/bmeg/webpack-react-test Here&a ...

Tips for making WebDriver pause until Sencha AJAX request finishes

While testing a page with Selenium WebDriver, I encountered an issue related to the Sencha JavaScript library being used on the underlying page. The problem arises when I input a value into a field and an AJAX call is made to validate that field. If the va ...

Resource Jump.js could not be loaded

Although I am still new to NPM, I have mostly built websites without using it. Recently, I decided to implement smooth scroll using Jump.js. Initially, everything seemed to work well when I used the live server extension in VScode. However, once I uploade ...

Modify the database value linked to an <input> element each time its value is modified

I attempted to create something similar to the example provided here $(document).ready(function(){ $('input[type=text]').keyup(function(){ var c=0; var a=$(this).attr('name'); //a is string //if var a change.. ...

Python Selenium: Clicking a Button

Here is the HTML code I am currently working with: <button aria-label="Nur zuhören" aria-disabled="false" class= "jumbo--Z12Rgj4 buttonWrapper--x8uow audioBtn--1H6rCK"> I am trying to automate the clicking of the button using the following Python ...