Animating two divs simultaneously with JQueryUI

On my webpage, I have two full-width divs that I want to show one at a time using JQueryUI slide transitions. The desired effect is for one div to slide away to the left while the other slides in from the right. However, the current issue is that both animations occur simultaneously, causing the second div to initially appear below the first before sliding up to replace it at the end of the animation. How can I adjust the positioning of the second div to create a seamless sliding motion?

Here's the code snippet:

<script type="text/javascript">
    $('#schedule').hide();
    $( '#changer' ).click(function() {
            $("#roster").toggle( "slide", {direction: "left"}, "fast" );
            $("#schedule").toggle("slide", { direction: "right"}, "fast");

            if ($("#changer").html() == 'Schedule View')
                $("#changer").html('Roster View');
            else
                $("#changer").html("Schedule View");
        });
</script>

Answer №1

Take a look at this demo: http://jsfiddle.net/FkNa2/2/

The key part of the code is the css positioning,

// html
<p>some content here </p>
<div class="container">
    <div id="div1"></div>
    <div id="div2"></div>
</div>
<button>Toggle</button>

// css
#div1{
    width: 500px;
    height: 100px;
    background-color: blue;
    position: absolute;
    top: 0px;
    left: 0px;
}

#div2{
    width: 500px;
    height: 100px;
    background-color: red;
    position: absolute;
    top: 0px;
    left: 0px;
}

.container {
    width: 800px;
    position: relative;
    left: 100px;
    height: 100px;
}

The trick here is to make sure that both divs are positioned in the same place on the screen. This is achieved by using absolute positioning for both, with coordinates relative to the outer div. You can find more information on this technique in the answers provided in this question

Both “relative” and “absolute” positioning are really relative, just with different framework. “Absolute” positioning is relative to the position of another, enclosing element. “Relative” positioning is relative to the position that the element itself would have without positioning.

It depends on your needs and goals which one you use. “Relative” position is suitable when you wish to displace an element from the position it would otherwise have in the flow of elements, e.g. to make some characters appear in a superscript position. “Absolute” positioning is suitable for placing an element in some system of coordinates set by another element, e.g. to “overprint” an image with some text.

As a special case, use “relative” positioning with no displacement (just setting position: relative) to make an element a frame of reference, so that you can use “absolute” positioning for elements that are inside it (in markup).

Answer №2

Increase the z-index (w3schools) value of the div element.

Think of it like different levels or layers on a website – lower numbers are at the bottom, higher numbers are at the top.

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

adjusting the size of a CSS image sprite

I have an image called background.png with dimensions of 500X400 px, and a HTML div that is 150X100 px in size. My goal is to display a specific portion of the image background.png starting from the top-left coordinates (x: 50, y: 50) to the bottom-right ...

tips for revealing content in a div by sliding from right to left

I came across a fiddle that animates from bottom to top when the cursor hovers over it. Is there a way to modify it so that it animates from right to left on click, and then hides the content? After hiding the content, I would like to have a button that a ...

Creating a blank div in bootstrap without any content inside and filling it with a background color

Is it possible to display 3 lines inline side by side in bootstrap? I've encountered a challenge with the height and width! While using pure css works fine, integrating bootstrap seems to be causing issues. Below is an example of the code using CSS f ...

Is there a more straightforward alternative method to retrieve the title attribute from an input within a table cell?

Could you please help me with extracting the title attribute of the input located in the 2nd column (th) of the row with the "row_selected" class from the following table? Here's a link to the table. <table id="tabla_descuentos" class="tablesorter ...

Checkbox customization that shifts position when clicked

I seem to be missing something as I can't seem to find a solution for this particular issue. I have implemented custom checkboxes on a list of sentences. Initially, they appear fine, but once you check the first checkbox, it shifts its position. I am ...

Leverage Summernote in conjunction with Vue components to dynamically switch their locations

I am experiencing a strange behavior with Vue components created using v-for. Each component has a textarea for summernote, and initially everything works correctly. The editor loads the text and updates it when edited. However, when the position propert ...

Only one execution of the ajax success function (using the html method) is triggered

I am encountering some issues with ajax callbacks and could really use some assistance. Essentially, this script is designed to replace the #inbox div with archive.php (which usually generates messages and modals, but for simplicity's sake, I have inc ...

When applying the 'aspect-ratio: 1' property to create square cells, the 3 by 3 CSS Grid exceeds the boundaries of the container with overflowing squares

After countless attempts and hours of tweaking, my goal remains to create a 3x3 grid composed of squares that adjust to fit the content inside. I also want each square to have a corner number, with text content centered both vertically and horizontally. De ...

Click on the nearest Details element to reveal its content

Is there a way to create a button that can open the nearest details element (located above the button) without relying on an ID? I've experimented with different versions of the code below and scoured through various discussions, but I haven't be ...

Executing jQuery AJAX Request to PHP File with JSON Response

I've hit a roadblock trying to solve this issue. I've attempted numerous solutions on stackoverflow without success! Essentially, when I make an AJAX POST request, the PHP returns JSON data, but the AJAX displays 'Undefined' instead of ...

Disabling a button in AngularJS until an input field has a value: step-by-step guide

When it comes to Angular, I am a beginner. However, I have been tasked with creating a validation for a new Google Maps input field. My goal is simple: ensure that the #lugar_continuar button remains disabled until the #ciudad input field is filled in. But ...

Mentioning a component that is loaded dynamically

Here is the code snippet: <html> <head> <script type="text/javascript" src="jquery.js"></script> </head> <body> <div id="Block"></div> <script type="text/javascript"> function Request(M ...

Update displayed information according to the dropdown option selected

Currently, I am working on implementing a dropdown feature that allows users to switch between two different views of the same information. I am looking for guidance on how to connect my dropdown selection to control which div is visible to the user, while ...

Custom Native Scrollbar

Currently, there are jQuery plugins available that can transform a system's default scroll bar to resemble the iOS scroll bar. Examples of such plugins include . However, the example code for these plugins typically requires a fixed height in order to ...

How can I make a basic alert box close after the initial click?

After clicking a button, I have a simple jQuery script that triggers a fancybox. However, the issue is that the fancy box does not close after the first click of the button; it only closes after the second click... Another problem arises inside the fancy ...

navLinkDayClick function from FullCalendar

Has anyone implemented navLinkDayClick in FullCalendar to trigger a custom event based on the selected date by fetching data from a database? I have successfully used eventClick to populate the calendar data, but I am facing difficulties in implementing t ...

Troubleshooting: jQuery AJAX not Invoking C# Method in ASP.NET MVC Application

I am attempting to initiate a call to the controller from the views using jQuery ajax, however, it seems like the controller is not being called. The goal is to pass the token value obtained on the registration page to the controller in order to utilize it ...

"Check if jQuery contains a specific number using the .contains()

Today has felt never-ending, and I think I've developed code blindness... I'm attempting to determine if an element contains a number in order to trigger an action on another element. result_val = 9; $(".result").contains("+result_val+") (func ...

Changing animation during mouse hover off

I've been experimenting with using 'translate3d' and 'transition' on hover to animate a div into view. While the animation works smoothly when hovering over the element, I'm having trouble getting it to transition back out of ...

How can I transform a string into a date using jQuery specifically for Internet Explorer?

I’m looking to change a date string to a date object using jQuery. The code below does the job in Chrome and Firefox, however, Internet Explorer is causing some issues: <script type="text/javascript" charset="utf-8"> // Validate if the followup da ...