Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
is-in-viewport
Advanced tools
An ultra-light jQuery plugin that tells you if an element is in the viewport but with a twist.
An ultra-light jQuery plugin that tells you if the element is in the viewport, but with a twist.
Did you say demo (inclusive of tests)?
npm install --save is-in-viewport
You can then require('is-in-viewport')
or import 'is-in-viewport'
in your code.
It will automagically work with the bundler of your choice. If it breaks, please feel free to open an issue.
Example usage in an ES6/ES2015 module is shown in the examples/es6-example
folder.
Note: isInViewport
is a side-effecting module. It imports jquery
that you have installed and attaches itself on it.
As a consequence, isInViewport
requires jquery
to be installed as a peer dependency.
Your bundling will fail if jquery
isn't installed as is-in-viewport
import
s jquery
.
bower install isInViewport
or npm install --save is-in-viewport
)isInViewport.js
or isInViewport.min.js
and the respective sourcemap from the lib
folder to your folder containing your scriptsjQuery
$( '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. It can also be used with jquery's .is
.
$( 'div:in-viewport' ).css( 'background-color', 'red' );
// same as
var $div = $( 'div' );
if ( $div.is( ':in-viewport' ) ) {
$div.css( 'background-color', 'red' );
}
Both of the above will set the background-color
as red
for all div
s that are in the viewport.
in-viewport
pseudo-selector$( 'selector:in-viewport( tolerance[, viewport selector] )' )
This returns all the elements that are in the viewport while taking into account the tolerance
criterion.
Since it returns the element(s) it can thus be chained with other jQuery methods.
When a viewport selector is specified, it uses that to calculate if the element is in that viewport or not.
When a viewport selector is not specified, it defaults to window as the viewport.
The viewport selector is any valid jQuery selector.
tolerance
defaults to 0
viewport
defaults to window
//example 1
//the height of tolerance region is 100px from top of viewport
$( 'div:in-viewport( 100 )' ).css( 'background-color', 'red' );
//example 2
//the height of tolerance region is (viewport.height - 100px) from top of viewport
$( 'div:in-viewport( -100 )' ).css( 'background-color', 'green' );
//example 3
$('#viewport > div.box:in-viewport( 100, #viewport )').css( 'background-color', 'blue' )
.text( 'in viewport' );
Example 1 will set the background-color
as red
for all divs
that are in the viewport with a tolerance
of 100px
.
Example 2 will set the background-color
as green
for all divs
that are in the viewport with a tolerance
of viewport height - 100px
. This lets the user conveniently provide a tolerance
value closer to the viewport height without having to call $(viewport).height()
all the time.
Example 3 will set the background-color
as blue
and text
as in viewport
for all divs
that are in the custom viewport given by #viewport
and 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.
See the examples in the examples
directory for more clarity.
tolerance
is 0
or undefined
it is actually equal to tolerance: $(viewport).height()
and not 0
.This makes it easier for developers to have the whole viewport
available to them as a valid viewport
.
isInViewport
function$( 'selector' ).isInViewport({ tolerance: tolerance, viewport: viewport })
This returns all the elements that are in the viewport while taking into account the tolerance
criterion.
Since it returns the element(s) it can thus be chained with other jQuery methods.
When a viewport is specified, it uses that to calculate if the element is in that viewport or not.
When a viewport is not specified, it defaults to window as the viewport.
The viewport is a valid DOM element or jQuery wrapped DOM element, NOT a selector string.
tolerance
defaults to 0
viewport
defaults to window
//example 1
//the height of tolerance region is 100px from top of viewport
$( 'div' ).isInViewport({ tolerance: 100 }).css( 'background-color', 'red' );
//example 2
//the height of tolerance region is (viewport.height - 100px) from top of viewport
$( 'div' ).isInViewport({ tolerance: -100 }).css( 'background-color', 'green' );
//example 3
var $viewport = $('#viewport');
$viewport
.find('div.box')
.isInViewport({ tolerance: 100, viewport: $viewport })
.css( 'background-color', 'blue' )
.text( 'in viewport' );
Chrome, Firefox 3.5+, IE9+, Safari 5+, Opera 10.5+
:in-viewport
selector does support chaining.3.0.3
jQuery.expr.pseudos
when found since jQuery.expr[':']
is deprecated3.0.2
moduleId
, moduleName
, etc)3.0.1
3.0.0
$(el).do
methodimport 'is-in-viewport'
/require('is-in-viewport')
in your project (yay!)2.4.2
postInstall
script which was breaking builds2.4.1
2.4.0
as is-in-viewport
already exists on bower and isn't owned by me2.4.0
bower.json
to comply with new validationsis-in-viewport
2.3.1
2.3.0
isInViewport
with saner semantics. You can now pass options as JS objects to isInViewport
and hence can now do things like:
var $viewport = $(<viewport selector>);
$viewport
.find(<selector for elements>)
.isInViewport({ tolerance: 100, viewport: $viewport }) // <- passing the viewport jQuery object in directly
.css(color: 'red');
do
in favour of run
isInViewport
now uses Sizzle.selectors.createPseudo
2.2.5
2.2.4
2.2.3
2.2.2
npm
npm
2.2.1
2.2.0
.do
method with .run
since do
is a reserved word and errors out when used as a property in IE. To be on the safer side, use .run
to chain any arbitrary function or an array of functions.2.1.0
.do
method that lets the user chain any arbitrary function or an array of functions. Example://usage 1: pass a function
$( 'div:in-viewport' )
.do(function(){
console.log( this ); //will log the current jQuery element object it's being called on
})
.css( 'background-color', 'red' );
//usage 2: pass an array of functions
var fnArray = [
function(){ console.log("Fn 1: %o", this); },
function(){ console.log("Fn 2: %o", this); }
//or say another function that maybe adds
//elements to be tracked when in viewport
];
$( 'div:in-viewport' ).do(fnArray);
2.0.0
tolerance
values that are now relative to the viewport
height:in-viewport
selector i.e.,//removed
$( selector ).isInViewport( {"tolerance" :100, "debug": true} )
//current usage
$( 'selector:in-viewport( 100 )' )
debug
option because, lets be honest, no one really used it.1.1.1
bower
support.1.1.0
:in-viewport
selector as per joeframbach's suggestion.FAQs
An ultra-light jQuery plugin that tells you if an element is in the viewport but with a twist.
The npm package is-in-viewport receives a total of 0 weekly downloads. As such, is-in-viewport popularity was classified as not popular.
We found that is-in-viewport demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.