• Stars
    star
    111
  • Rank 303,533 (Top 7 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated about 11 years ago

Reviews

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

Repository Details

jQuery plugin for on-scroll detecting whether the element entered or left the viewport.

Espy

jQuery plugin for on-scroll detecting whether the element entered or left the viewport.

Any time you scroll, Espy checks whether the element entered or left the trigger area (window viewport by default) specified in the options. On each change in state, callback will be fired with all necessary diagnostics of the element position relative to the trigger area.

What is considered a trigger area is configurable. By default, the trigger area is the visible part of the screen.

Dependencies

  • jQuery 1.7+

Compatibility

So simple, it should work everywhere. Briefly tested in IE6+, Chrome, FF, Opera, Safari.

Changelog

Espy upholds the Semantic Versioning Specification, and currently is in Beta.

API documentation

$(selector).espy( callback [, options ] );

callback

Function to be executed when element enters or leaves the trigger area.

Function context - this - is the concerned element.

Receives these arguments:

  • entered Boolean True when element entered the viewport, false when it left.
  • state String Specifying in which direction is the element in regard to the trigger area. Can be: up & down in vertical mode, left & right in horizontal mode, and inside when covering, or contained in the trigger area.

Example:

$(selector).espy(function (entered, state) {
	if (entered) {
		// element entered the viewport
		// state === 'inside'
	} else {
		// element left the viewport
		if (state === 'up') {
			// element is now above the trigger area
		} else if (state === 'down') {
			// element is now below the trigger area
		}
	}
});

[ options ]

context: Node window Element scrolling context.

horizontal: Bool 0 Enables the horizontal scrolling mode.

offset: Int 0 Offset from start of the context (top in vertical, left in horizontal) specifying the trigger area. Can be integer for offset in pixels, % for offset relative to the size of the context, positive number for offset from start, and negative for offset from end.

size: Mixed 100% Size of the trigger area. Can be integer for size in pixels, or % string for size relative to the size of the context.

contain: Bool 0 By enabling this, the callback with entered=true will be called only when the whole element is completely contained within the trigger area, as opposed to just partially covering it. If the element is bigger than the trigger area itself, the callback with entered=true will never be fired.

Using the Espy class

jQuery.fn.espy (described above) is basically just a simple proxy for Espy class declared in jQuery.Espy namespace. You can use this class directly to get more power over the Espy functionality.

jQuery.Espy( context [, callback ] [, options ] );

Arguments:

context: Node Scrolling context.

callback: Function Global function to be called for each element that changes state. Receives the same arguments as callback documented above.

options: Object Global options for this object instance. Same properties as options defined above. Extends default options, and can be further overridden for each element. Has one extra property called delay, specifying the minimum delay in milliseconds that throttles the frequency of scroll events to once per delay. Default delay is 100.

Example:

Create an Espy object for window context:

var windowSpy = new jQuery.Espy(window, callback);

Methods


add

windowSpy.add( element [, callback ] [, options ] );

Add element(s) to spying list. Arguments:

element: Mixed Can be a selector, element node, or a jQuery object with one or multiple elements.

callback: Function Function to be executed on element state change. Same arguments as callbacks documented above. Doesn't override the main instance's callback, but is triggered along with it.

options: Object Object with options. Same properties as options defined above.

Examples:

windowSpy.add('#element'); // jQuery selector
windowSpy.add(document.getElementById('element')); // Direct element node
windowSpy.add(jQuery('.elements')); // Multiple elements in jQuery object

Note!: When you add an item multiple times, you won't produce duplicates, you will just override it's callback and options.


reload

windowSpy.reload( element );

Reload element(s)'s dimensions. Call this on element that has changed it's position or size. Arguments:

element: Mixed Can be a selector, element node, or a jQuery object with one or multiple elements.


remove

windowSpy.remove( element );

Remove element(s) from spying list. Arguments:

element: Mixed Can be a selector, element node, or a jQuery object with one or multiple elements.


destroy

windowSpy.destroy();

Destroy Espy object. Removes registered events, clears spying list, ...

Contributing

Contributions are welcome! But please:

Credits

More Repositories

1

sly

JavaScript library for one-directional scrolling with item based navigation support.
JavaScript
2,880
star
2

motio

Small JavaScript library for sprite based animations and panning.
JavaScript
752
star
3

fpsmeter

Sexy, fast, and themable FPS meter.
JavaScript
572
star
4

uosc

Minimalist cursor proximity based UI for MPV player.
Lua
226
star
5

tooltip

JavaScript Tooltip component.
JavaScript
190
star
6

laravel-console

In-browser console for Laravel PHP framework.
CSS
171
star
7

imagesloaded

A small helper that triggers a callback after all the selected/child images have been loaded.
JavaScript
99
star
8

tooltips

Tooltips for DOM elements.
JavaScript
40
star
9

loremImages

jQuery plugin for populating elements with random images
JavaScript
38
star
10

grunt-bumpup

Update the version, date, and other properties in JSON files while preserving indentation style.
JavaScript
35
star
11

grunt-gcc

Grunt task for minifying JavaScript files with Google Closure Compiler.
JavaScript
18
star
12

grunt-tagrelease

Commit the changes and tag the last commit with a version from provided JSON file.
JavaScript
17
star
13

volley

jQuery plugin for dividing and filtering elements based on their visual position.
JavaScript
16
star
14

remove-trailing-separator

Removes all separators from the end of a string.
JavaScript
11
star
15

e

Simple element creation / templating with SVG support.
JavaScript
9
star
16

utilus

Utilities fof Stylus.
CSS
9
star
17

position

Component that returns element's position box object relative to documentElement.
JavaScript
9
star
18

fs-extended

Node.js module that extends the native `fs` with convenient methods.
JavaScript
5
star
19

grunt-checkrepo

Grunt task to check the state of repository.
JavaScript
4
star
20

raft

requestAnimationFrame Throttle - throttle a function to be executed at most once per animation frame.
JavaScript
4
star
21

definer

Easier and less verbose wrapper for Object.defineProperties.
JavaScript
3
star
22

node-gcc

Node wrapper for Google Closure Compiler.
JavaScript
3
star
23

event

Event binding component with support for legacy browsers.
JavaScript
2
star
24

constructor-apply

Apply an arguments array to object constructor.
JavaScript
1
star
25

lab

Collection of my web experiments.
JavaScript
1
star
26

darsain.github.com

Personal website.
CSS
1
star
27

isarraylike

Check if value is an array-like object.
JavaScript
1
star
28

iswindow

Checks whether value is a window object.
JavaScript
1
star