Jump to content

drag and drop question


Matej

Recommended Posts

Hello , i found drag and drop tutorial but i didnt get one thing

here is the link - http://luke.breuer.com/tutorial/javascript-drag-and-drop-tutorial.aspx - (if you are too lazy to open it i can copy the code :D)

 

So back to my question

as first he defines

 var _startX = 0; 

than in mousedown invoking function he redefine it like this

_startX = e.clientX;
so now starX does not equal 0 but location of mouse in X line

and in mousemove invoking function he does this

_dragElement.style.left = (_offsetX + e.clientX - _startX) + 'px';

1)basicly _offsetX=event.target.style.left;

2)e.clientX = position of mouse in X line

3)_startX = is also position of mouse in Xline;

 

so that means that 1)500+2)1000-3)1000 = 500 , so it shouldnt even move

 

could someone please explain it? or if i got it wrong also explain it :D , thanks for answers

Link to comment
Share on other sites

When you move the mouse, the e.clientX here: _dragElement.style.left = (_offsetX + e.clientX - _startX) + 'px'; changes values resulting in _dragElement.style.left value changing which drags/moves the element.

Link to comment
Share on other sites

  • 3 weeks later...

Change document.onmousemove=ahoj(); to document.onmousemove=ahoj; and see if that works.

oh:) , thanks for fast answers , but why did it work with "ahoj" and not with "ahoj()"?

//

http://jsfiddle.net/3kksmag5/4/ why does it works only first time? i mean , when "mouseup" and try to drag it again , it doesnt work

Edited by Matej
Link to comment
Share on other sites

In the first case you're telling it the name of the function it has to use when the event happens, in the second case you're actually running the function right on that line and assigning whatever value the function returns to the "onmousemove" property.

Link to comment
Share on other sites

In the first case you're telling it the name of the function it has to use when the event happens, in the second case you're actually running the function right on that line and assigning whatever value the function returns to the "onmousemove" property.

Well if first time i tell it , to use ahoj onmousemove , and second time just invoking ahoj with click (if i got it right) , why is chaning of innerHTML working like it should but div aint movin?

Link to comment
Share on other sites

Look at this update; http://jsfiddle.net/3kksmag5/10/ See the changes. Use offsetLeft and offsetTop to get left and top values. When setting the left and top in the ahoj function, instead of the left and top margins, set the left and top positions: y.style.left, y.style.top. Also since you're moving the red box div around, make sure to set position to absolute in CSS.

 

Well if first time i tell it , to use ahoj onmousemove , and second time just invoking ahoj with click (if i got it right) , why is chaning of innerHTML working like it should but div aint movin?

 

Hopefully this answers your question.

Edited by Don E
Link to comment
Share on other sites

The offsetTop and offsetLeft properties are numbers indicating the actual distance in pixels from the top and left of the parent.

 

style.left and style.top only have values if they were set earlier using the element's style attribute or Javascript. They are strings that include a number and a unit, such as "30px".

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...