Javascript Drag & Drop.

Core

Member
Jan 23, 2010
3,120
195
0
Milky Way/Local Cluster/Local Sol/Earth
You can use this method on your blog to make any item drag-able.

can you see this bunch of code

<div>
<img src=".jpg" alt="earth" title="earth" id='divStyle' />
</div>

so insert your elements which need to be dragged instead image.
and make its name divStyle.

Copy paste onto note pad and save it with .html extension.
that's all.


Don't steal my codes without my permission.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
    <head>
        <style type='text/css'>
        #divStyle{
        position:absolute;
        //background-color:red;
        //width:50px;
        //border:solid 2px;
        //border-color:black;
        cursor:pointer;
        }
        </style>    
        
        <script type='text/javascript'>

        document.onmousedown = mouseDown;
        document.onmouseup = mouseUp;
    
        function mouseDown(event)
        {
        //check the browser
            if(window.event)//for IE browser
            {
                //get the event trigged object
                ObjEvent = event.srcElement;
            }else if(event.which)
            {
                //for FF/Safari/Opera/ browser
                ObjEvent = event.target;
            }


            //check it got the appropriate object
            if(ObjEvent.id = "divStyle" )
            {
                //then get the left top positions of the object
                objLeft = parseInt(ObjEvent.style.left);
                objTop = parseInt(ObjEvent.style.top);
                //get the mouse pointer's positions on the screen but on the object
                cltX = event.clientX;
                cltY = event.clientY;
                //then call for the move event when it's needed
                document.onmousemove = mouseMove;
            }
        return false;

        }

        function mouseMove(event)
        {
            //get the current location of the moving
            currentX = event.clientX;
            currentY = event.clientY;
            
            //set the new location of the object which will be redraw in each time when items is being moved
            ObjEvent.style.left = objLeft+(currentX - cltX )+ "px";
            ObjEvent.style.top = objTop+(currentY - cltY)+ "px";
        return false;
        }

        function mouseUp(event)
        {
            document.onmousemove = null;//cancel the moving
        return false;
        }


        document.onmousedown = mouseDown;
        document.onmouseup = mouseUp;


        </script>
    </head>
<body>

    <div>
        <img src=".jpg" alt="earth" title="earth"  id='divStyle' />
    </div>

    <script type='text/javascript'>
        //set the initial location
        ObjEvent = document.getElementById("divStyle");
        ObjEvent.style.left = "100px";
        ObjEvent.style.top = "150px";
    </script>

</body>

</html>
By CORE/ 2010/05/20 Originally was written
Made on Elakiri.com on 2011/08/01
 
Last edited:

Core

Member
Jan 23, 2010
3,120
195
0
Milky Way/Local Cluster/Local Sol/Earth
♥-Aurora-♥;10793617 said:
Good work bro! :cool: Keep posting..

:rofl: haha no I won't do that in here. I feel I just waste my time in here.
I have been posting threads on various SL sites since begin of 2010 but it seems no one cares it. people don't want to learn programming. they only care graphic Designing or already made software. pointless. I got only few replies. I stop writing programming tutorials instead I thought dedicate my time for my open source projects. you can find me on sourceforge if u want just ask the link.but in these days I am really busy cause I am still in 2nd year.