Adjust the size of divs using JQuery UI's draggable feature

Looking for a way to make two divs share 100% of their parent's width, with a dynamic resizing feature in between them? Check out my solution here: http://jsfiddle.net/aRQ7a/

However, I'm facing an issue where when I decrease the size of the previous div, the alignment of the slider goes off. How can I resolve this? Below is my code:

CSS:

* {
    margin: 0px;
    padding: 0px;
}
div {
    display: inline-flex;
    height: 300px;
    padding: 0px;
    margin: 0px;
}
#part1 {
    width: 47%;
    background-color: red;
}
#part2 {
    width: 47%;
    background-color: green;
}
.divider {
    width: 5%;
    background-color: blue;
}

HTML:

<body>
    <div id="part1"></div>
    <div class="divider"></div>
    <div id="part2"></div>
</body>

Javascript:

$(function () {
    var p1 = parseInt($("#part1").css("width"));
    var p2 = parseInt($("#part2").css("width"));

    $(".divider").draggable({
        axis: "x",
        containment: "parent",
        scroll: false,
        start: function(){
            p1 = parseInt($("#part1").css("width"));
            p2 = parseInt($("#part2").css("width"));
        },
        drag: function (event,  ui) {
            var a = parseInt($(this).css("left"));
            $(this).prev().css("width", p1 +  a);
            $(this).next().css("width", p2 -  a);            
        }
    });
});

Answer №1

Take a look at the following code snippet:

http://jsfiddle.net/aRQ7a/3/

This piece of JavaScript:

$(function () {
    var p1 = parseInt($("#part1").css("width"));
    var p2 = parseInt($("#part2").css("width"));
    var fullWidth= $('body').width();
    var initialPos = parseInt($(".divider").css("left"));
    $(".divider").draggable({
        axis: "x",
        containment: "parent",
        scroll: false,
        drag: function () {
            var a = parseInt($(this).css("left"));

            $("#part1").css("width", a);
            $("#part2").css("width", fullWidth-a);

        }
    });
});

And this snippet of CSS:

* {
    margin: 0px;
    padding: 0px;
}
div {
    display: inline-block;
    height: 300px;
}
#part1 {
    position: absolute;
    width: 47%;
    background-color: red;
}
#part2 {
    position: absolute;
    right:0;
    width: 47%;
    background-color: green;
}
.divider {
    width: 5%;
    position:absolute;
    left: 47%;
    background-color: blue;
}

Answer №2

Here is a live demonstration: http://jsfiddle.net/aRQ7a/2/

The issue arose from both the mouse and #part1 affecting the position of the divider when dragged, causing it to move twice the distance dragged. By setting it to position absolute, we can isolate it from any influence except the mouse.

Positioning #part2 to right:0 allows us to focus on adjusting its width accurately.

$(function () {
    var p1 = parseInt($("#part1").width());
    var p2 = parseInt($("#part2").width());

    $(".divider").draggable({
        axis: "x",
        containment: "parent",
        scroll: false,
        drag: function () {
            var a = parseInt($(this).position().left);
            $("#part1").css({width:a});
            $("#part2").css({width:p1 + p2 -a });
        }
    });
});

CSS

* {
    margin: 0px;
    padding: 0px;
}
div {
    display: inline-block;
    height: 300px;
}
#part1 {
    position: absolute;
    left:0;
    width: 47%;
    background-color: red;
}
#part2 {
    position: absolute;
    right:0;
    width: 47%;
    background-color: green;
}
.divider {
    width: 5%;
    position:absolute;
    left: 47%;
    background-color: blue;
}

One other note: The divider is 5% width, but 2 x 47% equals 6%. Whether this is intentional or not, I cannot determine.

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

Jekyll adjusting the starting line of code snippet formatting

Currently, I'm in the process of setting up code highlighting for my blog using Jekyll and Pygments. However, I'm facing an issue where the first line of every code snippet seems to be slightly offset. To create a gutter effect, I'm utilizin ...

Arranging elements in a manner reminiscent of the tiles in Windows 8

Click here to view an example. I'm attempting to create a layout similar to Windows 8 tiles grid, where there are three types of tile sizes available: Normal = 1x1; Wide = 2x1; and Big = 2x2. My goal is to have these tiles displayed without any em ...

Unable to delete element from the given array

I have been working on removing instances of 'store.state.location.locations' from my locationData array that should no longer be there, but for some reason, they are persisting in the array even though I am no longer passing those instances. ta ...

How to enlarge the size of specific letters in HTML

I am looking to enhance the values of C and I. In addition, I am utilizing ::first-text{}. This is currently functioning well. Now, the question arises - how can I elevate the value of I. <p>Creating and Implementing</p> <center> < ...

Adding a detached element under certain conditions

When attempting to add a detached span based on a specific condition, I encountered the following situation. const arrow = d3 .create('span') .classed('arrow', true) .text('\u2192'); //d3.select('bod ...

Ensuring the Persistence of Column State in Material-UI DataGrid/DataGridPro when Adjusting Visibility Using Column Toolbar

We have integrated MUI DataGrid into our React project. Currently, I am exploring options to save the state of columns after toggling their visibility using the DataGrid toolbar column menu. After each re-render, the column setup returns to its default st ...

What is the best method to modify and access imported .css files within the WordPress style.css file?

I could use some assistance with a project involving minor edits to a WordPress website. I've hit a roadblock and need some help. The style.css file in WordPress doesn't have much CSS code, but instead imports files like this: /*Animate*/ @impo ...

Surprising use of template string value

After following a tutorial, I decided to create ProductScreen.js and Product.js. However, when implementing my code, I encountered some warnings. Can anyone help me identify the issue? product.js import React from 'react' import Rating from &apo ...

Issue with CSS3 gradient display in Firefox and IE, functioning correctly only in Chrome

I recently developed a text gradient class that looks great in Google Chrome but unfortunately doesn't display correctly in Firefox and Internet Explorer. In these browsers, there is a visible background gradient behind the text which is not the desir ...

The ideal way to markup a calendar: including a list of times, day containers, and events

Currently, I am creating markup for a one-day calendar display. The calendar consists of a list of times on the left side, ranging from 9am to 9pm, and the events for the day on the right side. I have used a <table> for the list of times, a <secti ...

"Enhance your website with powerful AJAX functionality using the October CMS

Can you help me troubleshoot this AJAX error I'm encountering when trying to call an AJAX action? The error message is indicating an 'Invalid handler name.' The correct format for the handler name should be "onEvent" I am attempting to u ...

What is the best way to handle multiple JSON data using jQuery?

Understanding how to utilize jquery for parsing simple json is a valuable skill. json { "Symbol": "AAL.L", "Name": "ANGLO AMERICAN", "Last": "3061.50", "Date": "7/26/2011", "Time": "11:35am", "Change": "+3 ...

React error: The module "react-router-dom" does not have a member named "useNavigate" available for export

I'm attempting to include useNavigate for use as outlined in the top answer here: react button onClick redirect page import { useNavigate } from "react-router-dom"; However, I am encountering this error: export 'useNavigate' (impo ...

Can <option> tags be arranged side by side using CSS?

Here is a form I am working with: <form action="file.php" method="GET"> <input type="hidden" name="page"> <select> <option>Text 1</option> <option>Text 2</option> <option>Text 3 ...

What is the best way to prompt users to submit comments with a popup textarea box?

Within my project, I have incorporated a dropdown list: <div class="dropdown"> <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Select subject <span class="caret"></span> </but ...

Export data table from HTML to Excel successfully implemented with C#

I am currently working on an Umbraco website and developing a custom plugin for the backend that allows users to export an Excel worksheet from an HTML table. In order to achieve this functionality, I am utilizing AngularJS along with a C# controller. Belo ...

Can Chrome Support Bookmarklets?

While attempting to craft a bookmarklet in Chrome using the console, I encountered the following error: Refused to load the script 'https://code.jquery.com/jquery-1.6.1.min.js' because it violates the following Content Security Policy directive: ...

Tips for locating elements based on the final portion of their identifier using jQuery

When using jQuery, I need to locate an element based on its id. The original id is: txtHistoricalPricesDate In a typical situation, I would use the following code to find it easily: var element = $('#txtHistoricalPricesDate'); However, due to ...

Firebase onCall function is executing properly, however, it is only delivering {data: null} as the

I am facing an issue with my cloud function where everything runs smoothly until I attempt to return a response back to the front-end. Despite successfully modifying databases, the final log points D and G still show {data:null} as the output. I have a fe ...

I must first log a variable using console.log, then execute a function on the same line, followed by logging the variable again

Essentially, I have a variable called c1 that is assigned a random hexadecimal value. After printing this to the console, I want to print another hex value without creating a new variable (because I'm feeling lazy). Instead, I intend to achieve this t ...