-
Notifications
You must be signed in to change notification settings - Fork 156
Home
An ultra-light jQuery plugin that tells you if the element is in the viewport, but with a twist. Did you say demo?
Current version: 1.1.0
- Added support for
:in-viewportselector as per joeframbach's suggestion.
- Download either
isInViewport.jsorisInViewport.min.js( orbower install isInViewport) - Add it after you include
jQuery - You're ready to go!
As of version 1.1.0, the :in-viewport selector is preferred way of using the plugin.
Syntax is as below:
$( 'selector:in-viewport' )When used as a selector it returns all the elements that match. Since it returns the element(s) it can thus be chained with other jQuery methods.
$( 'div:in-viewport' ).css( 'background-color', 'red' );This will set the background-color as red for all divs that are in the viewport.
$( selector ).isInViewport()This returns true if the element is in the viewport and false otherwise.
As of version 1.1.0, you can use the :in-viewport selector with the tolerance passed as a parameter to the selector. This removes the need to loop over a collection of elements and to test them all separately using .isInViewport() method. The syntax is given below:
$( 'selector:in-viewport( tolerance )' )$( 'div:in-viewport( 100 )' ).css( 'background-color', 'red' );This will set the background-color as red for all divs that are in the viewport with a tolerance of 100px.
With the advanced usage it becomes very easy to build things like menus with items that get auto-highlighted based on which section you are on, transition effects when an element comes into the viewport, etc.
- Note that the
:in-viewportselector does not support thedebugparameter. - If you need to use
debugthen use the method call syntax:
isInViewport( {"tolerance": 100, "debug": true} )
$( selector ).isInViewport( {"tolerance" :100, "debug": true} ).isInViewport() takes two options:
-
tolerancespecfies the upper limit inpixelsof when the element is deemed in viewport.
It defaults to0. -
debugenables logging to the console.
It defaults tofalse.
See the examples in the examples directory for more clarity.
Chrome, Firefox 3.0+, IE6+, Safari 4.0+, Opera 10.0+
-
:in-viewportselector does support chaining. -
.isInViewport()doesn't support chaining as it returns abooleanvalue. - To use with IE < 9 use jQuery <= 1.7.0