Click here to Skip to main content
Licence CPOL
First Posted 29 Jul 2008
Views 11,214
Downloads 95
Bookmarked 20 times

Drag and Move an Item

By | 29 Jul 2008 | Article
You can drag and move an item in the page by mouse.

Introduction

1- In the style of your item (like img, div, etc...) make the position as absolute, for example:

<div style="position:absolute" id="move_id" onmousedown="mousedown()" onmouseup="mouseup()">

2- You should have a temp for describe the condition of your item. set the display as none to hide the temp. the value of this input describes the condition. The value 0 means that the item is not dragged. when your mouse is down over the item (onmousedown) this value will set by 1 to be ready for drag and move.

<input type="text" style="display:none" id="temp_id" value="0"> 

3- make the <body> like this:

<body onmousemove="mousemove();">

4- Now we can make the JavaScript functions:

<script language="javascript" type="text/javascript">
function mousedown()
{
    document.getElementById("temp_id").value = "1";
    
}
function mouseup()
{
    document.getElementById("temp_id").value = "0";
    document.getElementById("move_id").style.cursor = "default";
}
function mousemove()
{
    if (document.getElementById("temp_id").value == "1")
    {
        document.getElementById("move_id").style.top = event.clientY - 10;
        document.getElementById("move_id").style.left = event.clientX - 50;
        document.getElementById("move_id").style.cursor = "move";
    }
}
</script>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Reza Raza

Web Developer
Persian Gulf Web Services
Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member

-Graphic Designer
-Web Developer
-Web Administrator
-Programmer
-Teacher
-Litterateur
-Have more than 20 online websites

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generaldoesnt work in FF3 Pinmemberkingvegeta2:28 10 Jan '11  
GeneralDid't work in FF3 PinmemberMohammad Dayyan12:06 30 Jul '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 30 Jul 2008
Article Copyright 2008 by Reza Raza
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid