Skip to content

Instantly share code, notes, and snippets.

Created Oct 3, 2017
What would you like to do?
Perle viewport-checker.js
The MIT License (MIT)
Copyright (c) 2014 Dirk Groenen
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
Modified: replaced $scrollElem with window.pageXYOffset to fix Chrome/Safari issues 2017-10-02
$.fn.viewportChecker = function(useroptions){
// Define options and extend with user
var options = {
classToAdd: 'visible',
classToRemove : 'invisible',
classToAddForFullView : 'full-visible',
removeClassAfterAnimation: false,
offset: 100,
repeat: false,
invertBottomOffset: true,
callbackFunction: function(elem, action){},
scrollHorizontal: false,
scrollBox: window
$.extend(options, useroptions);
// Cache the given element and height of the browser
var $elem = this,
boxSize = {height: $(options.scrollBox).height(), width: $(options.scrollBox).width()}
* Main method that checks the elements and adds or removes the class(es)
this.checkElements = function(){
var viewportStart, viewportEnd;
// Set some vars to check with
if (!options.scrollHorizontal){
viewportStart = window.pageYOffset;
viewportEnd = (viewportStart + boxSize.height);
viewportStart = window.pageXOffset;
viewportEnd = (viewportStart + boxSize.width);
// Loop through all given dom elements
var $obj = $(this),
objOptions = {},
attrOptions = {};
// Get any individual attribution data
if ($'vp-add-class'))
attrOptions.classToAdd = $'vp-add-class');
if ($'vp-remove-class'))
attrOptions.classToRemove = $'vp-remove-class');
if ($'vp-add-class-full-view'))
attrOptions.classToAddForFullView = $'vp-add-class-full-view');
if ($'vp-keep-add-class'))
attrOptions.removeClassAfterAnimation = $'vp-remove-after-animation');
if ($'vp-offset'))
attrOptions.offset = $'vp-offset');
if ($'vp-repeat'))
attrOptions.repeat = $'vp-repeat');
if ($'vp-scrollHorizontal'))
attrOptions.scrollHorizontal = $'vp-scrollHorizontal');
if ($'vp-invertBottomOffset'))
attrOptions.scrollHorizontal = $'vp-invertBottomOffset');
// Extend objOptions with data attributes and default options
$.extend(objOptions, options);
$.extend(objOptions, attrOptions);
// If class already exists; quit
if ($'vp-animated') && !objOptions.repeat){
// Check if the offset is percentage based
if (String(objOptions.offset).indexOf("%") > 0)
objOptions.offset = (parseInt(objOptions.offset) / 100) * boxSize.height;
// Get the raw start and end positions
var rawStart = (!objOptions.scrollHorizontal) ? $obj.offset().top : $obj.offset().left,
rawEnd = (!objOptions.scrollHorizontal) ? rawStart + $obj.height() : rawStart + $obj.width();
// Add the defined offset
var elemStart = Math.round( rawStart ) + objOptions.offset,
elemEnd = (!objOptions.scrollHorizontal) ? elemStart + $obj.height() : elemStart + $obj.width();
if (objOptions.invertBottomOffset)
elemEnd -= (objOptions.offset * 2);
// Add class if in viewport
if ((elemStart < viewportEnd) && (elemEnd > viewportStart)){
// Remove class
// Do the callback function. Callback wil send the jQuery object as parameter
objOptions.callbackFunction($obj, "add");
// Check if full element is in view
if (rawEnd <= viewportEnd && rawStart >= viewportStart)
// Set element as already animated
$'vp-animated', true);
if (objOptions.removeClassAfterAnimation) {
$'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
// Remove class if not in viewport and repeat is true
} else if ($obj.hasClass(objOptions.classToAdd) && (objOptions.repeat)){
$obj.removeClass(objOptions.classToAdd + " " + objOptions.classToAddForFullView);
// Do the callback function.
objOptions.callbackFunction($obj, "remove");
// Remove already-animated-flag
$'vp-animated', false);
* Binding the correct event listener is still a tricky thing.
* People have expierenced sloppy scrolling when both scroll and touch
* events are added, but to make sure devices with both scroll and touch
* are handles too we always have to add the window.scroll event
* @see
* @see
// Select the correct events
if( 'ontouchstart' in window || 'onmsgesturechange' in window ){
// Device with touchscreen
$(document).bind("touchmove MSPointerMove pointermove", this.checkElements);
// Always load on window load
$(options.scrollBox).bind("load scroll", this.checkElements);
// On resize change the height var
boxSize = {height: $(options.scrollBox).height(), width: $(options.scrollBox).width()};
// trigger inital check if elements already visible
// Default jquery plugin behaviour
return this;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment