Skip to content
/ verge Public
forked from ryanve/verge

Get viewport dimensions, detect elements in the viewport

Notifications You must be signed in to change notification settings

siulam/verge

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

verge is a compact (<1k gzipped) set of cross-browser viewport utilities written in native JavaScript. It includes the ability to detect if an element is in the current viewport. It works as a standalone module, an ender module, or as a jQuery plugin.

API (1.8)

In standalone usage, methods are available on the verge namespace. (verge.aspect(), …)

The docs below use $ to denote verge or a host lib.


$.viewportW()

$.viewportW()  // -> viewport width in pixels

$.viewportH()

$.viewportH()  // -> viewport height in pixels

$.viewport()

Get an object with width and height properties—an alternate syntax to get both viewport dimensions.

$.viewportW() === $.viewport().width  // always true
$.viewportH() === $.viewport().height // always true

The .viewportW() syntax is faster.

Mitigate the speed difference with local vars.
var current = $.viewport()
  , width = current.width
  , height = current.height

$.inViewport()

Test if any part of an element (or the first element in a matched set) is in the current viewport. Returns boolean.

$.inViewport(elem)       // true if elem is in the current viewport
$.inViewport(elem, 100)  // true if elem is in the current viewport or within 100px of it
$.inViewport(elem, -100) // true if elem is in the current viewport and not within 99px of the edge

Performance tip for sites that only scroll along 1 axis

$.inViewport(elem) === $.inX(elem) && $.inY(elem) // always true
Substitute inViewport with: inY on vertical sites, inX on horizontal ones.
  • On pages without horizontal scroll, inX is always true.
  • On pages without vertical scroll, inY is always true.
  • If the viewport width is >= the document width, then inX is always true.

$.inX()

Test if any part of an element (or the first element in a matched set) is in the same x-axis section as the viewport. Returns boolean.

$.inX(elem)       // true if elem is in same x-axis as the viewport (exact)
$.inX(elem, 100)  // true if elem is in same x-axis as the viewport or within 100px of it
$.inX(elem, -100) // true if elem in is the viewport and not within 99px of the edge

$.inY()

Test if any part of an element (or the first element in a matched set) is in the same y-axis section as the viewport. Returns boolean.

$.inY(elem)       // true if elem is in same y-axis as the viewport (exact)
$.inY(elem, 100)  // true if elem is in same y-axis as the viewport or within 100px of it
$.inY(elem, -100) // true if elem in is the viewport and not within 99px of the edge

$.scrollX()

Get the horizontal scroll position in pixels. (Like window.scrollX, but cross-browser.)

$.scrollX()  // -> horizontal pixels scrolled

$.scrollY()

Get the vertical scroll position in pixels. (Like window.scrollY, but cross-browser.)

$.scrollY()  // -> vertical pixels scrolled

$.mq()

$.mq(mediaQueryString)

Test if a media query is active.

$.mq('(min-color:2)') // -> boolean
$.mq('tv')            // -> boolean

$.rectangle()

$.rectangle(element, cushion?)

Get an a object containing the properties top, bottom, left, right, width, and height with respect to the top-left corner of the current viewport, and with an optional cushion amount. Its return is like that of the native getBoundingClientRect.

The optional cushion parameter is an amount of pixels to act as a cushion around the element. If none is provided then it defaults to 0 and the rectangle will match the native rectangle. If a cushion is specified, the properties are adjusted according to the cushion amount. If the cushion is positive the rectangle will represent an area that is larger that the actual element. If the cushion is negative then the rectangle will represent an area that is smaller that the actual element.

$.rectangle(element)       // rectangle object
$.rectangle(element, 100)  // rectangle object adjusted by 100 pixels

$.aspect()

$.aspect(object?)

Get the aspect ratio of the viewport or of an object with width/height properties.

$.aspect()         // -> viewport aspect ratio
$.aspect(element)  // -> element aspect ratio
$.aspect(screen)   // -> device aspect ratio
1 < $.aspect()     // => landscape orientation

Integrate

jQuery.extend(verge)
$ ender build verge

Developers

Contribute by making edits in /src or reporting issues.

$ npm install
$ grunt jshint:src

Fund

Fund development with tips to @ryanve =)

Copyright (C) 2012 by Ryan Van Etten

About

Get viewport dimensions, detect elements in the viewport

Resources

Stars

Watchers

Forks

Packages

No packages published