Sha256: 130822d0eb1bf512c66530a76b48129e5c9b1d9699e127799ee1a91232adf1a2

Contents?: true

Size: 996 Bytes

Versions: 3

Compression:

Stored size: 996 Bytes

Contents

module Browser; class Window

class Scroll
  def initialize(window)
    @window = window
    @native = window.to_n
  end

  if Browser.supports? 'Window.scroll'
    def position
      %x{
        var doc  = #@native.document,
            root = doc.documentElement,
            body = doc.body;

        var x = root.scrollLeft || body.scrollLeft,
            y = root.scrollTop  || body.scrollTop;
      }

      Position.new(`x`, `y`)
    end
  elsif Browser.supports? 'Window.pageOffset'
    def position
      Position.new(`#@native.pageXOffset`, `#@native.pageYOffset`)
    end
  else
    def position
      raise NotImplementedError, 'window scroll unsupported'
    end
  end

  def x
    position.x
  end

  def y
    position.y
  end

  def to(what)
    x   = what[:x] || self.x
    y   = what[:y] || self.y

    `#@native.scrollTo(#{x}, #{y})`

    self
  end

  def by(what)
    x = what[:x] || 0
    y = what[:y] || 0

    `#@native.scrollBy(#{x}, #{y})`

    self
  end
end

end; end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
diamonds-0.1.5 lib/diamonds/opal/browser/window/scroll.rb
opal-browser-0.2.0 opal/browser/window/scroll.rb
opal-browser-0.2.0.beta1 opal/browser/window/scroll.rb