Mark Forums Read
  #1  
Old 06-04-2009, 07:43 AM
rose19 rose19 is offline
Junior Member
 
Join Date: Jun 2009
Posts: 12
rose19 is on a distinguished road
Default Javascript Dragging???

Hello, I'm practicing on this Javascript dragging function.
Here's a mock up site I have for it: http://chibuki.comze.com/test/index.html (the css file)
Hello, I'm practicing on this Javascript dragging function.
Here's a mock up site I have for it: http://chibuki.comze.com/test/index.html (the css file)

So far I got it to work, but how can I make it so that those puzzle pieces can "snap" into the big box? Let's say, when I drag it about 15 pixels away from the "destination#" div, the puzzle piece will snap in and it can't be moved anymore. Can anyone help please?

This is my current script:
Line number On/Off
Code: Select all
Quote:
1. var OnTop = 5;
2. var Draggable = new Array("token1","token2","token3","token4");
3. var DragObj = null;
4. var DeltaX = 0;
5. var DeltaY = 0;
6. function Grab(evt) {
7. var evt = (evt) ? evt : ((window.event) ? window.event : null);
8. var objectID = (evt.target) ? evt.target.id : ((evt.scrElement) ? evt.scrElement.id : null);
9. var isDraggable = false;
10. for(i=0;i<Draggable.length;i++)
11. {
12. if(Draggable[i]==objectID)
13. {
14. isDraggable = true;
15. }
16. }
17. if(isDraggable)
18. {
19. DragObj = document.getElementById(objectID)
20. DragObj.style.zIndex = ++OnTop;
21. DeltaX = evt.clientX - DragObj.offsetLeft;
22. DeltaY = evt.clientY - DragObj.offsetTop;
23. document.onmousemove = Drag;
24. document.onmouseup = LetGo;
25. }
26. }
27.
28. function Drag(evt) {
29. if(DragObj == null)
30. {
31. return;
32. }
33. var evt = (evt) ? evt : ((window.event) ? window.event : null);
34. DragObj.style.left = evt.clientX - DeltaX + "px";
35. DragObj.style.top = evt.clientY - DeltaY + "px";
36. }
37.
38. function LetGo(evt) {
39. DragObj = null;
40. document.onmousemove = null;
41. document.onmouseup = null;
42. }
43.
44. window.onload = function() {
45. document.onmousedown = Grab;
46. }
47.
__________________
web design in Birmingham
Reply With Quote
 #Add to Ads's Reputation  
OldSponsored Ads
Ads AdsPromoter is online
Member
 
Join Date: LongTime
Posts: 1100
Ads is on a distinguished road
Default New Sponsored Ads



This message will go away once you are registered. Also, by registering, you will have access to all post topics, communicate privately with other members (PM), respond to polls, upload graphics, and access other special features! Registration is fast, simple and absolutely free so please Click Here to join our Web Hosting community today!
Reply With Quote
  #2  
Old 06-22-2009, 10:32 AM
gkumar gkumar is offline
Senior Member
 
Join Date: Jun 2009
Posts: 203
gkumar is on a distinguished road
Default Javascript Dragging???

JavaScript excels at modifying the DOM of a Web page, but we usually only do simple things with it, such as creating image rollovers, making tabs, etc. This article is going to show you how to create items on your page that you can drag and drop.

There are several reasons you might want to encorporate this drag and drop ability into your Web pages. One of the simplest reasons is to reorganize Data. As an example, you might want to have a queue of items that your users can reorganize. Instead of putting an input or select box next to each item to represent its order, you could make the entire group of items draggable. Or perhaps you want to have a navigation window on your site that can be moved around by your users. Then there's always the simplest reason: because you can!

There's really not much involved with dragging an item around on your Web page. First we have to know where the mouse cursor is, second we need to know when the user clicks on an item so that we know we should be dragging it, and finally we need to move the item.
Reply With Quote
 #Add to Ads's Reputation  
OldSponsored Ads
Ads AdsPromoter is online
Member
 
Join Date: LongTime
Posts: 1100
Ads is on a distinguished road
Default New Sponsored Ads



This message will go away once you are registered. Also, by registering, you will have access to all post topics, communicate privately with other members (PM), respond to polls, upload graphics, and access other special features! Registration is fast, simple and absolutely free so please Click Here to join our Web Hosting community today!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 04:34 PM.


Powered by vBulletin Version 3.6.1
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 2.4.0