I am trying to get relative mouse coordinates in an element in React using jQuery.
My code does not seem to work, and there are no console errors.
the code:
index.html
<html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <link rel="stylesheet" type="text/css" href="stylesheets.css" > <script src="https://code.jquery.com/jquery-3.1.0.js"></script> <script src="ja.js" type="text/javascript"></script> <title>Where Waldo</title> </head> <div id="root"></div> </body> </html>
ja.js (jQuery function)
jQuery(function($) { var x,y; $("#waldo1").mousemove(function(event) { var offset = $(this).offset(); x = event.pageX- offset.left; y = event.pageY- offset.top; $("#coords").html("(X: "+x+", Y: "+y+")"); }); });
component
import React, { Component } from 'react' import Timer from './timer' class Level1 extends Component { render () { return ( <div> <div id="gameBoard"> <img id="waldo1" src={require('../images/waldo1(1).jpg')} alt="waldo"/> </div> <h2 id="coords"></h2> <Timer start={Date.now()}/> </div> ) // return } // render } //component export default Level1
I heard jQuery doesn't play well while reacting. Is my syntax correct or is there a better way?
Thanks.
javascript jquery reactjs
Quesofat
source share