Solved

OnDrag Cursor

Posted on 2004-03-25
14
916 Views
Last Modified: 2008-02-07
I want to use the OnDrag event trigger on an object to where the cursor style during the drag is as I choose. Currently, when you drag an object the cursor style changes to "not-allowed". I would much rather it be "default" or perhaps "crosshair".

Can this be done in Internet Explorer 6? If so, How?
0
Comment
Question by:jasons016
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 33

Expert Comment

by:sajuks
Comment Utility
//Zvonkos script
<script>
function move() {
  thing.style.cursor='hand';
  thing.style.left = window.event.x;
  thing.style.top = window.event.y;
  return false
}
</script>

<img src="http://www.experts-exchange.com/images/greyLogo.jpg" style="position:absolute" id=thing ondragstart="return move()">
0
 
LVL 33

Expert Comment

by:sajuks
Comment Utility

<script>
function move() {
  thing.style.cursor="crosshair"; // use the style you prefer
  thing.style.left = window.event.x;
  thing.style.top = window.event.y;
  return false
}
</script>

<img src="http://www.experts-exchange.com/images/greyLogo.jpg" style="position:absolute" id=thing ondragstart="return move()">
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
How about this:

<img src="http://www.experts-exchange.com/images/greyLogo.jpg" id="img1" onDragStart="return false">

0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
And if you need the drag functionality, then use this:

<img src="http://www.experts-exchange.com/images/greyLogo.jpg" id="img1" onDragOver="return false" onDragStart="yourDrag()">

0
 
LVL 1

Author Comment

by:jasons016
Comment Utility
I thought of those things and tried them before I posted the question. You see, if I return false on OnDrag, OnDragOver, or OnDragStart, then the drag does not work. If I set the cursor to anything inside of any of those event functions, then Internet Explorer overrides it so that it is still 'not-allowed.'

My question stands.
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Show us your code or test this one:

<script>
function yourDragStart(){
  window.status = "Start."
  document.body.ondragover = yourDragOver;
}
function yourDragOver(){
  window.status = event.x+"  "+event.y;
  return true;
}
</script>
<body>
<img src="http://www.experts-exchange.com/images/greyLogo.jpg" id="img1" onDragOver="return yourDragOver()" onDragStart="yourDragStart()">
</body>




0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Sorry, check this:

<script>
function yourDragStart(){
  window.status = "Start."
  document.body.ondragover = yourDragOver;
}
function yourDragOver(){
  window.status = event.x+"  "+event.y;
  return false;
}
</script>
<body>
<img src="http://www.experts-exchange.com/images/greyLogo.jpg" id="img1" onDragOver="return yourDragOver()" onDragStart="yourDragStart()">
</body>

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:jasons016
Comment Utility
Returning false on OnDragOver makes the cursor not 'not-allowed', however, it does not solve my problem. I'd much rather the cursor be 'default' or 'crosshair'.

Here, I will show you what I have.

[clip from "script.js"]
function track_mouse()
{
      // The following statement does not change the cursor to 'hand'
      // document.all.bpix.style.cursor='hand';

      // The following statements (supposedly) work on JScript, but not IE
      // window.event.dataTransfer.effectAllowed="none";
      // window.event.dataTransfer.dropEffect="none";

      document.all.bpix.style.left=window.event.clientX-(document.all.bpix.width/2);
      document.all.bpix.style.top=window.event.clientY-(document.all.bpix.height/2);
      
      return true;
}
[/clip]

[clip from "index.html"]
<img src="blu_pix.jpg" style="position: absolute; cursor: default;" width="30" height="30" id="bpix" OnDrag="return track_mouse();" OnDragOver="return false;" />
[/clip]
0
 
LVL 12

Accepted Solution

by:
lil_puffball earned 500 total points
Comment Utility
Try this script:

<script>
var drag=false;
function track_mouse()
{
     // The following statements (supposedly) work on JScript, but not IE
     // window.event.dataTransfer.effectAllowed="none";
     // window.event.dataTransfer.dropEffect="none";

     document.all.bpix.style.left=window.event.clientX-(document.all.bpix.width/2);
     document.all.bpix.style.top=window.event.clientY-(document.all.bpix.height/2);
}
</script>




<body onmousemove="if(drag){track_mouse();}">

<img src="blu_pix.jpg" style="position: absolute; cursor: default;" width="30" height="30" id="bpix" onmousedown="drag=true;this.style.cursor='crosshair';" onmouseup="drag=false;this.style.cursor='default';" unselectable=on>
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Simmelar idea:


<script>
function lock_mouse(theImg){
     theImg.style.cursor='hand';
     theImg.follow = true;    
}
function track_mouse(theImg){
     if(theImg.follow){
       theImg.style.left=window.event.clientX-(theImg.width/2);
       theImg.style.top=window.event.clientY-(theImg.height/2);
     }
}
function release_mouse(theImg){
     theImg.style.cursor='crosshair';
     theImg.follow = false;    
}
</script>
<body>
<img src="http://www.experts-exchange.com/images/pe/143475.jpg" style="position: absolute; cursor: crosshair;" width="30" height="30" id="bpix" OnMouseDown="lock_mouse(this);" OnMouseMove="track_mouse(this);" onMouseUp="release_mouse(this)" onDragStart="return false" />
</body>

0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Please note: my method can move several images.
0
 
LVL 1

Author Comment

by:jasons016
Comment Utility
Thank you both very much.
0
 
LVL 12

Expert Comment

by:lil_puffball
Comment Utility
Thanks for the points and the A. :)
0
 
LVL 12

Expert Comment

by:lil_puffball
Comment Utility
By the way, to Zvonko:
It is better to put the onmousemove in the body tag. Because if you put the onmousemove in the img tag, and move the image very quickly, your mouse is no longer moving above the image, and the image won't be draggable anymore. I came across this problem while writing some other drag scripts a while ago. :)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now