Lizzaran Lizzaran - 1 year ago 96
HTML Question

JQuery-UI Draggable locked to the window

I created a really simple map and I wanted it to be dragable.

Here is the code:

It works, I can move the map around how I want. But I want it to be locked to the "window", so that there is never white space when it get dragged, I hope you understand what I mean.
I also had a look at the the overview from jquery-ui, but didn't find anything that I needed.

Answer Source

You need to constrain movement to a set of coordinates to make it work properly. Took a bit of messing with, but the containment constrains the element based on the top left corner of the element.

$(function() {
    var con = $('#container');
    var cw = con.width( ), ch = con.height( );
    var map = $('#map');
    var mw = map.width( ), mh = map.height( );
    var x1 = -mw + cw, y1 = -mh + ch;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download