Class: Browser::DOM::Element::Position

Inherits:
Object
  • Object
show all
Defined in:
lib/diamonds/opal/browser/dom/element/position.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ Position

Returns a new instance of Position.



7
8
9
10
# File 'lib/diamonds/opal/browser/dom/element/position.rb', line 7

def initialize(element)
  @element = element
  @native  = element.to_n
end

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



4
5
6
# File 'lib/diamonds/opal/browser/dom/element/position.rb', line 4

def element
  @element
end

#xInteger (readonly)

Returns the position of the element on the x axis.

Returns:

  • (Integer)

    the position of the element on the x axis



35
36
37
# File 'lib/diamonds/opal/browser/dom/element/position.rb', line 35

def x
  get.x
end

#yInteger (readonly)

Returns the position of the element on the y axis.

Returns:

  • (Integer)

    the position of the element on the y axis



41
42
43
# File 'lib/diamonds/opal/browser/dom/element/position.rb', line 41

def y
  get.y
end

Instance Method Details

#getObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/diamonds/opal/browser/dom/element/position.rb', line 13

def get
  offset        = @element.offset
  position      = offset.get
  parent        = offset.parent
  parent_offset = Browser::Position.new(0, 0)

  if @element.style[:position] == :fixed
    unless parent =~ :html
      parent_offset = parent.offset
    end

    parent_offset.x += parent.style['border-top-width'].to_i
    parent_offset.y += parent.style['border-left-width'].to_i
  end

  Browser::Position.new(
    position.x - parent_offset.x - @element.style['margin-left'].to_i,
    position.y - parent_offset.y - @element.style['margin-top'].to_i)
end