Results 1 to 2 of 2

Thread: Dragable Elements - Is it possible to remember positions where images are left?

  1. #1
    Join Date
    Jan 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dragable Elements - Is it possible to remember positions where images are left?

    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?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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:
    Code:
    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •