Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draggable inside a particular div

I am working on a project in which I have used draggable event. Now the thing is that draggable() event worked fine for me... But as I wanted to store my position I have used following javascript. This one is also working quit good... But the thing is that it will work for whole screen. I just wanted it to run on a specific parent div only by using this code. So is it possible ?

function $(el){
                return document.getElementById(el);
            }
            var tzdragg = function(){
                return {
                    move : function(divid,xpos,ypos){
                        var a = $(divid);
                        $(divid).style.left = xpos + 'px';
                        $(divid).style.top = ypos + 'px';
                    },
                    startMoving : function(evt){
                        evt = evt || window.event;
                        var posX = evt.clientX,
                            posY = evt.clientY,
                            a = $('elem'),
                        divTop = a.style.top,
                        divLeft = a.style.left;
                        divTop = divTop.replace('px','');
                        divLeft = divLeft.replace('px','');
                        var diffX = posX - divLeft,
                            diffY = posY - divTop;
                        document.onmousemove = function(evt){
                            evt = evt || window.event;
                            var posX = evt.clientX,
                                posY = evt.clientY,
                                aX = posX - diffX,
                                aY = posY - diffY;
                            tzdragg.move('elem',aX,aY);
                        }
                    },
                    stopMoving : function(){
                        var a = document.createElement('script');
                        document.onmousemove = function(){}
                    },
                }
            }();

if possible then please help me to sort this out...

i have made jsfiddle as well....

Fiddle

like image 945
joker Avatar asked Feb 05 '14 05:02

joker


People also ask

How do I make a div element draggable?

To make an object draggable set draggable=true on that element. Just about anything can be drag-enabled: images, files, links, files, or any markup on your page.

Can any HTML element be draggable?

In HTML, any element can be dragged and dropped.

How do you get the position of the draggable element?

You can use the drag event: $('#dragThis'). draggable({ drag: function() { var offset = $(this). offset(); var xPos = offset.


1 Answers

check the parent div boundary value for draggable element,If it satisfies the condition ,allow to move the element.

Try this code:

DEMO

 var boun=document.getElementById("parent").offsetWidth-document.getElementById("elem").offsetWidth;

 if((aX>0)&&(aX<boun)&&(aY>0)&&(aY<boun))
     tzdragg.move('elem',aX,aY);
like image 119
Manoj Avatar answered Sep 28 '22 05:09

Manoj