Div automatically shifts to center position when other div elements vanish from view

I am new to jquery and currently working on a basic website. The site consists of three boxes, each of which, when clicked, causes the other two to fade out using the .fadeOut('slow'); method. However, there is an issue - all of the boxes are enclosed in <center> tags. For example, if I click the box on the left, the middle and right boxes will fade out but then the left box jumps to the center abruptly instead of transitioning smoothly.

For reference, here is the jsfiddle:

Answer №1

To solve your issue, I recommend incorporating fadeTo() into your JavaScript code consistently. By using fadeTo(), you can prevent the sudden movement of other elements when transitioning to a new position.

Additionally, I made some optimizations to your JavaScript code. It's worth noting that the <center> tag is no longer recommended in HTML5, so try to steer clear of using it. Let me know if you have any feedback!

Here's the revised HTML:

<body>
    <div id="clickableContainer">
        <div class="clickable" id="one"></div>
        <div class="clickable" id="two"></div>
        <div class="clickable" id="three"></div>
    </div>
    <article class="slider" style="display:none;" id="1"><div class="back">Back</div></article>
    <article class="slider" style="display:none;" id="2"><div class="back">Back</div></article>
    <article class="slider" style="display:none;" id="3"><div class="back">Back</div></article>
</body>

JavaScript enhancements:

$(document).ready( function(){
    $('#one').on("click",function(){
        $("#two, #three").delay(300).fadeTo(300,0, function(){
            $('#one').delay(1000).fadeTo(300,0,function(){
                $("#one, #two, #three").css("display","none");
                $('#1').delay(1900).fadeTo('slow', 1);
            });                        
        });
    });
    // Additional click event handlers for '#two' and '#three'
       
    $('article').on("click",function(){
        $(this).fadeTo(300,0,function(){
            $(this).css("display","none");
            $('#one, #two, #three').delay(800).fadeTo(300,1);
        });        
    });
});

Explore the code snippet at this JSFiddle link.

Answer №2

Execute the function using a full action that changes visibility to hidden:

$element.fadeOut('slow', function() {
   $element.css("display", "initial").css("visibility", "hidden");
})

Answer №3

It's recommended to utilize .fadeTo over .fadeOut in this scenario. Adjusting your code slightly to hide and display the <center> element would be necessary.

Check out this Fiddle 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

Guide to converting a specific tag into div using Javascript

I am working with some HTML code that includes a div: <div class="myDiv"> <a href="" title="">My link</a> <p>This is a paragraph</p> <script>//This is a script</script> </div> Additionally, I ha ...

presentation banner that doesn't rely on flash technology

Looking to create a dynamic slideshow banner for my website inspired by the design on play.com. This banner will feature 5 different slides that transition automatically after a set time, while also allowing users to manually navigate using numbered button ...

Sending Real-Time Data Using Ajax from jQuery to PHP

I am encountering an issue with a table that is generating dynamic rows. I am trying to send the data through an Ajax post request to PHP, but it seems to be causing errors. Below are the codes I have selected, which do not seem to be working as expected. ...

Creating form elements without utilizing the <form action: MAILTO:> attribute

I'm in the process of designing a website with a side bar that features a form on every page. I'm looking for a way to have the form information submitted to an email address without using the action: MAILTO:? Would it be possible to create a sep ...

Identify the moment a dialogue box appears using jQuery

I'm facing a situation where multiple dialogs are opened in a similar manner: $("#dialog").load(URL); $("#dialog").dialog( attributes, here, close: function(e,u) { cleanup } The chall ...

Display the JQuery element that contains a child element with a specified class

On my webpage, I have a dropdown menu (ul) and I want to keep it open when the user is on the page. Specifically, I only want to display the ul item if it contains an li element with the class ".current-menu-item". The code snippet below accomplishes this ...

Items that do not appear once my page has finished loading

We are developing an admin panel for the client to manage updates on their own website. To achieve this, we are using JavaScript and PHP (ajax) to create divs: $(document).ready(function($) { function loadContent(page) { ...

Aligned sections within a Susy layout

Although Susy is a powerful tool, I have noticed a potential weakness with it. For example, imagine having three floated block elements within a container named "blocks": The "block" element is assigned a "span(4 of 12)" <div class="blocks"> &l ...

Error encountered: Attempting to render an object as a react component is invalid

I am attempting to query data from a Firestore database. My goal is to retrieve all the fields from the Missions collection that have the same ID as the field in Clients/1/Missions. Below, you can find the code for my query: However, when I tried to execu ...

Applying CSS styles to a page depending on certain conditions

Currently, I have a page in SharePoint that can function as a pop-up. I am using JavaScript to identify whether it is a pop-up by checking if window.location.search.match("[?&]IsDlg=1"). However, I am struggling to figure out how to insert CSS based on ...

What is the best way to show JavaScript output with span style?

I have added a translation feature that allows users to switch paragraphs to French when clicked. Each paragraph now has the first word wrapped in a span with a CSS class that enlarges and colors the text. However, I am facing an issue where when switchi ...

Create a dynamic design with Angular Material's mat-drawer at full height using flexbox, allowing content

Yesterday, I encountered an interesting CSS problem involving the mat-drawer and Angular's router-outlet. My layout consists of a full-page flexbox with two children: a mat-toolbar at the top, and a custom component called app-sidenav at the bottom. T ...

Exploring the wonders of Onsen UI's ng-repeat feature combined

Facing a major issue with my app, and I suspect it's due to the asynchronous call. The HTML section looks like this: <ons-list ng-repeat="item in newsEventi.items"> <ons-list-header class="news_titolo_lista">{{item.categoria}}</ons ...

Switch between Bootstrap Chevron

I need assistance with toggling a chevron in a bootstrap dropdown menu. Currently, the chevron only changes when clicked directly, but I want it to change whenever the dropdown is toggled open or closed. This way, if another part of the menu is clicked, th ...

Operating two independent Angular applications simultaneously on a single webpage

I'm currently developing an application that allows multiple Angular applications to be embedded within a main frame. Currently, I am using IFrames for this purpose, which is functioning well but has its limitations. I am exploring ways to embed ano ...

How to locate the position of an element within a multi-dimensional array using TypeScript

My data structure is an array that looks like this: const myArray: number[][] = [[1,2,3],[4,5,6]] I am trying to find the index of a specific element within this multidimensional array. Typically with a 1D array, I would use [1,2,3].indexOf(1) which would ...

Hover effect transition malfunctioning

I've been attempting to incorporate a hover image effect on my website. I included the script in the link below. However, I'm struggling to achieve a smooth fade transition and a 2-second delay on the hover image. Can someone please assist me wit ...

When there is content behind the list, the Autosuggest Ajax does not function properly

I have successfully implemented an ajax/jquery dropdown/list feature that retrieves results from the database based on user input. For each result in the database, it generates a <li> element and converts it into a clickable link to redirect users t ...

What is the best way to update properties of an array object using a map in pure JavaScript for maximum efficiency?

Looking for an efficient solution to replace property keys in an array of objects using a map? Here's an example scenario: // Consider an array of objects: var records = [ {"Id": "0035w000036m j7oAAA", "Business Phone": ...

Exports for Express Router Module/Functions

I am currently working on exporting a function and an express router from the same file. The function is intended to verify certificates, while the route is meant to be mounted on my main class for other routes to use. I want to encapsulate both functional ...