Class: Watir::Element::Scroll

Inherits:
Object
  • Object
show all
Defined in:
lib/watir-scroll/element/scroll.rb

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ Scroll

Returns a new instance of Scroll.



5
6
7
# File 'lib/watir-scroll/element/scroll.rb', line 5

def initialize(element)
  @element = element
end

Instance Method Details

#by(left, top) ⇒ Object

Scrolls by offset.

Parameters:

  • left (Fixnum)

    Horizontal offset

  • top (Fixnum)

    Vertical offset



41
42
43
44
# File 'lib/watir-scroll/element/scroll.rb', line 41

def by(left, top)
  @element.execute_script('window.scrollBy(arguments[0], arguments[1]);', Integer(left), Integer(top))
  self
end

#to(param = :top) ⇒ Object

Scrolls to element.

Parameters:

  • param (Symbol) (defaults to: :top)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/watir-scroll/element/scroll.rb', line 13

def to(param = :top)
  args = case param
         when :top, :start
           ['arguments[0].scrollIntoView();', @element]
         when :center
           script = <<-JS
             var bodyRect = document.body.getBoundingClientRect();
             var elementRect = arguments[0].getBoundingClientRect();
             var left = (elementRect.left - bodyRect.left) - (window.innerWidth / 2);
             var top = (elementRect.top - bodyRect.top) - (window.innerHeight / 2);
             window.scrollTo(left, top);
           JS
           [script, @element]
         when :bottom, :end
           ['arguments[0].scrollIntoView(false);', @element]
         else
           raise ArgumentError, "Don't know how to scroll element to: #{param}!"
         end
  @element.browser.execute_script(*args)

  self
end