Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting Element's global/document coordinates in Dart (aka $('some').offset())

Tags:

dom

dart

Im trying to get the document(global) coordinates of an Element (DOM Element) in Dart, which in pure JavaScript I was able to get via jQuery with $('some_selector').offset().

Ive been looking through the Dart dart:html API's, but no luck so far.

any ideas? thanks

like image 223
deepblue Avatar asked Oct 06 '22 17:10

deepblue


1 Answers

There's no native function on Element to get that coordinates.

Here's a Dart's version of jQuery.offset() :

class Position {
  num left, top;
  Position(this.left, this.top);
}

Position offset(Element elem) {
  final docElem = document.documentElement;
  final box = elem.getBoundingClientRect();
  return new Position(box.left + window.pageXOffset - docElem.clientLeft,
                      box.top  + window.pageYOffset - docElem.clientTop);
}

An other solution is to use jQuery.offset() via js-interop :

Position jQueryOffset(Element elem) {
  return js.scoped((){
    final offset = js.context.jQuery(elem).offset();
    return new Position(offset.left, offset.top);
  });
}
like image 156
Alexandre Ardhuin Avatar answered Oct 10 '22 17:10

Alexandre Ardhuin