Solved

OnDrag Cursor

Posted on 2004-03-25
14
920 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
ID: 10684592
//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
ID: 10684595

<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
ID: 10684774
How about this:

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

0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 63

Expert Comment

by:Zvonko
ID: 10684834
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
ID: 10692999
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
ID: 10693908
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
ID: 10693912
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
 
LVL 1

Author Comment

by:jasons016
ID: 10696164
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
ID: 10696299
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
ID: 10696303
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
ID: 10696308
Please note: my method can move several images.
0
 
LVL 1

Author Comment

by:jasons016
ID: 10696372
Thank you both very much.
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10696411
Thanks for the points and the A. :)
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10696422
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

803 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