Incorporate a fresh animation once the transition is complete

Currently, I have implemented a script that triggers the animation of content boxes sliding in from the top upon entry and then sliding out from the bottom upon exit.

Although it is functioning almost flawlessly, I am encountering an issue where, when switching between content box one and content box two multiple times, the first box enters from the bottom instead of the top.

I have a theory as to why this occurs (due to the code being executed all at once, resulting in direct transition from below the viewport to above the viewport without a proper entrance sequence), but I am struggling to find a solution to consistently make it enter from the top.

Here is a snippet of the HTML:

<div class="slidey slidey1 enter">
    Content Box 1
</div>
<div class="slidey slidey2">
    Content Box 2
</div>
<div class="slidey slidey3">
    Content Box 3
</div>

Corresponding CSS style:

.slidey { top:-100% }
.enter { top:0; transition: all 0.7s ease-in-out; }
.exit { top:100%; transition: all 0.7s ease-in-out; }

Additionally, the jQuery script used:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){ 
  $(".changer").click(function(){
    if ($(".slidey" + $(this).data("slidey")).hasClass("enter")) {
        return false
    }  else {
        $(".slidey").removeClass("exit");
        $(".slidey.enter").addClass("exit").removeClass("enter");
        $(".slidey" + $(this).data("slidey")).addClass("enter");
        $(".changer").removeClass("link_change");
        $(".changer" + $(this).data("slidey")).addClass("link_change");
        return false;
    }
  });
});
</script> 

Unfortunately, the page is no longer accessible for viewing.

Answer №1

Once you have added a class, make sure to set up a listener for the "transitionend" event like this:

$(".myElement").addClass("transitionClass").on("transitionend", function() {
    // Transition has ended.
    // Add more actions here.
});

I have simplified and reconstructed your HTML and CSS for this example, so feel free to focus on the jQuery code. It should work seamlessly on your website without any modifications.

$(document).ready(function(){
  
  var $slides = $(".slidey");
  
  $(".changer").click(function( e ){
    e.preventDefault(); 
    var num = $(this).data("slidey");
    var $target = $(".slidey"+ num);
    
    $(".enter").not($target) 
      .removeClass("enter")  
      .addClass("exit")      
      .on("transitionend", function(){ 
              $(this).removeClass("exit"); 
      });

    $target.addClass("enter"); 
    
    // Updating navigation links
    $(".changer").removeClass("link_change");
    $(this).addClass("link_change");

  });
  
});
*{margin:0;}
html, body{height:100%;}
body{overflow:hidden;}

#navbar{
  position:absolute;
  bottom:130px;
  right:130px;
}
#navbar ul {list-style:none;}

.link_change{
  color:fuchsia;
}

.slidey {
  position:absolute;
  width:50%;
  height:90vh;
  background:#ddd;
  top:-100%;
}
.enter {
  top:0;
  transition: all 0.7s ease-in-out;
}
.exit {
  top:100%;
  transition: all 0.7s ease-in-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="navbar">
  <ul id="navlinks">
    <li class="changer changer1 link_change" data-slidey="1">home</li>
    <li class="changer changer2" data-slidey="2">profile</li>
    <li class="changer changer3" data-slidey="3">message</li>
  </ul>
</div>

<div class="slidey slidey1 enter">Content Box 1</div>
<div class="slidey slidey2">Content Box 2</div>
<div class="slidey slidey3">Content Box 3</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

File resolution issue with TypeScript

While I am aware that using TypeScript outFile is generally advised against, I currently have no choice but to utilize it until a more suitable solution like AMD can be implemented. It seems like there may be a "module splitting issue" in my project. In t ...

Design of web pages, Cascading Style Sheets

I want to add another table next to my dataGrid, floating to the left of it. Like this representation in the main div: | | | | A | B | | | | Here, A represents the current dataGrid containing all user information, and B will be the ...

issue with angular and typescript

I'm currently working on developing an Angular 2 application that incorporates touch gestures using hammerjs. My goal is to merge the quickstarter application from: Angular 2 with the hammerjs application from: Hammerjs sample. However, I keep encou ...

References to high order functions in JavaScript

Although the code below is functional, I believe there might be a more efficient approach. I am currently passing a reference to the higher-order function's scope. var self=this; this.nodeModal.find(".modal-footer .save").click(function(){ ...

Having trouble with the CSS content Property in Firefox?

My current project involves implementing a feature where a close button (in the form of an 'x' character) appears in the search box after a user enters a search term, allowing them to clear the contents easily. This functionality is working smoo ...

Tips on transforming JSON data into a hierarchical/tree structure with javascript/angularJS

[ {"id":1,"countryname":"India","zoneid":"1","countryid":"1","zonename":"South","stateid":"1","zid":"1","statename":"Karnataka"}, {"id":1,"countryname":"India","zoneid":"1","countryid":"1","zonename":"South","stateid":"2","zid":"1","s ...

Creating a fixed top bar button in Ionic and iOS along with a scrollable list

In my app using Ionic with the iOS platform, I am trying to achieve a specific layout: A header A fixed bar button A scrollable list Here is what I have attempted so far: <ion-view view-title="Account"> <div class="button-bar" style="margin ...

Ensure that the element fits within the container without increasing in size

My current challenge involves solving a seemingly simple problem. Imagine I have a layout consisting of a header, content, and footer. Within the content section, there is a row of items that fill all available space. Each item has equal width and contains ...

Implementing a dynamic dropdown list with pre-selected values within a while loop

Is there a way to have the selected value in a dropdown list reflect a value given by a JavaScript function that updates a GET parameter in the URL when an option is chosen? <?php while ($row = mysql_fetch_array($res)) { echo '<option value=&ap ...

Unlocking the Power of Django with Ajax and Facebook API: Conquering the 403 Forbidden

I'm attempting to integrate the Facebook Login API into my Django project. My initial plan was to use a Facebook username and a default password (since Django requires a password for user creation) and authenticate via AJAX. FB.api('/me', ...

Field types: Elements encased within square brackets

When using Flow Types: export type GroupType = { options: OptionsType, [string]: any, }; What does the syntax [string]: any signify? Edit : Appreciate all the responses provided. How should I interpret this particular piece of code? import type { ...

Is there a method to detect the presence of a bot in your discord server using another bot?

I am developing my own unique discord bot and I am looking to implement a feature that can determine if a specific bot is present in the server. Here's how I've been approaching it: if (!message.guild.args[0]) In this code, args[0] represents ...

Steps to dynamically populate a datatable with JSON data by triggering a click event in jQuery

I am facing an issue with feeding JSON data into datatables when the search button is clicked. The JSON data structure is as follows: [ { "port_code":"BOM", "cont_details_id":"9", "price":"44.000", "cont_price":"500", "c ...

Color remains unchanged for selected value in dropdown menu

I'm facing a challenge in changing the color of the selected value in the dropdown menu. Each value has a different color, but when I select one, it reverts back to black. .appt-status select option[value="0"] { color: #3EA47B; } .appt-status o ...

Resolving schema by mapping array items

When using GraphQL, I successfully queried a single object on this Launchpad without any issues: However, I encountered an error when attempting to fetch an array of all "characters" with the same approach: "Expected Iterable, but did not find one for fi ...

management in the same row or in the following row fashion

Looking to achieve a specific behavior for a label and control: If the screen width is less than X, the label and control should be on different rows with the control expanding to full width. If the width is equal to or greater than X, the label and cont ...

Is there a way to save a Morris.js chart as a PDF file

I have successfully created a Morris.js Bar Chart using data from PHP and MySQL. Now, I am looking for a way to export this chart into a PDF format. I have attempted to do so using the FPDF library but I am struggling with the implementation. Can anyone ...

Setting the height to "auto" will take precedence over any other

I recently designed a responsive webpage with two distinct sections. In order to ensure all elements in the right section were responsive, I utilized the following CSS code: #rightSection * {max-width:100%; height:auto;} Despite this, I noticed that any ...

Difficulty arising from commands

I am currently familiarizing myself with the use of directives in AngularJS. I have a password.html file that contains an input field for passwords, and I have created a custom directive called 'passwordRequirement' to enforce specific requiremen ...

Are the frameworks Vue, Angular, and React known for

During a conversation, I came across an interesting viewpoint criticizing popular frameworks such as Angular, Vue, and React. It was argued that these frameworks have a significant disadvantage: apart from the API part that interacts with the server's ...