INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Monitor MouseMove

Monitor MouseMove

(OP)
Hi -
I have recently been looking for some code that will allow me to monitor the mouse position for touch-screen devices. I found some code that worked, but am having problems parsing the example I found in to something more modular for my purposes. What I cant understand is why the second code example wont work ? I am guessing its something to do with scope, but any tips would be much appreciated.

===========================================================================================
CODE WORKS - BUT very "INLINE" I was trying to pull the code out in to distinct functions
===========================================================================================

CODE

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
  <script src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    </head>
    <body>
        <div id="status">x, y</div>
<script>
$( document ).ready(

function() 
{

document.addEventListener('touchmove', function(e) {
        e.preventDefault();
        $('#status').html('x='+event.touches[0].pageX + '  y= ' + event.touches[0].pageY);
    }, false);
}

);


</script>
       
    </body>
</html> 
================================================================================================
SIMILAR CODE _ BUT USING DISTINCT FUNCTIONS - THIS CODE DOES NOT WORK!
===============================================================================================

CODE

<!DOCTYPE html>
<html>
    <head>
         <link rel="stylesheet" href="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
  <script src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
        
    </head>
<body>

<div id="status">x, y</div>

<script>
document.addEventListener('mousemove', myFunction(e));

function myFunction(e) 
{
      e.preventDefault();
        $('#status').html('x='+event.touches[0].pageX + '  y= ' + event.touches[0].pageY);
}
</script>

</body>
</html> 

I dont think its due to the second attempt running outside of the "document Ready" function, but I am not sure. Anyway, any guidence would be much appreciated.

Cheers..



RE: Monitor MouseMove

Why are you using "mousemove" rather than "touchmove" in the second example?

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Monitor MouseMove

I also believe you cannot use

CODE

document.addEventListener('mousemove', myFunction(e)); 

When using UDF's as arguments, if they have parentheses, they are evaluated immediately.

To get round this you have to wrap them in an anonymous function.

CODE

document.addEventListener('mousemove', function(e){myFunction(e);}); 

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Monitor MouseMove

(OP)
OK Cheers 1DMF - very helpful.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close