Class: Browser::DOM::Element::Offset

Inherits:
Object
  • Object
show all
Defined in:
opal/browser/dom/element/offset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



4
5
6
# File 'opal/browser/dom/element/offset.rb', line 4

def element
  @element
end

#xInteger

Returns:

  • (Integer)


18
19
20
# File 'opal/browser/dom/element/offset.rb', line 18

def x
  get.x
end

#yInteger

Returns:

  • (Integer)


28
29
30
# File 'opal/browser/dom/element/offset.rb', line 28

def y
  get.y
end

Instance Method Details

#getObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'opal/browser/dom/element/offset.rb', line 37

def get
  doc  = @element.document
  root = doc.root.to_n
  win  = doc.window.to_n

  %x{
    var box = #@native.getBoundingClientRect(),
        y   = box.top + (#{win}.pageYOffset || #{root}.scrollTop) - (#{root}.clientTop || 0),
        x   = box.left + (#{win}.pageXOffset || #{root}.scrollLeft) - (#{root}.clientLeft || 0);
  }

  Browser::Position.new(`x`, `y`)
end

#parentObject



12
13
14
# File 'opal/browser/dom/element/offset.rb', line 12

def parent
  DOM(`#@native.offsetParent || #{@element.document.root.to_n}`)
end

#set(*value) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'opal/browser/dom/element/offset.rb', line 65

def set(*value)
  position = @element.style![:position]

  if position == :static
    @element.style[:position] = :relative
  end

  offset = get
  top    = @element.style![:top].to_u
  left   = @element.style![:left].to_u

  if Browser::Position === value.first
    x, y = value.first.x, value.first.y
  elsif Hash === value.first
    x, y = value.first[:x], value.first[:y]
  else
    x, y = value
  end

  @element.style[:left] = (x.px - offset.x) + left if x
  @element.style[:top]  = (y.px - offset.y) + top  if y
end