Make objects on your web page draggable

Discussion in 'HTML & Website Design' started by cc2365, Nov 28, 2008.

  1. #1
    Want to make objects, such as tables, images, texts or something else on you web page draggable? Well, I found a script which can do it for you.

    
    <style type="text/css">
    
    .drag{
    position:relative;
    cursor:hand;
    z-index: 100;
    }
    
    </style>
    
    <script type="text/javascript">
    
    /***********************************************
    * Drag and Drop Script: © Dynamic Drive (http://www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit http://www.dynamicdrive.com/ for this script and 100s more.
    ***********************************************/
    
    var dragobject={
    z: 0, x: 0, y: 0, offsetx : null, offsety : null, targetobj : null, dragapproved : 0,
    initialize:function(){
    document.onmousedown=this.drag
    document.onmouseup=function(){this.dragapproved=0}
    },
    drag:function(e){
    var evtobj=window.event? window.event : e
    this.targetobj=window.event? event.srcElement : e.target
    if (this.targetobj.className=="drag"){
    this.dragapproved=1
    if (isNaN(parseInt(this.targetobj.style.left))){this.targetobj.style.left=0}
    if (isNaN(parseInt(this.targetobj.style.top))){this.targetobj.style.top=0}
    this.offsetx=parseInt(this.targetobj.style.left)
    this.offsety=parseInt(this.targetobj.style.top)
    this.x=evtobj.clientX
    this.y=evtobj.clientY
    if (evtobj.preventDefault)
    evtobj.preventDefault()
    document.onmousemove=dragobject.moveit
    }
    },
    moveit:function(e){
    var evtobj=window.event? window.event : e
    if (this.dragapproved==1){
    this.targetobj.style.left=this.offsetx+evtobj.clientX-this.x+"px"
    this.targetobj.style.top=this.offsety+evtobj.clientY-this.y+"px"
    return false
    }
    }
    }
    
    dragobject.initialize()
    
    </script>
    HTML:
    After put them between <head> and </head>, give the objects that should be draggable a "drag" class. For example:

    Draggable text:<font class="drag"></font>
    Draggable table:<table class="drag">...</table>
    Draggable image:<img src="..." class="drag" />

    Enjoy! ;)
     
    cc2365, Nov 28, 2008 IP
  2. gonnadiscover

    gonnadiscover Peon

    Messages:
    184
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i will try it!
     
    gonnadiscover, Nov 29, 2008 IP
  3. aman11dhanpat

    aman11dhanpat Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    try some flash
     
    aman11dhanpat, Nov 29, 2008 IP