Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:52:53+00:00 2026-05-27T14:52:53+00:00

How to prevent draggable child elements from dragging over each other in absolute position?

  • 0

How to prevent draggable child elements from dragging over each other in absolute position?
Something like:

if( ($("#firstChild").position().left) >= ($("#secondChild").position().left) )
{
    $(this).draggable({ disabled: true });
}

but this only disables dragabble when stop dragging, the goal is to prevent from overdragging in some way…Or with Droppable???

enter image description here

Any ideas for CASE 2?

EDIT:

Also, this is maximum for child1 towards child2 , so no overlap occurs, but child1 can push child2 on right, and child2 can push child1 on left, important thing is that no overlap ocurrs!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:52:53+00:00Added an answer on May 27, 2026 at 2:52 pm

    Some notes before / in order to reach the optimal solution:

    • You have declared and initialised the allBetween variablw, but not using it. I suggest to remove it.
    • At your #buttonOne event listener, you’re using inconsistent values: 429 and 424: if($("#firstInput").val() > 429 )$("#firstInput").val(424);
      1. You’re using the #firstInput (and #thirdInput) selector multiple times without caching a reference.
      2. You’re duplicating the functions for each button. A better, less error-prone solution is to create a method to build these methods.

    Final code

    The optimisations have been explained previously. I will not elaborate them, since that’s not the topic of the question. I have explained the function logic of the requested behavior in the scripts comments (below).

    $(document).ready(function() {
        // Helper to easily bind related events to the button.
        function createButtonClickEvent(sel_selector, input_selector, left_limit, right_limit) {
            return function(){
                var $sel = $(sel_selector),
                    $input = $(input_selector),
                    val = $input.val();
                if (val > right_limit) input.val(val = right_limit);
                else if (val < left_limit) input.val(val = left_limit);
                $sel.css('left', val + "px");
    
                var $allElems = $("#second").children(".single"),
                     $between = $allElems.inRangeX("#selFirst", "#selSecond");
                $allElems.removeClass("ui-selected");
                $between.addClass("ui-selected");
            }
        }
           //setValue 1
        $("#buttonOne").click(createButtonClickEvent("#selFirst", "#firstInput", 0, 429));    
        //setValue2
        $("#buttonTwo").click(createButtonClickEvent("#selSecond", "#thirdInput", 0, 429));
    
    
        //graph values
        var valuesG = [],
            $elements = $();
        for (i = 0; i < 144; i++) {
            valuesG[i] = Math.floor(Math.random() * (30 - 20 + 1) + 10);
            $elements = $elements.add($("<div class='single'>")
                                 .css('height', valuesG[i])
                                 .css('margin-top', 30 - valuesG[i]));
        }
        $elements.appendTo($("#second"));
        $("#second").children(".single").addClass("ui-selected");
    
        //inRangeX (http://stackoverflow.com/a/8457155/938089)
        (function($) {
            $.fn.inRangeX = function(x1, x2) {
    
                if (typeof x1 == "string" && +x1 != x1 || x1 instanceof Element) {
                    x1 = $(x1);
                }
                if (typeof x2 == "string" && +x1 != x1 || x1 instanceof Element) {
                    x2 = $(x2);
                }
    
                if (x1 instanceof $) {
                    x1 = x1.offset().left;
                }
                if (x2 instanceof $) {
                    x2 = x2.offset().left;
                }
                x1 = +x1;
                x2 = +x2;
    
    
                if (x1 > x2) {
                    var x = x1;
                    x1 = x2;
                    x2 = x;
                }
                return this.filter(function() {
                    var $this = $(this),
                        offset = $this.offset(),
                        rightSide = offset.left - 5;
                    return offset.left >= x1 + 5 && rightSide <= x2;
                });
            }
        })(jQuery);
    
        //firstPositions
        var startFirst = $(".selector#selFirst").position().left;
        var startSecond = $(".selector#selSecond").position().left;
        $('input#firstInput').val(startFirst);
        $('input#thirdInput').val(startSecond);
    
        // *********** Actual requested code *********** //
        //first and second-Picker
        var $selFirst = $("#selFirst"),
            $selSecond = $("#selSecond"),
            cachedWidth = $selFirst.outerWidth();
    
        function drag_function(event, ui){
            var $firstRightBorder = $selFirst.position().left + cachedWidth,
                 $secondLeft = $selSecond.position().left,
                 diff = $firstRightBorder - $secondLeft;
            /*
             * The logic is as follows:
             * dif < 0 if selFirst and selSecond do not overlap
             * dif = 0 if they're next to each other
             * dif > 0 if they overlap each other
             * To fix this (reminder: if they overlap, dif is negative):
             * If current == #selFirst,
             *    left = left + dif
             * else (if current == #selSecond),
             *    left = left - dif
             */
            if (diff > 0) {
                var currentLeft = parseFloat($(this).css("left"));
                if (this.id == "selSecond") diff = -diff;
                ui.position.left = currentLeft - diff;
                ui.helper.css("left", currentLeft - diff);
            }
            var $allElems = $("#second").children(".single"),
                $between = $allElems.inRangeX("#selFirst", "#selSecond");
            $("#firstInput").val($("#selFirst").position().left);
            $("#thirdInput").val($("#selSecond").position().left);
            $allElems.removeClass("ui-selected");
            $between.addClass("ui-selected");
            var allBetween = $('.ui-selected');
        }
    
        $("#selFirst, #selSecond").draggable({
            containment: 'parent',
            axis: 'x',
            drag: drag_function,
            stop: drag_function
        });
    }); //eof
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To prevent impatient users from clicking on a link to a webstart application too
How do I prevent vim from replacing spaces with tabs when autoindent is on?
Is it possible to prevent an asp.net Hyperlink control from linking, i.e. so that
Does TCP/IP prevent multiple copies of the same packet from reaching the destination? Or
How do I prevent my users from accessing directly pages meant for ajax calls
How can I prevent a user from resizing GridViewColumns withing a ListView control?
how do i prevent expanding the jquery blockui dialog box? i thought draggable: false?
When finish dragging a HTML5 draggable element, the element will revert back to its
I'm building a widget which allows user to copy items from one list over
I have elements on the page which are draggable with jQuery. Do these elements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.