Results 1 to 7 of 7

Thread: movable layer script

  1. #1
    Join Date
    Jul 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default movable layer script

    can someone please design a script for me that can move an object about a page. for example...

    you click a button that says 'move right' and a layer moves 50 pizels right. you click it again and the layer once again moves 50 pixels right. the layer would be a picture (myimage.gif)

    is this possible?

  2. #2
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    here take a look at this:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function moveright(id, amount){
    var el = document.getElementById(id);
    var cp = el.offsetLeft;
    el.style.left = cp - amount + "px";
    }
    function moveleft(id, amount){
    var el = document.getElementById(id);
    var cp = el.offsetLeft;
    el.style.left = cp + amount + "px";
    }
    function moveup(id, amount){
    var el = document.getElementById(id);
    var cp = el.offsetTop;
    el.style.top = cp - amount + "px";
    }
    function movedown(id, amount){
    var el = document.getElementById(id);
    var cp = el.offsetTop;
    el.style.top = cp + amount + "px";
    }
    </script>
    </head>
    <body>
    <div id="yourimage" style="width: 100px;height: 100px;position:absolute;background-color: red;top:100px;"></div>
    <input type="button" onclick="moveright('yourimage', 50);" value="Move Right">
    <input type="button" onclick="moveleft('yourimage', 50);" value="Move Left">
    <input type="button" onclick="moveup('yourimage', 50);" value="Move Up">
    <input type="button" onclick="movedown('yourimage', 50);" value="Move Down">
    </body>
    </html>

  3. #3
    Join Date
    Jul 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    yes this works but how do i insert MY image if my image name is broomani.gif

  4. #4
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Change the :
    <div id="yourimage" style="width: 100px;height: 100px;position:absolute;background-color: red;top:100px;"></div>

    to
    <img src="broomani.gif" border="0" id="your_id"/>

    and the :
    Code:
    <input type="button" onclick="moveright('yourimage', 50);" value="Move Right">
    <input type="button" onclick="moveleft('yourimage', 50);" value="Move Left">
    <input type="button" onclick="moveup('yourimage', 50);" value="Move Up">
    <input type="button" onclick="movedown('yourimage', 50);" value="Move Down">

    to:
    Code:
    <input type="button" onclick="moveright('your_id', 50);" value="Move Right">
    <input type="button" onclick="moveleft('your_id', 50);" value="Move Left">
    <input type="button" onclick="moveup('your_id', 50);" value="Move Up">
    <input type="button" onclick="movedown('your_id', 50);" value="Move Down">
    Note: The part in red can be changed according to your settings.

  5. #5
    Join Date
    Jul 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks but this doesn't work. I tried your suggestion and many variations but i can't get it to work. If I try your suggestion the picture is inserted but it won't move.

  6. #6
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    By the way can you post your code after the modifications??

  7. #7
    Join Date
    Feb 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Soren Twilight View Post
    thanks but this doesn't work. I tried your suggestion and many variations but i can't get it to work. If I try your suggestion the picture is inserted but it won't move.
    Some trouble? Put your *.gif into div container:

    HTML Code:
    <div style="background-color: white;"><img src="sometrouble.gif"></img></div>
    this works very well!

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
  •