views:

118

answers:

2

Hi,

I have many draggable div's on my page

$('#bibo').draggable({
    addClasses: false,
    containment: 'window',
    zIndex: '999',
    stack: '.tko.sto'
});

I save the new position on dragstop and put the new top and left values into the SQL.

Ok, everything works great. But when I visit my Page with the Laptop (small screen) some div are out of the screen and a scrollbar is shown.

Is it possible to fix this if the screen is too small?

Thanks in advance!

A: 

A simple solution would be to calculate relative sizes and adjust accordingly. For example, instead of saving the direct x,y coordinate, maybe calculate the x,y percentage based on the screen size and save that.

Jordan S. Jones
Hi Jordan, it works with percentage but it is not the exaclty position. The problem is that you cant save (for example) this position top:15,154789%; left:55,11987%;. Css only allow 1 digit after the comma :(
Peter
Right, what I am suggesting, is converting the pixel offsets to a percentage and store that. Then when you load the page, convert the percentage back to pixels.
Jordan S. Jones
You have an error in reasoning ;) .. My hope was that maybe jquery ui has a function for this. But i found nothing :(
Peter
+1  A: 

You could check the $(window).width() and do a little math to see if you need to move your div.

var windowWidth = $(window).width();
var left = $("div").position().left;
var width = $("div").width();
if (windowWidth < left + width) {                
    var newLeft = left - ((left + width) - windowWidth);
    $("div").css({ left: newLeft });
}

You may also acheive this with the jqueryui position utility:

fehays
Hi fehays, the problem is that the $(window).width() value is not the real visible window size. My solution to get the right width and height was to create a 1px div and put it into the middle. Than you can get the top and left value (multiply 2). But that is not a nice solution. I think the jq-ui position is exactly what i want, but i dont understand it :( ... can you create a little jsfiddle example for me? Here is a little example from my code -> http://jsfiddle.net/L5Wbt/7/
Peter
Here's an example. http://jsfiddle.net/L5Wbt/11/ you could add the repositioning to a $(window).resize(). basically, the "my" is the location of the element that is being moved. "at" specifies what part of the target element to position "my" to. and "of" is which element you are going to position to. I'm not sure if that explanation was any better than the jqueryui doc but i tried :)
fehays
@Peter - If this resolved your issue be sure to award the bounty as well, it's a separate action from accepting a correct answer :)
Nick Craver