The jquery and css3 slider smoothly slides in one direction, but struggles to move in the opposite

I have successfully implemented a feature in my code where an item slides to the left and gets deleted. However, I am facing an issue where after deleting the element, a new element is created on the right side of the screen but it appears in the center without the intended animation.

 <!DOCTYPE html>
<html>
    <head>
        <title>Slide Animation Issue</title>
        <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
        <style>
            .leftRemove{
                left: -3000px;
            }
            .rightAdd{
                position: relative;
                right: -3000px;
                transition: right 2s;
                -webkit-transition: right 2s;
            }
            .centerFocus{
                position: relative;
                width: 100px;
                height: 100px;
                margin: 0 auto;
                right: 0px;
                background-color: green;
                transition: left 2s;
                -webkit-transition: left 2s;
            }                   

        </style>
        <script>
        $(document).on('click', '#main', function(){
            $('body').append("<div class='rightAdd'>Hello!</div>");
            $( "#main" ).addClass('leftRemove');
            setTimeout(function(){
                $('.leftRemove').remove();
                $('.rightAdd').attr('id', 'main');
                }, 800);
            setTimeout(function(){
                $( "#main" ).removeClass('rightAdd');
                $( "#main" ).addClass('centerFocus');
                }, 1000);
        });
        </script>
    </head>
    <body>
        <div id='main' class='centerFocus'></div>
    </body>
</html>

Any ideas on how to fix this issue?

Answer №1

It is my belief that utilizing the right property in CSS may not be valid. To shift an element to the right, you can assign a positive value to the left property instead. Additionally, I made adjustments to the placement of your appended hello to ensure a smoother transition.

Consider implementing the following code snippet:

.rightAdd{
	position: relative;
	left: 1000px;
	transition: right 2s;
	-webkit-transition: right 2s;
}

$(document).on('click', '#main', function(){
	$("#main").addClass('leftRemove');
	setTimeout(function(){
		$('.leftRemove').remove();
		$('body').append("<div class='rightAdd'>Hello!</div>");
		$('.rightAdd').attr('id', 'main');
	}, 800);
	setTimeout(function(){
		$("#main").removeClass('rightAdd');
		$("#main").addClass('centerFocus');
	}, 1000);
});

Check out an example here:

http://jsfiddle.net/9CGUU/

Update

I noticed some flickering with the "Hello!" message on fullscreen mode while testing the fiddle. The following update resolved the issue for me:

setTimeout(function(){
	$('.leftRemove').remove();
	$('body').append("<div class='rightAdd'></div>");
	$('.rightAdd').attr('id', 'main');
}, 800);
setTimeout(function(){
	$("#main").removeClass('rightAdd');
	$('#main').html('Hello!');
	$("#main").addClass('centerFocus');
}, 1000);

http://jsfiddle.net/9CGUU/6/

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

Navigating the intricacies of handling login with ajax

Essentially, the process of logging in typically unfolds as follows: The user inputs their information into the login form and submits it, The server (whether it be Ruby, PHP, Node.js, or any other) processes this data and either, a) redirects to the lo ...

Tips for achieving full width design within a container

Hello everyone, I require some assistance. I am attempting to create a full-width navigation while being constrained by a wrapper div with a fixed width of 900. I am unsure of how to achieve this without creating an additional div like the header. I am see ...

Verify if there is a cookie available; then execute the load() function. If the cookie matches, it is necessary to

$(document).ready(function() { var hrefs = { "en": ["includes/test1.html", "includes/test2.html"], "es": ["includes/test3.html", "includes/test4.html"] } $(function() { var cookie = Cookies.get('langCookie'); if (cookie) { ...

Updating Vue.js asynchronously using JavaScript import

I am facing a challenge with two form components that share a common JS validator. import { validateInput } from './validateInput.js' export default { data () { return { email: '<a href="/cdn-cgi/l/email-protection" class="_ ...

The issue of webkit animation rotation malfunctioning on Nexus Mobile devices

I'm currently working on developing a double ring radar animation within an Ionic hybrid application. The animation functions perfectly when viewed in a web browser, however, I've encountered issues when attempting to run it on the Nexus mobile ...

Resize a container to match the height of either its children or the full height of the window

I'm seeking advice on how to properly use height: 100%. Is there a way to scale a div based on the height of the window and its content simultaneously? I want one to override the other if necessary. Here's an example of standard height 100% css ...

Arrange four divisions so that they are displayed in pairs on each row when the viewport width is smaller

I have a row of 4 div elements aligned horizontally from left to right. Each div takes up 25% of the screen width. I am looking for a solution to make them wrap when the user resizes the screen instead of overlapping each other. .MenuSett { margin-to ...

Arranging form fields for uploading files in sails.js

I am facing an issue where I cannot receive parameters after the file upload parameter. This problem is mentioned in Skipper's documentation. The suggestion is to reorder the parameters before submitting the form and sending them to the server. I have ...

Does Ajax block the setting of jQuery select values in Django admin?

I'm facing a puzzling issue with the behavior of a select box on my webpage. In the Django admin, I have two consecutive select elements, one for the form (#id_form) and the other for the section (#id_section). If no selection is made for the form, th ...

Preventing toggle from altering the width of the side navigation bar

Is there a way to prevent the width of the side bar from changing when toggling the top level items in the side nav bar, especially if the sub navigation items are wider? Click on 'Click me' in the side nav item to see what happens. Check out th ...

Navigating the jQuery Search Form: Redirecting to Pages within your Website

After successfully creating my first Javascript search form with an Autocomplete Script, I am facing a challenge. I want to enable users to press "enter" after searching for a product and get redirected to the corresponding product URL page. The operation ...

Which is Better for Mobile Web Apps: Multiple Pages or AJAX?

I am currently planning a new project focused on mobile web apps. The project will involve creating a mobile web app with multiple screens, a search system, and various other features. One decision I need to make is choosing the primary framework for this ...

Display the PrimeFaces dialog in the middle of the screen

I've encountered an issue with positioning dialogs on different pages. I tried using a CSS code to center them on the screen, but it didn't work as expected. .ui-dialog{ position: fixed; top: 50%; left: 50%; margin-top: -50px; ...

Is the position relative malfunctioning when using display table-cell?

I have designed a horizontal menu consisting of buttons that I want to resize in width so that together they occupy 100% of the menu container. I need them to function like TD elements inside a TABLE. Here is the code snippet I have developed: <div id ...

What is needed to enable the functionality of the next and previous buttons? (Carousel Bootstrap)

I'm working on a text-only carousel using Bootstrap, but I'm facing an issue with the slider not functioning properly when I press the "next" and "prev" buttons. Any assistance would be greatly appreciated! <link rel="stylesheet" href="htt ...

"Can someone provide guidance on extracting the ID from a JSON value and storing it in a

Hey there, I have come across this jQuery code snippet: var fbuid = zuck; var fbjson = $.getJSON("https://graph.facebook.com/"+fbuid); I am wondering how to extract the id directly from the JSON response into a variable : { id: "4", first_name: "Mark", ...

What is the most efficient way to create a vertically stacked grid with minimal reliance on JavaScript?

When using an AngularJS ng-repeat, I encounter an issue with aligning elements on a new line. Instead of aligning the element above it, all elements on the same 'row' align with the lowest-hanging element. Even though I am using Bootstrap, I cou ...

Storing dropdown selection in database using Laravel form and AJAX

In my controller (FocalController.php), I have generated a dropdown list using the following code: $focalData = DB::table('role_users')->orderBy('users.id','DESC') ->leftJoin('users', function($j ...

Bootstrap button word wrapping issue remains unsolved

I've implemented a bootstrap layout on a basic page, featuring rows with columns arranged in fullscreen. The width and height are not specified and should remain as is. Below is my layout definition: <div class="container-fluid"> <div c ...

Creating a multi-level mobile navigation menu in WordPress can greatly enhance user experience and make

Hey there, I am currently in the process of developing a custom WordPress theme and working on creating a mobile navigation system. Although I have managed to come up with a solution that I am quite pleased with after multiple attempts, I have encountered ...