How can I make a div collapse in the same way as the Facebook chat

I am working on creating a chat feature similar to Facebook chat for personal use. Everything is functioning correctly, except for some issues with the CSS.

I attempted using position absolute to arrange the div, which worked fine, but encountered problems when looping the chat box in PHP. I needed to use float to align the div boxes to the right.

You can view my jsfiddle here

Below are snippets of code in jQuery:

//Close
$('.closed1').click(function () {
    $('.wrap_box1').hide();
    $('.main_chat1').addClass('hide_wrap_box');
});

//Open
$('.open_chat1').click(function () {
    $('.wrap_box1').show();
    $('.main_chat1').removeClass('hide_wrap_box');
});

In my jsfiddle demo, you'll notice that the chat box collapses to the top. How can I make it collapse to the bottom? Try clicking the close button.

Answer №1

If I were to tackle this problem, I would take the following approach:

My Approach:

  • Create a chat area that contains the chat-boxes
  • Set the chat-boxes to display as inline-blocks
  • Position the user boxes at the bottom of the chat area

In your specific scenario:

I suggest using display:inline-block for the chat_box class so that the parent element adjusts to the size of the box.

Also, float the parent element of the chat_box to the right:

#chat_area {
    float:right;
}

Meanwhile, align the user_box itself to the bottom of the chat_box:

.user_box {
    ...
    bottom:0;
}

This way, the entire chat area will float to the right and resize accordingly when all chat boxes are closed.

For a visual representation, check out this fiddle: http://jsfiddle.net/mazzt/7/

Answer №2

After studying your example, I attempted to implement it!

 $(document).ready(function () {
        $('.main_chat2').toggle("bounce",{ times: 3 }, "slow");
    $('.user_box').click(function () {
        if ($('.wrap_box2').is(":visible")) {
            $('.wrap_box2').hide();
            $('.main_chat2').addClass('hide_wrap_box');
            $('#icon').html('^');
        }
        else {
            $('.wrap_box2').show();
            $('.main_chat2').removeClass('hide_wrap_box');
            $('#icon').html('x');
        }
    });
   });

You can check out the results at the following link: http://jsfiddle.net/ernestoarbitrio/DyfBW/31/

Answer №3

Experiment with slideToggle(); and perhaps even toggleClass();

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

Positioning two images at the bottom left and bottom right corners of a container

I have successfully implemented a justified block of text within the footer of my Bootstrap HTML page. Now, I want to enhance it by adding two images underneath the text. These images should have the same height but different widths and be aligned to eithe ...

Checking for the existence of a specific key in JSON data from the Twitter API using

Successfully fetching a tweet from the Twitter API is possible. Along with displaying the user, tweet, and other data, I also want to display the image. However, if there is no image attached to the tweet, nothing appears as the key doesn't exist. ...

A div element springs forth into a new window and seamlessly transitions back to its original position with fresh content

Most of us are familiar with Gmail chat, where you can pop out the chat window into a new window with its content, and then pop it back in to its original position. However, I encountered an issue while working on replicating this functionality. While I w ...

What is the best way to ensure that a parent element with a nowrap property can inherit the width of

It's proving difficult for me to make a parent element inherit the width of its children. Here is the link to the jsfiddle http://jsfiddle.net/4mk3S/ Currently, I only have an outer div with white-space: nowrap; and child divs with display ...

Setting overflow sizes manually for parent containers when child elements are transformed using CSS can be done by following these steps

It has come to my understanding that CSS transforms do not impact the actual size of an element, rather its visual representation. There have been discussions on this topic: CSS Scale transform on child not affecting parent size CSS transform: scale does ...

How come my Django application is returning an empty response when using jQuery.ajax?

Trying to send a JSON response from a Django view using an ajax call: var tab = 'example'; var response = $.ajax({ url: "/" + tab + "/" }).responseText; alert(response); This is my Django view code: If request.is_ajax() == True: req = ...

What is the best way to perform a multi-link latency check, display the ping results, and use JavaScript to determine the fastest URL?

click here for image description I have noticed that many websites offer this feature, making it easier for users to choose the best URL or server based on their location. As a JavaScript novice, I'm wondering if someone could demonstrate how this is ...

The textbox fails to update when the condition in the IF statement is met

In the following code, I have an input box with the ID volumetric_weight that gets updated on keyup. However, the second textbox with the ID volumetric_price does not update as expected, even though I believe I wrote it correctly. I am wondering if there ...

Struggling with adding auto-suggestions using data retrieved from a RESTful API

I need help implementing an autocomplete search bar on my UI. Here's the HTML code: <div> <input type="text" name="apexClass" id="autocomplete"/> </div> I've incorporated the Devbridge JavaScript library: $('#autocomple ...

Troubleshooting Cascading Style Sheet problem on Samsung Galaxy S5

In developing a Cordova Android application, I designed a CSS specifically for screen resolutions of 1080 x 1920. This CSS is accessed using media queries. @media screen and (max-width: 680px) and (orientation:portrait) //Compatible with Galaxy S5 or @m ...

Fill a dropdown menu in HTML with data from a parsed JSON object array

Can someone help me with populating an html select element from the JSON data below? I can't figure out how to use a for each loop with this type of json data. Thanks! //JSON [{ "group": "US (Common)", "zones": [{ "value": "America/P ...

Removal of div elements based on checkbox being unchecked - a step-by-step guide

How can I delete elements from a div when the checkbox is unchecked? function removeCampaignName(id) { var campaignDisp = $('#camp' + id).html(); if ($("#campaign-name-disp").text().search("" + campaignDisp) === -1) { ...

Is there a way to load jQuery after the page has loaded? Any suggestions on how

Exploring jQuery $(document).ready(function(){ $('.categories').live('change', function() { var my_location = window.location.pathname.replace('admin/', ''); $(this).parents("tr").next("tr.subcat ...

Exciting possibilities with jQuery and dynamic queries in PHP

I am currently working on a jQuery headline with the following structure: <div class="mainGallery"> <div class="main"> <div class="mainDesc"> <h2> </h2> <a class="mainA" href="#"> </a> </div&g ...

Split the input string into individual characters for each entry

As a beginner in Angular, I am currently exploring ways to split a single input field into separate inputs for each word. I attempted to achieve this using jQuery but struggled and also learned that mixing jQuery with Angular is discouraged. Here's th ...

The issue arises when using IE8/9 with $.get and .html() functions, as the retrieved data

Below is a snippet of JavaScript code that I am currently working with: $(".refresh").on("click touch", function () { $.get($("a.suggest-date").attr('href') + '#suggestedDate', null, function (result) { console.log(result); ...

What is the best way to halt a window.setInterval function in JavaScript?

I have a JavaScript function that runs every 2000ms. I need to find a way to pause this function so that the user can interact with other elements on the page without interruptions. Is there a way to achieve this? Below is the code for the function: win ...

Generate separation among the navigation links in HTML/CSS

Just starting out with HTML/CSS and I'm facing an issue with spacing out the items on my nav-link. Specifically, the "collections" and "spark" items are overlapping. Can anyone provide guidance on why this is happening and how I can resolve it? Should ...

Issue with div element not functioning properly within table declaration on Internet Explorer

There seems to be an issue with the div tag not functioning properly inside a table definition: <table> <tr></tr> <div id="choice"><tr> <td>-------</td> <td>-------</td> <td>-------</td> &l ...

Utilizing a CSS file within a YAML configuration in R Quarto

Is there a way to reference a css file from a yaml file in my R Quarto Book? The css file defines the theme for my Quarto document, and I want all our Quarto docs to have the same appearance. When I directly call the css file, I can customize aspects like ...