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



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

def initialize(element)
  @element = element
end

Instance Method Details

#to(param = :top) ⇒ Object

Scrolls to element.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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 elementRect = arguments[0].getBoundingClientRect();
             var top = elementRect.top - (window.innerHeight / 2);
             var left = elementRect.left - (window.innerWidth / 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)
end