Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
190
rated 0 times [  194] [ 4]  / answers: 1 / hits: 17018  / 11 Years ago, mon, september 2, 2013, 12:00:00

I would like to get mouse coordinates relative to parent or any other element in the DOM other than this but I keep getting



Uncaught TypeError: Object [object Array] has no method 'getBoundingClientRect' d3.v3.min.js:1
H d3.v3.min.js:1
vo.mouse d3.v3.min.js:3
(anonymous function) index.html:291
(anonymous function)


My code :



.on(mouseup, function(d){
var th = d3.select( this );

var coordinates = [0, 0];
coordinates = d3.mouse( d3.select(body) );
console.log( coordinates[1] );
console.log( window );
//th.attr(cy, d3.mouse),
//d.newY = th.attr(cy);
console.log(d);
});


As far as I have noticed I can only get mouse coordinates relative to element that I have attached .on(mouseup, ...) event listener.



Is there a way to get those coordinates relative to other element in the DOM ?


More From » d3.js

 Answers
26

You can use d3.event.pageX and d3.event.pageY. An example where I use this:



.on(mouseover, function(d){
hover.transition().duration(200).style(opacity, .9);
hover.html(new Date(d.creationDate)+: +d.reactionTime).style(left, d3.event.pageX+px).style(top, (d3.event.pageY - 28)+px);
});

[#75961] Friday, August 30, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shane

Total Points: 239
Total Questions: 91
Total Answers: 114

Location: Faroe Islands
Member since Tue, Jul 7, 2020
4 Years ago
;