Reduce the number of divs on a webpage while incorporating animated transitions

I've been attempting to incorporate an animation on the width property for my div .panels. I've tried using transition-property in CSS and also with .animate() in jQuery, but unfortunately, it doesn't seem to be working. I also noticed that calc() is not functioning properly with jQuery animate...

The third panel should consistently have a width equal to the subtraction of the other two widths, allowing it to float on the right.

ex. width: 'calc(100% - ' + (panelWidth1 + panelWidth2) + 'px)'

If anyone has suggestions on how to make this work, they would be greatly appreciated! Thank you!

HMTL

<div class="actions">
  <button id="MinPanel1">Minimize Panel 1</button>
  <button id="MinPanel2">Minimize Panel 2</button>
</div>

<div class="panels">
  <div class="panel1"></div>
  <div class="panel2"></div>
  <div class="panel3"></div>
</div>

JS

$( document ).ready(function() {

    $('#MinPanel1').click(function() {
        var toggleWidth = $(".panel1").width() == 225 ? "57px" : "225px";
        $('.panel1').css({
            width: toggleWidth
        });
        adjustPanel();
    });

    $('#MinPanel2').click(function() {
        var toggleWidth = $(".panel2").width() == 330 ? "66px" : "330px";
        $('.panel2').css({
            width: toggleWidth
        });
        adjustPanel();
    });


    adjustPanel = function() {
        var panelWidth1 = $('.panel1').width();
        var panelWidth2 = $('.panel2').width();
        var panelWidthTotal = panelWidth1 + panelWidth2;

        $('.panel3').css({
            width: '-webkit-calc(100% - ' + panelWidthTotal + 'px)',
            width: '-moz-calc(100% - ' + panelWidthTotal + 'px)',
            width: 'calc(100% - ' + panelWidthTotal + 'px)'
        });
    }

});

CSS

[...]

.panel1, .panel2, .panel3{
    height: 100%;
    float: left;
    /*-moz-transition-property: width;
    -o-transition-property: width;
    -webkit-transition-property: width;
    transition-property: width;
    -moz-transition-duration: 300ms;
    -o-transition-duration: 300ms;
    -webkit-transition-duration: 300ms;
    transition-duration: 300ms;*/
}

[...]

DEMO : JS FIDDLE

EDITED DEMO RESULT: JS FIDDLE

Answer №1

Here is a suggestion for you to try out:

$( document ).ready(function() {

    $('#MinPanel1').click(function() {
        var toggleWidth = $(".panel1").width() == 225 ? "57px" : "225px";
        $('.panel1').animate({
            width: toggleWidth
        },function(){
            adjustPanel($('.panel1').width(), $('.panel2').width())
        });
    });

    $('#MinPanel2').click(function() {
        var toggleWidth = $(".panel2").width() == 330 ? "66px" : "330px";
        $('.panel2').animate({
            width: toggleWidth
        },function(){
            adjustPanel($('.panel1').width(), $('.panel2').width())
        });
    });


    adjustPanel = function(pan1, pan2) {
        console.log($('.panels').width(), pan1, pan2);
        var panelWidthTotal = $('.panels').width() - (pan1 + pan2) + 'px';

        $('.panel3').animate({
            width: panelWidthTotal
        });
    }

});

Answer №2

Check out this functional code snippet that utilizes css transitions and requires a slight adjustment in the JavaScript to ensure it accurately captures the final width of the divs rather than the transient one during animation.

$( document ).ready(function() {
    
    $('#MinPanel1').click(function() {
        var toggleWidth = $(".panel1").width() == 225 ? "57px" : "225px";
        var toggleWidth1=toggleWidth;
        $('.panel1').css({
            width: toggleWidth
        });
        adjustPanel();
    });
    
    $('#MinPanel2').click(function() {
        var toggleWidth = $(".panel2").width() == 330 ? "66px" : "330px";
        var toggleWidth2=toggleWidth;
        $('.panel2').css({
            width: toggleWidth
        });
        adjustPanel();
    });
    
    
    adjustPanel = function() {
        var panelWidth1 = toggleWidth1;
        var panelWidth2 = toggleWidth2;
        var panelWidthTotal = panelWidth1 + panelWidth2;
        
        $('.panel3').css({
            width: '-webkit-calc(100% - ' + panelWidthTotal + 'px)',
            width: '-moz-calc(100% - ' + panelWidthTotal + 'px)',
            width: 'calc(100% - ' + panelWidthTotal + 'px)'
        });
    }
    
});
*{
    box-sizing: border-box;
}
html, body{
    width: 100%;
    height: 100%;
}
.actions{
    position: absolute;
    top: 0;
    left: 0;
    padding: 10px;
    width: 100%;
    background: white;
}

.actions button{
    margin-right: 10px;
}

.panels{
    height: 100%;
}
.panel1, .panel2, .panel3{
    height: 100%;
    float: left;
    /*-moz-transition-property: width;
    -o-transition-property: width;
    -webkit-transition-property: width;
    transition-property: width;
    -moz-transition-duration: 300ms;
    -o-transition-duration: 300ms;
    -webkit-transition-duration: 300ms;
    transition-duration: 300ms;*/
}

.panel1{
    width: 225px;
    background-color: blue;
    -webkit-transition: width 2s; /* Safari */
    transition: width 2s;

}

.panel2{
    width: 330px;
    background-color: red;
    -webkit-transition: width 2s; /* Safari */
    transition: width 2s;
}

.panel3{
    width: calc(100% - 555px);
    background-color: darkgreen;
    -webkit-transition: width 2s; /* Safari */
    transition: width 2s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="actions">
  <button id="MinPanel1">Minimize Panel 1</button>
  <button id="MinPanel2">Minimize Panel 2</button>
</div>

<div class="panels">
  <div class="panel1"></div>
  <div class="panel2"></div>
  <div class="panel3"></div>
</div>

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

"I have successfully removed the URL from index.html. However, I am now wondering how I can include them in app

I have modified the URL in index.html to remove query parameters, but now I need my app.component.ts file to still be able to access and work with those query params using ActivatedRoute. However, when I implemented the script in index.html to remove query ...

Using withRouter() to redirect will display all components

I'm brand new to Reactjs and am currently diving into routing. In my journey, I stumbled upon the use of withRouter() for programmatic redirection. I had assumed the flow would follow: constructor->Willmount->render, but it seems the current or ...

loading content from an external link using ajax and displaying it in a specified div

Challenge: I am trying to showcase news feeds from various websites. I want to implement a feature where upon clicking on a link, a modal panel pops up displaying the contents of that link. Methods I've Attempted: Utilizing Jquery's load and ...

What is the best way to ensure that two objects collide with one another?

Issue Description I am currently working on implementing collision detection for two objects. The goal is to determine if the objects are intersecting by calculating their bounding boxes with Box3 and using the .intersectsBox() function to obtain a boolea ...

What is the proper method for setting up handlers in functional React components?

My knowledge of JavaScript tells me that there are three different ways to define functions. Let's take a look at them: 1. Declaration function handleEvent(e) {} 2. Assignment var handleEvent = function(e) {} 3. Arrow var handleEvent = (e) => ...

An alternative to Firebug for Internet Explorer

Can anyone suggest a suitable replacement for Firebug that is compatible with IE 7 and 8? I need a tool that allows me to edit CSS/HTML in real-time, debug JavaScript code, and visualize the positions of elements on web pages. Appreciate any recommendati ...

Resize images in PHP and MySQL with precision without distorting the image

I'm looking for a solution to resize images on upload without deforming them. The current code uploads the image and stores it in the database, but I need to add resizing functionality. I'd prefer not to use JavaScript, but if necessary, please i ...

What is the proper way to utilize a class with conditional export within the Angular app.module?

This query marks the initiation of the narrative for those seeking a deeper understanding. In an attempt to incorporate this class into app.module: import { Injectable } from '@angular/core'; import { KeycloakService } from 'keycloak-angul ...

Problems arise when Twitter Typeahead, Knockout JS, and Twitter Bootstrap 3 are unable to cooperate harmoniously

I'm currently working on integrating the knockout-bootstrap custom binding for typeahead jQuery with Bootstrap 3, specifically to use it with Durandal 2.0. However, I'm facing some challenges in getting it to function correctly. The original bind ...

What strategies can be implemented to increase the number of backlinks?

<script type="text/javascript"> $(document).ready(function() { var linkas = $("#button").attr("value"); $('#button').click(function(){ $.get(linkas, function(data){ $(' ...

when the window is scrolled a certain amount, use jQuery to scroll back in the opposite direction

Using jQuery for Background Image Position: $(window).scroll(function() { var scroll = $(window).scrollTop(); if (scroll >= 50) { $(".class").addClass("bgposi"); // $(".top").addClass("fixd"); // $(".logo").addClass ...

Give instructions on how to sequence multiple ajax requests in a specific order

I have a collection of div elements in jQuery. Each div requires two separate ajax calls, where the result of the first call is used in the second call. These ajax requests each take about one second to complete. If I loop through the divs like this: $di ...

Transfer the value of a JavaScript variable to paste it into a fresh tab on Google Chrome

I have come across some examples where users can copy HTML text to the clipboard. However, I am working on something more dynamic. Here's what I'm trying to achieve: <button id="" ng-click="outputFolder()">Output Folder</button> $sc ...

Struggling to get a basic HTML form to function with JavaScript commands

In my form, there are two input fields and a button. Upon clicking the button, a JavaScript function is triggered which multiplies the values entered in the inputs. The result is then displayed in a <p> element and evaluated through an if else statem ...

What could be the reason for data-id coming back as undefined?

I'm having trouble figuring out why the code below isn't working for me. The console is showing 'undefined' for data-id. href='#detailsModal' class='btn btn-info btn-xs' data-toggle='modal' data-id='x ...

Top method for extracting mesh vertices in three.js

Being new to Three.js, I may not be approaching this in the most optimal way, I start by creating geometry like this: const geometry = new THREE.PlaneBufferGeometry(10,0); Next, I apply a rotation: geometry.applyMatrix( new THREE.Matrix4().makeRotation ...

Create artwork by drawing and adding text to an image before saving it

I am looking to enhance my website by adding a feature that allows users to draw on images hosted on the server. Additionally, I want users to have the ability to add text to the image and save their edits as a new picture. While it may seem like a simple ...

Showing formatted JSON in the view using ASP.NET MVC

Is there a method to modify JSON for presentation in the interface? I am looking for a way to automatically update my API documentation when adding new properties. It would be great if I could also apply CSS styling to certain elements. This feature is som ...

Maximizing the efficiency of a personalized hook that facilitates data sharing in React

I have developed a unique Custom Hook that looks like the following: import { useEffect, useState } from 'react'; import axios from 'axios'; const myCustomHook = () => { const [countries, setCountries] = useState([]); const [i ...

Conflict between Play framework and jQuery AJAX calls

Hi there, I have a simple issue, Currently, my application is utilizing Play framework 2.1, and at times I need to send multiple independent ajax requests using jQuery. For example: $.get('/url1', function(res){ ... }); $.get('/url2' ...