Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Monitor MouseMove

Status
Not open for further replies.

Nefarious1

Programmer
Feb 24, 2014
13
GB
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..



 
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
 
I also believe you cannot use
Code:
document.addEventListener('mousemove', [highlight #FCE94F]myFunction(e)[/highlight]);

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top