How to make sure CSS3 and JQuery are synchronized in applying styles

I would like to implement a feature where a div can slide in from either side of the page, eventually creating a carousel-like application. When a user clicks the "forward" or "backward" button, I want to slide out any active divs, create a new div, assign it the classes "left" or "right" to determine its starting position, and then add the transition class "active" so it smoothly moves into place.

$("#forward").click(function(){
    $(".active").removeClass("active left right").addClass("toRight").bind(transitionendstr, function(){
            $(this).remove();
    });

    data = "new data here";

    target = $(slidepartial).clone().addClass("left");
    $("#content").append(target);
    $(target).addClass("active");

});

The CSS for this setup is as follows:

.active{
left:50% !important;
transition:left 2s;
-moz-transition: left 2s; /* Firefox 4 */
-webkit-transition: left 2s; /* Safari and Chrome */
-o-transition: left 2s; /* Opera */
}

.left{
left:-100%;
}

.right{
left:100%;
}

.toRight{
left:100%;
transition:left 2s;
-moz-transition: left 2s; /* Firefox 4 */
-webkit-transition: left 2s; /* Safari and Chrome */
-o-transition: left 2s; /* Opera */

}


.toLeft{
left:-100%;
transition:left 2s;
-moz-transition: left 2s; /* Firefox 4 */
-webkit-transition: left 2s; /* Safari and Chrome */
-o-transition: left 2s; /* Opera */

}

However, I am facing an issue where the newly created div doesn't apply the "left" or "right" css properties before transitioning to the center. How can I ensure that the positioning changes take effect before applying the active css?

Answer №1

Here's a handy example to implement CSS transitions on a div. While it may not be a simple plug and play solution, it can definitely help you get started: http://jsfiddle.net/VPjX9/193/

This sample code can serve as inspiration for resolving your issue. The transition is initiated by clicking either on the div or a button, after which a class is applied. For a more detailed understanding, explore the transition classes provided in the fiddle.

$('.page1, .page2, #go').click(function() {
    var page1 = $('.page1');
    var page2 = $('.page2');
    var toHide = page1.is(':visible') ? page1 : page2;
    var toShow = page2.is(':visible') ? page1 : page2;

    toHide.removeClass('flip in').addClass('flip out').hide();
    toShow.removeClass('flip out').addClass('flip in').show();
});

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

Utilizing SVG elements for interactive tooltips

Can the < use > element show the rect tooltip on mouseover in modern browsers? Refer to 15.2.1 The hint element. <svg id="schematic" version="1.2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <symbol i ...

Display Button Information in InfoPath Form

In InfoPath 2010, I created a form and published it to a SharePoint 2010 list. The end user wants to print this form for their records, but when using the web part to print the screen, the page comes out very small and unusable. Is there any HTML code th ...

Deliver real-time notifications to linked users by leveraging socket.io and node.js

Is there a solution for sending real-time updates to connected clients every second without using the setInterval() function in nodejs and socket.io? Please provide an alternative method that fits my specific scenario. ...

The expression "routerlink" in Angular 9 is not recognized,

Currently, I am in the process of developing an Angular 9 application and have encountered two challenging issues. Firstly, as I navigate through the routes using the navbar, I notice an error message in the console stating "Syntax error, unrecognized exp ...

AngularJS ui-select not responding correctly to selected items

Currently, I am utilizing the ui-select module within AngularJS. <ui-select ng-model="tipData.category" search-enabled="false" name="category" required="required" class="orange-site-color-select custom-select"> <ui-select-match><span clas ...

Drop-down menu options failing to appear within the drop-down menu

I am facing an issue where the dropdown menu is visible but the items inside it are not appearing. <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" role="button" data ...

Mistake in Timestamp Separation - JavaScript

I have a Timestamp retrieved from MSSQL and displayed on a Webclient. The timestamp reads: Thu Jan 01 18:00:00 CET 1970, however, I am only interested in the time format: 18:00 or 18:00:00 (preferably the former). <script> var timestamp = ...

Discover ways to retrieve an ajax response from a different domain by submitting specific data to the controller method while operating on an internet server

I am facing an issue where I am unable to retrieve array data from a Codeigniter controller using an Ajax request. The data is being posted to the controller to fetch related data from the database, and it works perfectly fine on my local server. However, ...

Jquery double-click Event Not Functioning Properly

I've been attempting to toggle the visibility of my footer navbar while also changing the chevron icon. When the navbar is hidden, I want the chevron pointing up to be displayed, and when the navbar is shown, I want the chevron pointing down to be dis ...

Initial request fails to retrieve cookie generated by Javascript

I created a shopping cart that generates a cart, adds items, and stores it in a cookie named 'cart'. However, when I click on a hyperlink that leads to the checkout page, my ASP.NET application is unable to access the cookie. The cookie only get ...

Is it possible to create a special case for the beforeunload function?

Is there a way to exclude a specific URL or form button from the beforeunload event in JavaScript? I want to exempt them, not as error handling exceptions but to customize how they interact with this event. Here is the relevant JavaScript code: if(loggedi ...

Error message: Invalid label detected in Jquery ajax request

I am currently trying to make an ajax call to the URL below: However, I keep encountering an invalid label error in the firebug console. I have included my ajax code below. Can you please review it and let me know if there is something wrong with it? //M ...

Obtaining exchange rate data in JSON format from fixer.io and displaying it in

Looking to extract the current USD to EUR exchange rate from fixer.io and display it as a single line in an HTML file, with the USD value represented using commas instead of periods. Any assistance would be greatly appreciated! Find the link here: { ...

Enhance user interactivity on your website by incorporating jQuery and CSS for

<table id="tab"> <tr aaa="one" bbb="ooo"><td>xxx</td><</tr> <tr aaa="two" bbb="one"><td>xxx</td><</tr> <tr aaa="three" bbb="one"><td>xxx</td><</tr> ...

What alternative can be used instead of Document in Javascript when working with EJS?

Currently, I am utilizing ejs to handle my HTML tasks and I have come across an issue where I cannot use the usual document.getElementById('id') method within this environment. The error message displayed states "document not defined". This has ...

JavaScript's inability to load images lazily poses a significant challenge

I'm having trouble implementing lazy loading for my images using JavaScript. When I change the html attribute from src to data-src, the images do not display at all. Additionally, when attempting to use an intersectionObserver to lazily load the image ...

Enhancing the appearance of a JSX component in React

I have a segment of code within my project that calculates a specific percentage and displays it on the dashboard. <text className="netProfit-circle-text" x="50%" y="50%" dy=".2em" textAnchor="middl ...

Choose an option for retrieving additional information

My goal is to select the item name based on its ID and automatically add the item's serial number to the serial number input when it is selected. I believe I need to use AJAX for this functionality, but I am unsure of the exact implementation. Item N ...

Having trouble with JQuery's .find() function?

I am currently implementing AJAX to load HTML from a file and attempting to extract and evaluate a specific block of dynamic HTML/JS content. Here is the AJAX call: <script type="text/javascript" id="startScript"> $("#submitButton").button(); & ...

The <label> element is failing to recognize the "id" attribute of the input element in HTML

I have developed an Angular application that utilizes ASP.Net Core as a backend and incorporates bootstrap 4 and ngx-bootstrap for styling. Strangely, I am encountering an issue where the ID's on all my HTML <label> tags are not being recognize ...