Move the divs within the overflow container by sliding them, then take out the initial element and append it to the end

Currently, when I utilize .appendTo(".wrapper") as shown in the code below, it eliminates the animation effect. My goal is to have the div on the far left slide out of view, triggering an overflow hidden effect, and then be placed at the end of the slide creating a continuous slideshow effect by repeating the previous divs.

   $(document).ready( function(){
        var x = 0;
        $(".next").on("click", function(){
            x -= 245;
            console.log($(".element").length)
            $(".element").each(function(i, e){
                console.log(i)
                $(this).eq(i).css({"marginLeft" : x +"px"}).appendTo(".wrapper");
            })
        })
    });

CSS:

    .mgcont{
        margin:5% auto;
        width:970px;
        overflow: hidden;
        border: 2px solid gray;
    }
    .wrapper{
        /*overflow: hidden;*/
        white-space: nowrap;
        width:960px;
    }
    .element{
        width: 240px;
        height: 300px;
        background: tomato;
        display: inline-block;
        margin-left: 10px;
        transition: margin 3s;
    }
    .prev{
        float: right;
    }

HTML:

<div class="mgcont">
    <button class="next">next</button>
    <button class="prev">PREV</button>
    <div class="wrapper">
        <div class="element">1</div>
        <div class="element">2</div>
        <div class="element">3</div>
        <div class="element">4</div>
        <div class="element">5</div>
        <div class="element">6</div>
    </div>
</div>

FIDDLE

Answer №1

To make adjustments to your JavaScript code, follow these steps:

        $(document).ready( function(){
        $(".next").on("click", function(){
            console.log($(".element").length)
            $(".element").each(function(i, e){
                console.log(i)
            })
                $(".element").eq(0).css({"margin-left" : "-245px"}).delay(3000).queue(function(next){
                    $(this).appendTo(".wrapper").css({"margin-left":"10px"});
$( this ).dequeue();
});
        });
    });

Don't forget to update the CSS for the transition property as well:

.element{
        width: 240px;
        height: 300px;
        background: tomato;
        display: inline-block;
        margin-left: 10px;
        transition: margin-left 3s;
    }

These changes should work flawlessly for your project.

Check out the updated Fiddle here

If you prefer using a separate CSS class, implement these modifications:

$(document).ready( function(){
        $(".next").on("click", function(){
            console.log($(".element").length)
            $(".element").each(function(i, e){
                console.log(i)
            })
                $(".element").eq(0).addClass('translateleft').delay(3000).queue(function(next){$(this).appendTo(".wrapper").removeClass('translateleft');
$( this ).dequeue();
});
        });
    });

In your CSS file, define styles for the translateleft class like this:

   .translateleft{
    animation: translateleft 3s;
    -webkit-animation:translateleft 3s;
    -moz-animation:translateleft 3s;
    -o-animation:translateleft 3s;
}
@keyframes translateleft{
    from{margin-left:0px;}
    to{margin-left:-245px;}
}
@-webkit-keyframes translateleft{
    from{margin-left:0px;}
    to{margin-left:-245px;}
}
@-moz-keyframes translateleft{
    from{margin-left:0px;}
    to{margin-left:-245px;}
}
@-o-keyframes translateleft{
    from{margin-left:0px;}
    to{margin-left:-245px;}
}

For a demonstration, see the working example in this Fiddle link

UPDATE The .dequeue() statement was omitted from the function. The answer has been revised and now functions correctly for repetitions.

Answer №2

I have made changes to all the files. You can test the code snippets below:

   $(function(){
  var width = $('.element:first').width();   
$(".next").on("click", function(){
    var neww = $(".element:first").clone();
    $('.element').eq(1).css({'margin-left':'-'+width+'px'});
     $(".element:first").remove();
    neww.appendTo('.wrapper');        
   $('.element:last').css({'margin-left':'5px'});
});
$(".prev").on("click", function(){
    var neww = $('.element:last').clone();
    $(".element:last").remove();
     $('.element:first').css({'margin-left':'5px'});
    neww.prependTo('.wrapper');
    $('.element:first').css({'margin-left':'-'+width+'px'});
});
 });
   .mgcont{
    margin:5% auto;
    width:970px;
    overflow: hidden;
    border: 2px solid gray;
}
.wrapper{
    /*overflow: hidden;*/

    white-space: nowrap;
    width:960px;

}
.element:first-child{
    margin-left: -240px;
}
.element{
    width: 240px;
    height: 300px;
    background: tomato;
    display: inline-block;
    margin-left: 5px;
    transition: all 3s;
}
.prev{
    float: right;
}
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
   <div class="mgcont">
<button class="next">next</button>
<button class="prev">PREV</button>
<div class="wrapper">
  <div class="element">6</div>
  <div class="element">1</div>
  <div class="element">2</div>
  <div class="element">3</div>
  <div class="element">4</div>
  <div class="element">5</div>      
</div>
  </div>

Feel free to give it a try and let me know if it meets your expectations.

Check out the DEMO for a working example.

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

When trying to run a jQuery function on click or load events, an error message stating that

When I have an .on(click) event triggering an ajax call, I want the same actions to occur when the page loads as well. My idea is to create a function that contains everything within the .on(click) event and trigger this function on page load. Although I ...

The ng-repeat in the inner loop is excluding the initial element of my array and subsequently placing them in the final HTML tag

I am facing a challenge with converting a JSON array into HTML using angular's ng-repeat. The JSON structure I'm working with looks like: data:{ thing_one:[{ id:1, fields:[{ .... }] }, { id:2, fields:[{ .... ...

Over time (a few days), the setInterval function causes the react app to become unresponsive

In my React app (Next.js), I have implemented a feature that displays a QR code refreshing every 5 seconds, alongside a clock that updates every second. Additionally, I have integrated a service worker to enable Progressive Web App (PWA) functionality and ...

How to prevent CSS styles from being overridden by SASS in a stylesheet

When working with a Sass file, I encountered an issue with the way styles were being output. The original style in the Sass file looked like this: .style{ width: calc(100%); } However, when compiled to its corresponding CSS file, the output was: .style{ ...

The order of event handlers in jQuery

I am currently setting up event binding for a text element dynamically. Take a look at the following code snippet: <input type="text" id="myTxt" /> <script type="text/javascript"> function attachEvent1(element){ element.keyup(func ...

When using the $.ajax() function, the entire webpage is returned, and unfortunately, it is unable to delete

Is there a problem deleting a row from records using ajax? The $.ajax() function seems to be returning the whole page instead of just deleting the row. Html code <tbody> <tr class="gradeX odd"> <td class="sorting_1">2</ ...

When using jQuery, you can utilize the mouse over event to display elements with the same class within an each loop

I am struggling with a hover effect on my webpage. I have three visible elements and three hidden elements, and I want each visible element to display only one hidden element when hovered over. However, the issue is that currently, hovering over a visible ...

Enhancing Function Calls for Better Performance in V8

Is V8 capable of optimizing repeated function calls with identical arguments? For instance, in the code snippet below, Variance is invoked twice with the same arguments. var Variance = require('variance'); function summary(items) { ...

VueJS Component has trouble refreshing the DOM content following an AJAX Promise

I've encountered issues updating data in my Vue application after an AJAX callback. I have previously resolved similar problems by using Vue.set, but for some reason, it's not working for me today. The only difference is that I am calling a serv ...

Navigating to a Different Vue or Page with Vue Form Wizard

I'm currently exploring VUE JS and trying to figure out how to redirect to another vue or page after submitting a form. I've been working with the sample code provided at https://stackblitz.com/edit/vue3-form-wizard-demo?file=src%2FApp.vue. Whil ...

I'm not sure what the issue is with my navbar animation not functioning properly

My navbar has been styled with a transition that should ease in and out, but for some reason it's not working as expected. Even when I hover over the li a elements, the ease-in-out animation is not displaying, and I'm struggling to figure out wha ...

IE9 does not properly display SVGs used as background images

Utilizing SVG files as backgrounds for my HTML elements has been successful in most major browsers. However, an issue arises when trying to ensure compatibility with Internet Explorer 9. The problem lies within IE9's rendering of the SVG backgrounds, ...

Modifying the position of an image in Flask

I am trying to center an image on my webpage and have attempted the following code: <image height="300px" width="300px" src="{{url_for('static',filename = 'img/Rasd.jpeg')}}" > Visit this webpage fo ...

Generate a fresh object with distinct identifiers

I am interested in creating a new object, utilizing unique keys consisting of comma-separated IDs. For instance, I have: const d = { "1,2,3,4,5,6,7,8,9,10": [ "created_by", "modified_on", "external_o ...

Concealing categories within an accordion-styled menu

Recently, I put together a list that includes various pieces of information along with an accordion menu. Take a look at the list However, I've encountered a small issue which has left me quite perplexed. When a menu item is clicked - for instance, ...

The span is unresponsive

When focusing on an input or checking its validity, the span should move up but it's not responding to any styles. <div class="login"> <span class="logtxt">Username or email</span> <input type=& ...

Updating Github pages requires clearing the cache first

As I work on my first website using GitHub pages, I've noticed that it can be frustrating to constantly clear the cache or open an incognito window whenever I add something new. I'm thinking about incorporating Jekyll into my workflow so I can t ...

Unable to locate the node module within the Rails stimulus controller

I'm facing an issue with integrating my own npm package into a Rails application that I have developed. It's unclear whether the problem lies in the creation of the node package or within the rails app itself. The package in question is fairly ...

The issue encountered with the JQuery DataTables is a "dialog: oCol is undefined

I am having an issue with jQuery DataTables while trying to display data in two separate tables on the same page. The error message I receive is: oCol is undefined This error pops up in a dialog box. Can someone assist me in resolving this problem? Bel ...

Run an ajax function when a variable is populated with data

My current dilemma involves a session variable email=$_GET['email'];. I am seeking a way to trigger an ajax function only when this variable is available, and to set it to do nothing if the variable is not present. What is the technique for invo ...