• Stars
    star
    696
  • Rank 65,018 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Get viewport dimensions, detect elements in the viewport, trust in <!DOCTYPE html>

verge

verge is a compact 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. (npm: verge)

API

Accuracy

  • Requires <!DOCTYPE html>
  • verge's viewport methods represent the CSS viewport: the @media (width) and @media (height) breakpoints.
  • At certain zooms the viewport dimensions given by verge may be 1px off the expected @media value due to native rounding. For greater precision (at a slight speed tradeoff) consider actual.

.viewportW()

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

.viewportH()

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

.viewport()

Get both CSS viewport dimensions as an object with width and height properties.

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

The .viewportW() syntax is slightly faster.

.inViewport()

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

verge.inViewport(elem) // true if elem is in the current viewport
verge.inViewport(elem, 100) // true if elem is in the current viewport or within 100px of it
verge.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

verge.inViewport(elem) === verge.inX(elem) && verge.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.

verge.inX(elem) // true if elem is in same x-axis as the viewport (exact)
verge.inX(elem, 100) // true if elem is in same x-axis as the viewport or within 100px of it
verge.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.

verge.inY(elem) // true if elem is in same y-axis as the viewport (exact)
verge.inY(elem, 100) // true if elem is in same y-axis as the viewport or within 100px of it
verge.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.)

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

.scrollY()

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

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

.mq()

.mq(mediaQueryString)

Test if a media query is active.

verge.mq('(min-color:2)') // -> boolean
verge.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.

verge.rectangle(element) // rectangle object
verge.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.

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

Integrate

jQuery

jQuery.extend(verge)

ender

ender build verge

Contribute

Contribute by making edits in ./src or reporting issues.

npm install
npm test

More Repositories

1

response.js

Responsive design toolkit
JavaScript
801
star
2

actual

Determine actual CSS media query breakpoints via JavaScript
JavaScript
50
star
3

res

Device resolution detection module
JavaScript
39
star
4

eol

JavaScript newline character converter
JavaScript
37
star
5

ux

Website user preference API
JavaScript
30
star
6

flexboxes

CSS flexbox framework with pure flexbox grid ability
HTML
27
star
7

slash

PHP URI and path utilities.
PHP
17
star
8

dope

Cross-browser HTML5 dataset module
JavaScript
15
star
9

draf

double RAF JavaScript module
HTML
13
star
10

css3base

base stylesheets collection
ApacheConf
12
star
11

tunes

HTML5 audio and video playlists
JavaScript
11
star
12

numerologia

dark themed numerology calculator
HTML
9
star
13

energy

Universal JavaScript event emitter
JavaScript
8
star
14

fm

JavaScript function modulation
JavaScript
7
star
15

cxn

Network connection JavaScript utility module
JavaScript
7
star
16

chemical

JavaScript chemical API
JavaScript
7
star
17

symlinked

Got symlinked packages?
JavaScript
7
star
18

cargo

Web storage API JavaScript module
JavaScript
7
star
19

said

Run CLI commands and return output as string
JavaScript
6
star
20

vibe

CSS classes for the masses
JavaScript
6
star
21

ssv

Space Separated Values. JavaScript library for spaced data. Fun and fast for classnames and beyond πŸ’•
JavaScript
6
star
22

subtag

Language tag parser for JavaScript and Node.js
JavaScript
5
star
23

fit

Base responsive CSS for HTML5 elements
CSS
5
star
24

rave

PHP that you can dance to
PHP
5
star
25

traits

Opensource PHP traits
PHP
5
star
26

action

Extensible HTML5 WordPress parent theme
PHP
5
star
27

speculative

prerender, preconnect, preload, prefetch, dns-prefetch
JavaScript
5
star
28

bypass

WP plugin that allows you to write entry markup in files rather than in the WP editor
PHP
4
star
29

scan

querySelectorAll selector engine
JavaScript
4
star
30

elo

Lo-fi JavaScript events module.
JavaScript
4
star
31

lap

JavaScript performance testing library
JavaScript
4
star
32

spacing

Opensource functional CSS spacing for web spaces
CSS
4
star
33

dime

Cross-browser JavaScript dimensions module
JavaScript
4
star
34

a11y

accessibility resources
4
star
35

mark

JavaScript object tracking system
JavaScript
4
star
36

arro

left/right arrow keyboard shortcuts
JavaScript
3
star
37

downtime

Do background work during browser idle time.
JavaScript
3
star
38

aok

Simple JavaScript test suite API
JavaScript
3
star
39

universal

JavaScript universal module template
JavaScript
3
star
40

templace

HTML template tag progressive content loader with minimal JavaScript
HTML
3
star
41

flish

JavaScript flash message module
JavaScript
3
star
42

resilient-css

Resilient CSS talk slides
HTML
3
star
43

dj

JavaScript module integration API
JavaScript
3
star
44

plays

Detect playable audio and video filetypes
JavaScript
3
star
45

emoter

JavaScript event emitter for state changes
JavaScript
3
star
46

taos

Toggle areas or styles via HTML5 data attributes
JavaScript
3
star
47

map-file

Map a file into another file in Node.js
JavaScript
3
star
48

varam

change CSS var via URL param with this 77 line library called varam
JavaScript
3
star
49

all-filler

Fill function for arrays or strings based on array.fill
JavaScript
2
star
50

path

PHP filesystem utilities
PHP
2
star
51

access

WP plugin to control user access via taxonomy terms
PHP
2
star
52

image_src

WP plugin for image_src
PHP
2
star
53

focux

Match :focus-within for better focus experience
HTML
2
star
54

cueing

JavaScript utility to cue, seek, or cycle items
JavaScript
2
star
55

edit-file

Edit a file in Node.js
JavaScript
2
star
56

blood

JavaScript object life
JavaScript
2
star
57

chromosome

JSON-based template engine available for PHP or node.js
PHP
2
star
58

wtb

what the box dimension parser
JavaScript
2
star
59

yearly

Year utility to test or bump year in a string. Useful for keeping license files current.
JavaScript
2
star
60

avant

Cross-browser JavaScript events module
JavaScript
2
star
61

annex

DOM insertion module
JavaScript
2
star
62

aaronirwin.com

Aaron Irwin's website
CSS
2
star
63

freeform.css

Functional expressive CSS for rapid prototyping
CSS
2
star
64

border.css

Opensource functional CSS border classes
HTML
2
star
65

modder

JavaScript: mod your modules
JavaScript
2
star
66

phat

PHP HTML utility module
PHP
2
star
67

often

Async recursive JavaScript timers
JavaScript
2
star
68

sharp.css

CSS sharp corners
CSS
2
star
69

oi

cross-browser DOM ready module
JavaScript
2
star
70

format

free template for you all blank uno
HTML
2
star
71

skate

JavaScript app state manager
JavaScript
2
star
72

ryanve.github.io

Featured Github Pages
HTML
1
star
73

resume

Heartbearts
HTML
1
star
74

4everstatic

This talk is for creators like you.
1
star
75

media

HTML
1
star
76

admit

JavaScript array admit/ban/check utility module
JavaScript
1
star
77

daw

work in progress
1
star
78

uxe

@ryanve UX Engineer rΓ©sumΓ©
HTML
1
star
79

togg

Angular UI toggle behavior directive
HTML
1
star
80

404

404 page examples
1
star
81

overflow.css

CSS overflow classes
CSS
1
star
82

eros

rose
HTML
1
star
83

.github

CSS
1
star
84

ryan

sea
HTML
1
star
85

glimpse

moon
CSS
1
star
86

echo

natal
HTML
1
star
87

woas

opensource phrase guessing game made with static HTML and fresh CSS
HTML
1
star
88

cue

CSS
1
star
89

sappho

frog awash in butterfly tones
1
star
90

thunder

dub speaking
CSS
1
star
91

refresh

compassion
1
star
92

halfaxa

halfaxa era style like a comeback kid
HTML
1
star
93

moon

0ΒΊ aquarius
1
star
94

22

1
star
95

routine

moon
1
star
96

pages

graf
HTML
1
star
97

natal

natal chart
HTML
1
star
98

tx.css

Opensource CSS text utility classes
HTML
1
star
99

area.css

CSS functional width and height module
CSS
1
star
100

curious

Typechecking module
JavaScript
1
star