PDA

View Full Version : Dragable Elements - Is it possible to remember positions where images are left?



Naughty Paul
01-19-2006, 10:08 AM
I reckon your Dragable Elements script is great. I am keen to use it to create a website where visitors can drag various images to create their own sketches. Is there any way to make the positions of images be remembered if a visitor goes back to the page?

Twey
01-19-2006, 05:06 PM
I pondered this for a bit, then came up with this hideous code that will calmly slaughter any other cookies you use on the page:

function saveImgs() {
var imgs = new Array();
for(var i=0;i<document.images.length;i++)
if(document.images[i].className == "drag")
imgs.push(document.images[i]);
document.cookie = "";
for(var i=0;i<imgs.length;i++)
document.cookie += i + ":" + imgs[i].style.left + "," + imgs[i].style.top + ";";
}

function restoreImgs() {
if(document.cookie != "") {
var buns = new Array(),
lefts = new Array(),
tops = new Array(),
ig = 0,
strs = document.cookie.split(";");
strs.replace(/(\d+):(\d+),(\d+)/g, function(match, bun, left, top) {
if(!document.images[bun]) return;
buns[ig] = bun;
lefts[ig] = left;
tops[ig] = top;
ig++;
return match;
});
var e = document.images;
for(var i=0;i<buns.length;i++) {
var g = e[buns[i]].style;
g.top = tops[i];
g.left = lefts[i];
}
}
}Untested.