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 7833145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:35:00+00:00 2026-06-02T12:35:00+00:00

I am working on how to show guides when moving boxes like it is

  • 0

I am working on how to show guides when moving boxes like it is in Google Docs Drawing. I would prefer an open-source code or any type of guide before starting writing my own.

  1. I do not need drag-n-drop across multiple browser windows so i don’t need HTML5 Drag-n-Drop.
  2. Also i am using jquery-ui-draggable for boxes.

enter image description here

  • 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-06-02T12:35:02+00:00Added an answer on June 2, 2026 at 12:35 pm

    jquery ui has this already built in, see this demo:
    http://jqueryui.com/demos/draggable/#snap-to

    if you insist on the guidlines you would maybe have to fork jqueryui or look at the source and see if you can extend it.


    alternatively you could just add your own snapping-functions on top of jQuery ui, i’ve played with it for a bit,
    and while it doesn’t seem like fun at least it also doesn’t seem to be very hard.

    you can view the example on jsfiddle: http://jsfiddle.net/x7uMh/103/
    update: this works ~ jQuery 1.9 + jQueryUI 1.9. it breaks in newest jquery+ui. couldn’t be bothered to see what exactly the problem is, typically its only minor problems though.
    just in case that site ever goes down, here’s the code:

    css

    body{
        font-family: courier new, courier; 
        font-size: 12px; 
    }
    
    .draggable{
        border: 1px solid #ccc; 
        display: inline-block; 
        cursor: move;
        position: absolute;         
    }
    
    .guide{
        display: none; 
        position: absolute; 
        left: 0; 
        top: 0; 
    }
    
    #guide-h{
        border-top: 1px dashed #55f; 
        width: 100%; 
    }
    
    #guide-v{
        border-left: 1px dashed #55f; 
        height: 100%; 
    }
    ​
    

    html

    <div class="draggable">drag me!</div>
    <div class="draggable">you can drag me too, if you like</div>
    <div class="draggable">hep hep</div>
    
    <div id="guide-h" class="guide"></div>
    <div id="guide-v" class="guide"></div>
    ​
    

    javascript (make sure to include jquery + jquery ui)

    var MIN_DISTANCE = 10; // minimum distance to "snap" to a guide
    var guides = []; // no guides available ... 
    var innerOffsetX, innerOffsetY; // we'll use those during drag ... 
    
    $( ".draggable" ).draggable({
        start: function( event, ui ) {
            guides = $.map( $( ".draggable" ).not( this ), computeGuidesForElement );
            innerOffsetX = event.originalEvent.offsetX;
            innerOffsetY = event.originalEvent.offsetY;
        }, 
        drag: function( event, ui ){
            // iterate all guides, remember the closest h and v guides
            var guideV, guideH, distV = MIN_DISTANCE+1, distH = MIN_DISTANCE+1, offsetV, offsetH; 
            var chosenGuides = { top: { dist: MIN_DISTANCE+1 }, left: { dist: MIN_DISTANCE+1 } }; 
            var $t = $(this); 
            var pos = { top: event.originalEvent.pageY - innerOffsetY, left: event.originalEvent.pageX - innerOffsetX }; 
            var w = $t.outerWidth() - 1; 
            var h = $t.outerHeight() - 1; 
            var elemGuides = computeGuidesForElement( null, pos, w, h ); 
            $.each( guides, function( i, guide ){
                $.each( elemGuides, function( i, elemGuide ){
                    if( guide.type == elemGuide.type ){
                        var prop = guide.type == "h"? "top":"left"; 
                        var d = Math.abs( elemGuide[prop] - guide[prop] ); 
                        if( d < chosenGuides[prop].dist ){
                            chosenGuides[prop].dist = d; 
                            chosenGuides[prop].offset = elemGuide[prop] - pos[prop]; 
                            chosenGuides[prop].guide = guide; 
                        }
                    }
                } ); 
            } );
    
            if( chosenGuides.top.dist <= MIN_DISTANCE ){
                $( "#guide-h" ).css( "top", chosenGuides.top.guide.top ).show(); 
                ui.position.top = chosenGuides.top.guide.top - chosenGuides.top.offset;
            }
            else{
                $( "#guide-h" ).hide(); 
                ui.position.top = pos.top; 
            }
    
            if( chosenGuides.left.dist <= MIN_DISTANCE ){
                $( "#guide-v" ).css( "left", chosenGuides.left.guide.left ).show(); 
                ui.position.left = chosenGuides.left.guide.left - chosenGuides.left.offset; 
            }
            else{
                $( "#guide-v" ).hide(); 
                ui.position.left = pos.left; 
            }
        }, 
        stop: function( event, ui ){
            $( "#guide-v, #guide-h" ).hide(); 
        }
    });
    
    
    function computeGuidesForElement( elem, pos, w, h ){
        if( elem != null ){
            var $t = $(elem); 
            pos = $t.offset(); 
            w = $t.outerWidth() - 1; 
            h = $t.outerHeight() - 1; 
        }
    
        return [
            { type: "h", left: pos.left, top: pos.top }, 
            { type: "h", left: pos.left, top: pos.top + h }, 
            { type: "v", left: pos.left, top: pos.top }, 
            { type: "v", left: pos.left + w, top: pos.top },
            // you can add _any_ other guides here as well (e.g. a guide 10 pixels to the left of an element)
            { type: "h", left: pos.left, top: pos.top + h/2 },
            { type: "v", left: pos.left + w/2, top: pos.top } 
        ]; 
    }
    
    ​
    

    hope that helps,
    best, hansi.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working a on a Google Web Toolkit project. I want to show a word
Is it possible to show the working copy when doing searches in TortoiseHG? I've
I have a show/hide toggle working well in multiple instances (thanks to help here
I was working with datalist and datalits will show many images in page. so
I'm working on a small problem where I'm trying to show/hide a panel based
I am working on a website on which i show restaurants according to either
I have a div show hide function which is working fine. when I click
I'm working on sample webapp that I'm supposed to show to my CTO. It's
I am working on div which is hidden and has a show button. Once
i am working on tabactivity. i wanna show my tabwidget below the tabcontent(framelayout). i

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.